1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00

Added status button in browser action to see and set the whitelist status

Fixes #535
This commit is contained in:
kkapsner 2021-05-25 16:44:20 +02:00
parent 16bef43945
commit 42b19a4ba5
7 changed files with 156 additions and 57 deletions

View File

@ -24,6 +24,14 @@
"message": "\n \u00B7 {api}", "message": "\n \u00B7 {api}",
"description": "" "description": ""
}, },
"browserAction_status_on": {
"message": "CanvasBlocker on",
"description": ""
},
"browserAction_status_off": {
"message": "CanvasBlocker off",
"description": ""
},
"more": { "more": {
"message": "more", "message": "more",

View File

@ -20,3 +20,26 @@ div {
.action.search { .action.search {
padding-left: calc(0.5em + 19px + 0.25em); padding-left: calc(0.5em + 19px + 0.25em);
} }
#addonStatus {
border: none;
display: block;
margin: 5px auto;
width: 40px;
min-width: 0;
height: 40px;
background: none;
background-position: 50%;
background-size: 100%;
cursor: pointer;
}
#addonStatus.unknown {
background-image: radial-gradient(black, rgba(0, 0, 0, 0), transparent);;
}
#addonStatus.off {
background-image: url(../icons/browserAction-whitelisted.svg);
}
#addonStatus.on {
background-image: url(../icons/browserAction-notPrinted.svg);
}

View File

@ -7,6 +7,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head> </head>
<body> <body>
<button id="addonStatus" class="undefined"></button>
<div id="actions" class="stackedInputs"></div> <div id="actions" class="stackedInputs"></div>
<script src="../lib/require.js"></script> <script src="../lib/require.js"></script>
<script src="../lib/logging.js"></script> <script src="../lib/logging.js"></script>
@ -14,6 +15,7 @@
<script src="../lib/settingDefinitions.js"></script> <script src="../lib/settingDefinitions.js"></script>
<script src="../lib/settingContainers.js"></script> <script src="../lib/settingContainers.js"></script>
<script src="../lib/settings.js"></script> <script src="../lib/settings.js"></script>
<script src="../lib/lists.js"></script>
<script src="../lib/theme.js"></script> <script src="../lib/theme.js"></script>
<script src="browserAction.js"></script> <script src="browserAction.js"></script>
</body> </body>

View File

