1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-01 02:48:44 +01:00
CanvasBlocker/browserAction/browserAction.js

161 lines
4.8 KiB
JavaScript
Raw Normal View History

2018-07-29 21:43:40 +02:00
/* 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/. */
(function(){
"use strict";
2019-04-09 08:29:52 +02:00
const extension = require("../lib/extension");
2019-04-08 00:02:29 +02:00
const logging = require("../lib/logging");
const settings = require("../lib/settings");
const settingContainers = require("../lib/settingContainers");
const lists = require("../lib/lists");
require("../lib/theme").init();
2018-07-29 21:43:40 +02:00
logging.message("Opened browser action");
browser.tabs.query({active: true}).then(async function([currentTab]){
function isWhitelisted(url){
if (!(url instanceof URL)){
url = new URL(url);
}
return lists.get("white").match(url) ||
settings.get("blockMode", url).startsWith("allow");
}
2018-07-29 21:43:40 +02:00
const currentURL = new URL(currentTab.url);
const reloadButton = document.getElementById("reload");
reloadButton.addEventListener("click", async function(){
await browser.tabs.reload(currentTab.id);
window.close();
});
const addonStatus = document.getElementById("addonStatus");
addonStatus.addEventListener("click", async function(){
reloadButton.classList.toggle("hidden");
if (isWhitelisted(currentURL)){
settingContainers.resetUrlValue("blockMode", currentURL);
if (settings.get("blockMode").startsWith("allow")){
settings.set("blockMode", "fake", currentURL.host);
2018-07-29 21:43:40 +02:00
}
const entries = lists.get("white").filter(e => e.match(currentURL)).map(e => e.value);
await Promise.all([
lists.removeFrom("white", entries),
lists.removeFrom("sessionWhite", entries)
]);
}
else {
settings.set("blockMode", "allowEverything", 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",
2021-11-01 13:18:38 +01:00
icon: extension.getURL("icons/pageAction-showOptions.svg"),
action: function(){
if (browser.runtime && browser.runtime.openOptionsPage){
browser.runtime.openOptionsPage();
2018-07-31 14:17:39 +02:00
}
else {
2021-11-01 13:18:38 +01:00
window.open(extension.getURL("options/options.html"), "_blank");
2018-07-29 21:43:40 +02:00
}
window.close();
}
},
{
label: "faq",
2021-11-01 13:18:38 +01:00
icon: extension.getURL("icons/browserAction-faq.svg"),
action: function(){
window.open("https://canvasblocker.kkapsner.de/faq/", "_blank");
window.close();
}
},
{
label: "test",
advanced: true,
2021-11-01 13:18:38 +01:00
icon: extension.getURL("icons/browserAction-test.svg"),
action: function(){
window.open("https://canvasblocker.kkapsner.de/test", "_blank");
window.close();
}
},
{
label: "review",
2021-11-01 13:18:38 +01:00
icon: extension.getURL("icons/browserAction-review.svg"),
action: function(){
window.open("https://addons.mozilla.org/firefox/addon/canvasblocker/reviews/", "_blank");
window.close();
}
},
{
label: "reportIssue",
2021-11-01 13:18:38 +01:00
icon: extension.getURL("icons/browserAction-reportIssue.svg"),
action: function(){
window.open("https://github.com/kkapsner/CanvasBlocker/issues", "_blank");
window.close();
}
},
];
settings.onloaded(async function(){
const actions = document.getElementById("actions");
actionDefinitions.forEach(function(action){
2018-07-29 21:43:40 +02:00
logging.verbose("Action", action);
if (action.advanced && !settings.displayAdvancedSettings){
logging.verbose("Hiding advanced action");
return;
}
2019-11-28 01:26:35 +01:00
const actionButton = document.createElement("button");
2018-07-29 21:43:40 +02:00
actionButton.className = "action";
2019-11-28 01:26:35 +01:00
const icon = document.createElement("span");
icon.className = "icon";
function setIcon(url){
icon.style.maskImage = "url(" + url + ")";
}
setIcon(action.icon);
2018-07-29 21:43:40 +02:00
actionButton.appendChild(icon);
const textNode = document.createTextNode("");
function setLabel(label){
textNode.nodeValue = extension.getTranslation("browserAction_" + label) || label;
}
setLabel(action.label);
actionButton.appendChild(textNode);
actionButton.addEventListener("click", function(){
action.action.call(this, {setIcon, setLabel});
});
2018-07-29 21:43:40 +02:00
actions.appendChild(actionButton);
});
2019-11-28 01:26:35 +01:00
const search = document.createElement("input");
2019-04-09 08:29:52 +02:00
search.placeholder = extension.getTranslation("search");
search.className = "search action";
actions.appendChild(search);
search.focus();
search.addEventListener("keypress", function(event){
if ([10, 13].indexOf(event.keyCode) !== -1){
2021-11-01 13:18:38 +01:00
window.open(extension.getURL(
"options/options.html" +
"?search=" +
encodeURIComponent(this.value)
));
window.close();
}
});
2018-07-29 21:43:40 +02:00
});
}());