1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 13:20:27 +01:00
CanvasBlocker/lib/check.js

112 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-02-13 12:28:36 +01: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";
var scope;
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
window.scope.check = {};
scope = window.scope.check;
}
2017-11-07 00:36:44 +01:00
const settings = require("./settings");
2016-02-13 12:28:36 +01:00
const lists = require("./lists");
const {parseErrorStack} = require("./callingStack");
const logging = require("./logging");
2016-02-13 12:28:36 +01:00
scope.check = function check({url, errorStack}){
url = new URL(url || "about:blank");
var match = checkBoth(errorStack, url, settings.get("blockMode", url)).match(
2017-10-05 19:00:00 +02:00
/^(block|allow|fake|ask)(|Readout|Everything|Context|Input|Internal)$/
);
2016-02-13 12:28:36 +01:00
if (match){
return {
type: (match[2] === "Everything" || match[2] === "")?
["context", "readout", "input"]:
2016-02-13 12:28:36 +01:00
[match[2].toLowerCase()],
mode: match[1]
};
}
else {
return {
type: ["context", "readout", "input"],
2016-02-13 12:28:36 +01:00
mode: "block"
};
}
};
function checkBoth(errorStack, url, blockMode){
if (settings.enableStackList && errorStack && checkStack(errorStack)){
2016-02-13 12:28:36 +01:00
return "allow";
}
else {
return checkURL(url, blockMode);
}
}
function checkURL(url, blockMode){
logging.message("check url %s for block mode %s", url, blockMode);
2016-02-13 12:28:36 +01:00
switch (url.protocol){
case "about:":
if (url.href === "about:blank"){
logging.message("use regular mode on about:blank");
2016-02-13 12:28:36 +01:00
break;
}
logging.message("allow internal URLs");
return "allowInternal";
2016-02-13 12:28:36 +01:00
case "chrome:":
logging.message("allow internal URLs");
return "allowInternal";
2016-02-13 12:28:36 +01:00
}
var mode = "block";
switch (blockMode){
case "blockEverything":
mode = "block";
break;
case "block":
case "blockContext":
case "blockReadout":
case "blockInput":
2016-02-13 12:28:36 +01:00
case "ask":
case "askContext":
case "askReadout":
case "askInput":
2016-02-13 12:28:36 +01:00
case "fake":
case "fakeContext":
case "fakeReadout":
case "fakeInput":
2016-02-13 12:28:36 +01:00
case "allow":
case "allowContext":
case "allowReadout":
case "allowInput":
2016-02-13 12:28:36 +01:00
if (url && lists.get("white").match(url)){
mode = "allow";
}
else if (url && lists.get("black").match(url)){
mode = "block";
}
else {
mode = blockMode;
}
break;
case "allowEverything":
mode = "allow";
break;
default:
logging.warning("Unknown blocking mode (" + blockMode + "). Default to block everything.");
2016-02-13 12:28:36 +01:00
}
return mode;
}
function checkStack(errorStack){
var callingStack = parseErrorStack(errorStack);
return lists.get("stack").match(callingStack);
2016-02-13 12:28:36 +01:00
}
scope.checkStack = checkStack;
2016-02-13 12:28:36 +01:00
}());