@ -7,55 +7,99 @@
const extension = require("../lib/extension"); const extension = require("../lib/extension");
const logging = require("../lib/logging"); const logging = require("../lib/logging");
const settings = require("../lib/settings"); const settings = require("../lib/settings");
const settingContainers = require("../lib/settingContainers");
const lists = require("../lib/lists");
require("../lib/theme").init(); require("../lib/theme").init();
logging.message("Opened browser action"); logging.message("Opened browser action");
settings.onloaded(function(){
const actions = document.getElementById("actions");
[ browser.tabs.query({active: true}).then(async function([currentTab]){
{ function isWhitelisted(url){
label: "settings", if (!(url instanceof URL)){
icon: browser.extension.getURL("icons/pageAction-showOptions.svg"), url = new URL(url);
action: function(){ }
if (browser.runtime && browser.runtime.openOptionsPage){ return lists.get("white").match(url) ||
browser.runtime.openOptionsPage(); settings.get("blockMode", url).startsWith("allow");
} }
else {
window.open(browser.extension.getURL("options/options.html"), "_blank"); const currentURL = new URL(currentTab.url);
} const addonStatus = document.getElementById("addonStatus");
addonStatus.addEventListener("click", async function(){
if (isWhitelisted(currentURL)){
settingContainers.resetUrlValue("blockMode", currentURL);
if (settings.get("blockMode").startsWith("allow")){
settings.set("blockMode", "fake", currentURL.host);
} }
}, const entries = lists.get("white").filter(e => e.match(currentURL)).map(e => e.value);
{ await Promise.all([
label: "faq", lists.removeFrom("white", entries),
icon: browser.extension.getURL("icons/browserAction-faq.svg"), lists.removeFrom("sessionWhite", entries)
action: function(){ ]);
window.open("https://canvasblocker.kkapsner.de/faq/", "_blank"); }
else {
settings.set("blockMode", "allow", currentURL.hostname);
}
update();
});
function update(){
if (isWhitelisted(currentURL)){
addonStatus.className = "off";
addonStatus.title = extension.getTranslation("browserAction_status_off");
}
else {
addonStatus.className = "on";
addonStatus.title = extension.getTranslation("browserAction_status_on");
}
}
return settings.onloaded(update);
}).catch(function(){});
const actionDefinitions = [
{
label: "settings",
icon: browser.extension.getURL("icons/pageAction-showOptions.svg"),
action: function(){
if (browser.runtime && browser.runtime.openOptionsPage){
browser.runtime.openOptionsPage();
} }
}, else {
{ window.open(browser.extension.getURL("options/options.html"), "_blank");
label: "test",
advanced: true,
icon: browser.extension.getURL("icons/browserAction-test.svg"),
action: function(){
window.open("https://canvasblocker.kkapsner.de/test", "_blank");
} }
}, }
{ },
label: "review", {
icon: browser.extension.getURL("icons/browserAction-review.svg"), label: "faq",
action: function(){ icon: browser.extension.getURL("icons/browserAction-faq.svg"),
window.open("https://addons.mozilla.org/firefox/addon/canvasblocker/reviews/", "_blank"); action: function(){
} window.open("https://canvasblocker.kkapsner.de/faq/", "_blank");
}, }
{ },
label: "reportIssue", {
icon: browser.extension.getURL("icons/browserAction-reportIssue.svg"), label: "test",
action: function(){ advanced: true,
window.open("https://github.com/kkapsner/CanvasBlocker/issues", "_blank"); icon: browser.extension.getURL("icons/browserAction-test.svg"),
} action: function(){
}, window.open("https://canvasblocker.kkapsner.de/test", "_blank");
].forEach(function(action){ }
},
{
label: "review",
icon: browser.extension.getURL("icons/browserAction-review.svg"),
action: function(){
window.open("https://addons.mozilla.org/firefox/addon/canvasblocker/reviews/", "_blank");
}
},
{
label: "reportIssue",
icon: browser.extension.getURL("icons/browserAction-reportIssue.svg"),
action: function(){
window.open("https://github.com/kkapsner/CanvasBlocker/issues", "_blank");
}
},
];
settings.onloaded(async function(){
const actions = document.getElementById("actions");
actionDefinitions.forEach(function(action){
logging.verbose("Action", action); logging.verbose("Action", action);
if (action.advanced && !settings.displayAdvancedSettings){ if (action.advanced && !settings.displayAdvancedSettings){
logging.verbose("Hiding advanced action"); logging.verbose("Hiding advanced action");
@ -66,16 +110,23 @@
const icon = document.createElement("span"); const icon = document.createElement("span");
icon.className = "icon"; icon.className = "icon";
icon.style.maskImage = "url(" + action.icon + ")"; function setIcon(url){
icon.style.maskImage = "url(" + url + ")";
}
setIcon(action.icon);
actionButton.appendChild(icon); actionButton.appendChild(icon);
actionButton.appendChild( const textNode = document.createTextNode("");
document.createTextNode( function setLabel(label){
extension.getTranslation("browserAction_" + action.label) || action.label textNode.nodeValue = extension.getTranslation("browserAction_" + label) || label;
) }
); setLabel(action.label);
actionButton.addEventListener("click", action.action);
actionButton.appendChild(textNode);
actionButton.addEventListener("click", function(){
action.action.call(this, {setIcon, setLabel});
});
actions.appendChild(actionButton); actions.appendChild(actionButton);
}); });

View File

@ -36,6 +36,7 @@
regExp = new RegExp(entry, "i"); regExp = new RegExp(entry, "i");
} }
return { return {
value: entry,
match: function(url){ match: function(url){
if (domain){ if (domain){
return (url.hostname || "").match(regExp); return (url.hostname || "").match(regExp);
@ -119,9 +120,23 @@
await settings.set(type + "List", oldValue + (oldValue? ",": "") + entry); await settings.set(type + "List", oldValue + (oldValue? ",": "") + entry);
return updateList(type); return updateList(type);
}; };
scope.removeFrom = async function removeFromList(type, entry){
const oldValue = settings[type + "List"];
const filter = entry.forEach? v => entry.indexOf(v) === -1: v => v !== entry;
await settings.set(
type + "List",
oldValue
.split(",")
.map(v => v.replace(/^\s+|\s+$/, ""))
.filter(filter)
.join(",")
);
return updateList(type);
};
scope.update = updateList; scope.update = updateList;
scope.updateAll = function updateAllLists(){ scope.updateAll = function updateAllLists(){
updateList("white"); updateList("white");
updateList("sessionWhite");
updateList("ignore"); updateList("ignore");
updateList("black"); updateList("black");
updateStackList(settings.stackList); updateStackList(settings.stackList);

View File

@ -48,18 +48,17 @@
}; };
scope.resetUrlValue = function(name, url){ scope.resetUrlValue = function(name, url){
let urlContainerValue = scope.urlContainer.get(); let urlContainerValue = scope.urlContainer.get();
const matching = urlContainerValue.filter(function(urlSetting){ urlContainerValue.filter(function(urlSetting){
return urlSetting.match(url); return urlSetting.match(url);
}); }).forEach(function(match){
if (matching.length){ delete match[name];
delete matching[0][name]; if (Object.keys(match).every(function(key){return key === "url";})){
if (Object.keys(matching[0]).every(function(key){return key === "url";})){
urlContainerValue = urlContainerValue.filter(function(urlSetting){ urlContainerValue = urlContainerValue.filter(function(urlSetting){
return urlSetting !== matching[0]; return urlSetting !== match;
}); });
} }
scope.urlContainer.set(urlContainerValue); });
} scope.urlContainer.set(urlContainerValue);
}; };
function processHideContainer(settingDefinition){ function processHideContainer(settingDefinition){

View File

@ -7,6 +7,7 @@ Version 1.6:
new features: new features:
- try to not break tabs when updating - try to not break tabs when updating
- setting to postpone updates until browser restart or extension is reloaded - setting to postpone updates until browser restart or extension is reloaded
- added status button in browser action to see and set the whitelist status
fixes: fixes:
- fix message canvasBlocker-unload - fix message canvasBlocker-unload