2015-09-08 11:41:33 +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";
|
2017-06-25 22:22:17 +02:00
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
window.scope.askForPermission = {};
|
|
|
|
scope = window.scope.askForPermission;
|
|
|
|
}
|
|
|
|
|
2016-02-13 12:28:36 +01:00
|
|
|
const {parseErrorStack} = require("./callingStack");
|
2015-09-08 11:41:33 +02:00
|
|
|
|
|
|
|
// Check canvas appearance
|
2018-06-21 00:13:32 +02:00
|
|
|
function canvasAppearance(window, api, context){
|
2015-09-08 11:41:33 +02:00
|
|
|
var oldBorder = false;
|
|
|
|
var canvas = false;
|
|
|
|
var inDOM = null;
|
2018-06-21 00:13:32 +02:00
|
|
|
if (api === "canvas" && context){
|
2016-04-30 15:05:20 +02:00
|
|
|
var nodeName;
|
|
|
|
try {
|
2016-08-06 19:17:36 +02:00
|
|
|
nodeName = context.nodeName;
|
2016-04-30 15:05:20 +02:00
|
|
|
}
|
2017-10-03 15:35:31 +02:00
|
|
|
catch (e){
|
|
|
|
nodeName = "";
|
|
|
|
}
|
2016-04-30 15:05:20 +02:00
|
|
|
if (nodeName === "CANVAS"){
|
2015-09-08 11:41:33 +02:00
|
|
|
canvas = context;
|
|
|
|
}
|
|
|
|
else if (
|
|
|
|
context instanceof window.CanvasRenderingContext2D ||
|
|
|
|
context instanceof window.WebGLRenderingContext
|
|
|
|
){
|
|
|
|
canvas = context.canvas;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (canvas){
|
|
|
|
oldBorder = canvas.style.border;
|
|
|
|
canvas.style.border = "2px solid red";
|
|
|
|
inDOM = canvas.ownerDocument.contains(canvas);
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
canvas: canvas,
|
2018-06-21 00:13:32 +02:00
|
|
|
askCategory: canvas? (inDOM? "visible": "invisible"): (api === "canvas"? "nocanvas": api),
|
2015-09-08 11:41:33 +02:00
|
|
|
get text(){
|
2018-06-21 00:13:32 +02:00
|
|
|
var text = canvas? (this.visible? "visible": "invisible"): (api === "canvas"? "nocanvas": api);
|
2015-09-08 11:41:33 +02:00
|
|
|
Object.defineProperty(this, "text", {value: text});
|
|
|
|
return text;
|
|
|
|
},
|
|
|
|
inDom: inDOM,
|
|
|
|
get visible(){
|
|
|
|
var visible = inDOM;
|
|
|
|
if (inDOM){
|
|
|
|
canvas.scrollIntoView();
|
|
|
|
var rect = canvas.getBoundingClientRect();
|
2017-10-06 16:06:31 +02:00
|
|
|
var foundEl = window.document.elementFromPoint(
|
|
|
|
rect.left + rect.width / 2,
|
|
|
|
rect.top + rect.height / 2
|
|
|
|
);
|
2015-09-08 11:41:33 +02:00
|
|
|
visible = (foundEl === canvas);
|
|
|
|
}
|
|
|
|
Object.defineProperty(this, "visible", {value: visible});
|
|
|
|
return visible;
|
|
|
|
},
|
|
|
|
reset: function(){
|
|
|
|
if (canvas){
|
|
|
|
canvas.style.border = oldBorder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var modes = new WeakMap();
|
2016-02-13 12:28:36 +01:00
|
|
|
function getAskMode(window, type, _){
|
2015-09-08 11:41:33 +02:00
|
|
|
var mode = modes.get(window);
|
|
|
|
if (mode){
|
|
|
|
return mode[type];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = {
|
|
|
|
context: {
|
|
|
|
askText: {
|
|
|
|
visible: _("askForVisiblePermission"),
|
|
|
|
invisible: _("askForInvisiblePermission"),
|
2018-06-21 00:13:32 +02:00
|
|
|
nocanvas: _("askForPermission"),
|
|
|
|
audio: _("askForAudioPermission")
|
2015-09-08 11:41:33 +02:00
|
|
|
},
|
|
|
|
askStatus: {
|
|
|
|
alreadyAsked: {},
|
|
|
|
answer: {}
|
|
|
|
}
|
|
|
|
},
|
2017-06-25 21:14:13 +02:00
|
|
|
input: {
|
|
|
|
askText: {
|
|
|
|
visible: _("askForVisibleInputPermission"),
|
|
|
|
invisible: _("askForInvisibleInputPermission"),
|
2018-06-21 00:13:32 +02:00
|
|
|
nocanvas: _("askForInputPermission"),
|
|
|
|
audio: _("askForAudioInputPermission")
|
2017-06-25 21:14:13 +02:00
|
|
|
},
|
|
|
|
askStatus: {
|
|
|
|
alreadyAsked: {},
|
|
|
|
answer: {}
|
|
|
|
}
|
|
|
|
},
|
2015-09-08 11:41:33 +02:00
|
|
|
readout: {
|
|
|
|
askText: {
|
|
|
|
visible: _("askForVisibleReadoutPermission"),
|
|
|
|
invisible: _("askForInvisibleReadoutPermission"),
|
2018-06-21 00:13:32 +02:00
|
|
|
nocanvas: _("askForReadoutPermission"),
|
|
|
|
audio: _("askForAudioReadoutPermission")
|
2015-09-08 11:41:33 +02:00
|
|
|
},
|
|
|
|
askStatus: {
|
|
|
|
alreadyAsked: {},
|
|
|
|
answer: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
modes.set(window, mode);
|
|
|
|
return mode[type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 00:13:32 +02:00
|
|
|
scope.ask = function({window, type, api, canvas, errorStack}, {_, prefs}){
|
2015-09-08 11:41:33 +02:00
|
|
|
var answer;
|
2016-02-13 12:28:36 +01:00
|
|
|
var askMode = getAskMode(window, type, _);
|
2015-09-08 11:41:33 +02:00
|
|
|
var askStatus = askMode.askStatus;
|
2018-06-21 00:13:32 +02:00
|
|
|
var appearance = canvasAppearance(window, api, canvas);
|
|
|
|
var category = appearance.askCategory;
|
|
|
|
if (prefs("askOnlyOnce") !== "no" && askStatus.alreadyAsked[category]){
|
2015-09-08 11:41:33 +02:00
|
|
|
// already asked
|
|
|
|
appearance.reset();
|
2018-06-21 00:13:32 +02:00
|
|
|
return askStatus.answer[category];
|
2015-09-08 11:41:33 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-01-04 13:36:18 +01:00
|
|
|
let imgContainer = null;
|
|
|
|
if (type === "readout" && prefs("showCanvasWhileAsking") && canvas){
|
|
|
|
try {
|
|
|
|
let content = canvas.toDataURL();
|
|
|
|
let document = window.top.document;
|
|
|
|
imgContainer = document.createElement("div");
|
|
|
|
imgContainer.style.cssText = `
|
|
|
|
position: fixed;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
|
|
|
right: 0;
|
|
|
|
bottom: 0;
|
|
|
|
background-color: rgba(0, 0, 0, 0.7);
|
|
|
|
color: white;
|
|
|
|
text-align: center;
|
|
|
|
z-index: 100000000000000;
|
|
|
|
padding: 1em;`;
|
|
|
|
|
|
|
|
let heading = document.createElement("h1");
|
|
|
|
heading.textContent = "CanvasBlocker";
|
|
|
|
imgContainer.appendChild(heading);
|
|
|
|
|
|
|
|
let text = document.createElement("div");
|
|
|
|
text.style.margin = "0.5em auto";
|
|
|
|
text.textContent = browser.i18n.getMessage("showCanvasWhileAsking_message");
|
|
|
|
imgContainer.appendChild(text);
|
|
|
|
|
|
|
|
let img = document.createElement("img");
|
|
|
|
img.style.backgroundColor = "white";
|
|
|
|
img.style.border = "2px solid lightgray";
|
|
|
|
img.src = HTMLCanvasElement.prototype.toDataURL.call(canvas);
|
|
|
|
imgContainer.appendChild(img);
|
|
|
|
document.body.appendChild(imgContainer);
|
|
|
|
}
|
|
|
|
catch (e){
|
|
|
|
// unable to read the canvas
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 11:41:33 +02:00
|
|
|
// asking
|
2017-06-25 21:14:13 +02:00
|
|
|
var msg = askMode.askText[appearance.text];
|
2018-06-21 00:13:32 +02:00
|
|
|
|
|
|
|
// visible vs invisible is only calculated here correctly
|
|
|
|
category = appearance.text;
|
2016-02-13 12:28:36 +01:00
|
|
|
if (prefs("showCallingFile")){
|
|
|
|
msg += parseErrorStack(errorStack).toString(_);
|
2015-09-08 11:41:33 +02:00
|
|
|
}
|
2018-01-04 13:30:48 +01:00
|
|
|
answer = window.top.confirm(msg)? "allow": prefs("askDenyMode");
|
2018-01-04 13:36:18 +01:00
|
|
|
if (imgContainer && imgContainer.parentNode){
|
|
|
|
imgContainer.parentNode.removeChild(imgContainer);
|
|
|
|
}
|
2017-11-10 23:45:09 +01:00
|
|
|
|
|
|
|
if (prefs("askOnlyOnce") === "combined"){
|
|
|
|
["context", "readout", "input"].forEach(function(type){
|
|
|
|
var askMode = getAskMode(window, type, _);
|
|
|
|
var askStatus = askMode.askStatus;
|
2018-06-21 00:13:32 +02:00
|
|
|
askStatus.alreadyAsked[category] = true;
|
|
|
|
askStatus.answer[category] = answer;
|
2017-11-10 23:45:09 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2018-06-21 00:13:32 +02:00
|
|
|
askStatus.alreadyAsked[category] = true;
|
|
|
|
askStatus.answer[category] = answer;
|
2017-11-10 23:45:09 +01:00
|
|
|
}
|
2015-09-08 11:41:33 +02:00
|
|
|
appearance.reset();
|
|
|
|
return answer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|