1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 04:26:35 +02:00

Added logging lib with setting to control log level.

This commit is contained in:
kkapsner 2017-07-27 19:14:04 +02:00
parent 65ad3a5814
commit 9715eb09d2
13 changed files with 299 additions and 108 deletions

View file

@ -9,8 +9,9 @@
<ul id="prints">
<li>...</li>
</ul>
<script src="../lib/require.js"></script>
<script src="../lib/defaultSettings.js"></script>
<script src="../lib/require.js"></script>
<script src="../lib/logging.js"></script>
<script src="../lib/lists.js"></script>
<script src="../lib/callingStack.js"></script>
<script src="pageAction.js"></script>

View file

@ -1,38 +1,7 @@
// console.log(window, browser, browser.runtime);
function modalPrompt(message, defaultValue){
return new Promise(function(resolve, reject){
document.body.innerHTML = "";
document.body.appendChild(document.createTextNode(message));
var input = document.createElement("input");
input.value = defaultValue;
document.body.appendChild(input);
var button = document.createElement("button");
button.textContent = "OK";
button.addEventListener("click", function(){
resolve(input.value);
});
document.body.appendChild(button);
});
}
function log(...args){
function leftPad(str, char, pad){
str = "" + str;
return char.repeat(pad - str.length) + str;
}
args.unshift("page action script:");
var now = new Date();
args.unshift(
now.getFullYear() + "-" +
leftPad(now.getMonth() + 1, "0", 2) + "-" +
leftPad(now.getDate(), "0", 2) + " " +
leftPad(now.getHours(), "0", 2) + ":" +
leftPad(now.getMinutes(), "0", 2) + ":" +
leftPad(now.getSeconds(), "0", 2) + "." +
leftPad(now.getMilliseconds(), "0", 3)
);
console.log.apply(console, args);
}
/* jslint moz: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Promise.all([
browser.tabs.query({active: true, currentWindow: true}),
@ -42,7 +11,31 @@ Promise.all([
});
return settings;
})
]).then(function([tabs, settings]){
]).then(function(values){
"use strict";
const [tabs, settings] = values;
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = require("./logging");
setLogPrefix("page action script");
function modalPrompt(message, defaultValue){
message("open modal prompt");
return new Promise(function(resolve, reject){
document.body.innerHTML = "";
document.body.appendChild(document.createTextNode(message));
var input = document.createElement("input");
input.value = defaultValue;
document.body.appendChild(input);
var button = document.createElement("button");
button.textContent = "OK";
button.addEventListener("click", function(){
resolve(input.value);
message("modal prompt closed with value", input.value);
});
document.body.appendChild(button);
});
}
if (!tabs.length){
throw new Error("noTabsFound");
}
@ -108,10 +101,10 @@ Promise.all([
var tab = tabs[0];
browser.runtime.onMessage.addListener(function(data){
if (Array.isArray(data["canvasBlocker-notifications"])){
log("got notifications", data);
message("got notifications");
var ul = document.getElementById("prints");
data["canvasBlocker-notifications"].forEach(function(notification){
console.log(notification);
verbose(notification);
var url = new URL(notification.url);
var li = document.createElement("li");
@ -157,17 +150,17 @@ Promise.all([
});
}
});
log("clearing the display");
notice("clearing the display");
var ul = document.getElementById("prints");
ul.innerHTML = "";
log("request notifications from tab", tab.id);
message("request notifications from tab", tab.id);
browser.tabs.sendMessage(
tab.id,
{
"canvasBlocker-sendNotifications": tab.id
}
);
log("waiting for notifications");
notice("waiting for notifications");
}).catch(function(e){
console.error(e);
});