Configurable capability to show noscript elements on script-disabled pages.

This commit is contained in:
hackademix 2021-02-14 19:57:27 +01:00
parent 12c654f130
commit c42cbb7290
5 changed files with 77 additions and 91 deletions

View File

@ -577,6 +577,9 @@
"cap_ping": {
"message": "ping"
},
"cap_noscript": {
"message": "noscript"
},
"cap_other": {
"message": "other"
},

View File

@ -238,7 +238,19 @@ var LifeCycle = (() => {
if (Ver.is(previousVersion, "<=", "11.0.10")) {
log(`Upgrading from 11.0.10 or below (${previousVersion}): configure the "ping" capability.`);
await ns.initializing;
ns.policy.TRUSTED.capabilities.add("ping")
ns.policy.TRUSTED.capabilities.add("ping");
await ns.savePolicy();
}
if (Ver.is(previousVersion, "<", "11.2.rc4")) {
log(`Upgrading from ${previousVersion}: configure the "noscript" capability.`);
await ns.initializing;
let {DEFAULT, TRUSTED, UNTRUSTED} = ns.policy;
// let's add "noscript" to DEFAULY, TRUSTED and any CUSTOM preset
let presets = [DEFAULT, TRUSTED];
presets = presets.concat([...ns.policy.sites.values()].filter(p => p !== TRUSTED && p !== UNTRUSTED));
for (let p of presets) {
p.capabilities.add("noscript");
}
await ns.savePolicy();
}
},

View File

@ -1,99 +1,69 @@
// depends on /nscl/content/NoscriptElements.js
"use strict";
function onScriptDisabled() {
if (document.readyState === "loading") {
if (!onScriptDisabled._installed) {
window.addEventListener("DOMContentLoaded", e => onScriptDisabled());
onScriptDisabled._installed = true;
}
return;
}
onScriptDisabled = () => {};
let refresh = false;
for (let noscript of document.querySelectorAll("noscript")) {
// force show NOSCRIPT elements content
let replacement = createHTMLElement("span");
replacement.innerHTML = noscript.innerHTML;
// emulate meta-refresh
for (let meta of replacement.querySelectorAll('meta[http-equiv="refresh"]')) {
refresh = true;
document.head.appendChild(meta);
console.log(`State %s, emulating`, document.readyState, meta);
let emulateNoScriptElement = () => {
if (ns.allows("noscript")) {
NoscriptElements.emulate(true);
}
};
if (noscript.closest("head") && document.body) {
document.body.insertBefore(noscript, document.body.firstChild);
}
noscript.replaceWith(replacement);
if (document.readyState === "loading") {
window.addEventListener("DOMContentLoaded", emulateNoScriptElement, true);
return;
} else {
emulateNoScriptElement();
}
if (refresh) {
let html = document.documentElement.outerHTML;
let rewrite = () => {
let document = window.wrappedJSObject ? window.wrappedJSObject.document : window.document;
try {
document.open();
document.write(html);
document.close();
} catch (e) {
error(e);
}
};
if (document.readyState === "complete") {
rewrite();
} else {
window.addEventListener("load", e => {
if (e.isTrusted) rewrite();
});
}
}
{
let eraser = {
tapped: null,
delKey: false,
};
addEventListener("pagehide", ev => {
if (!ev.isTrusted) return;
let eraser = {
tapped: null,
delKey: false,
};
addEventListener("pagehide", ev => {
if (!ev.isTrusted) return;
eraser.tapped = null;
eraser.delKey = false;
}, false);
addEventListener("keyup", ev => {
if (!ev.isTrusted) return;
let el = eraser.tapped;
if (el && ev.code === "Delete" || ev.code === "Backspace") {
eraser.tapped = null;
eraser.delKey = true;
let doc = el.ownerDocument;
let w = doc.defaultView;
if (w.getSelection().isCollapsed) {
let root = doc.body || doc.documentElement;
let posRx = /^(?:absolute|fixed)$/;
do {
if (posRx.test(w.getComputedStyle(el, '').position)) {
(eraser.tapped = el.parentNode).removeChild(el);
break;
}
} while ((el = el.parentNode) && el != root);
}
}
}, true);
addEventListener("mousedown", ev => {
if (!ev.isTrusted) return;
if (ev.button === 0) {
eraser.tapped = ev.target;
eraser.delKey = false;
}, false);
}
}, true);
addEventListener("keyup", ev => {
if (!ev.isTrusted) return;
let el = eraser.tapped;
if (el && ev.code === "Delete" || ev.code === "Backspace") {
eraser.tapped = null;
eraser.delKey = true;
let doc = el.ownerDocument;
let w = doc.defaultView;
if (w.getSelection().isCollapsed) {
let root = doc.body || doc.documentElement;
let posRx = /^(?:absolute|fixed)$/;
do {
if (posRx.test(w.getComputedStyle(el, '').position)) {
(eraser.tapped = el.parentNode).removeChild(el);
break;
}
} while ((el = el.parentNode) && el != root);
}
}
}, true);
addEventListener("mousedown", ev => {
if (!ev.isTrusted) return;
if (ev.button === 0) {
eraser.tapped = ev.target;
eraser.delKey = false;
}
}, true);
addEventListener("mouseup", ev => {
if (!ev.isTrusted) return;
if (eraser.delKey) {
eraser.delKey = false;
ev.preventDefault();
ev.stopPropagation();
}
eraser.tapped = null;
}, true);
}
addEventListener("mouseup", ev => {
if (!ev.isTrusted) return;
if (eraser.delKey) {
eraser.delKey = false;
ev.preventDefault();
ev.stopPropagation();
}
eraser.tapped = null;
}, true);
}

View File

@ -100,8 +100,9 @@
"lib/CSP.js",
"nscl/content/patchWindow.js",
"common/CapsCSP.js",
"/nscl/common/RequestKey.js",
"nscl/common/RequestKey.js",
"content/DocumentCSP.js",
"nscl/content/NoscriptElements.js",
"content/onScriptDisabled.js",
"content/staticNS.js",
"content/PlaceHolder.js",

@ -1 +1 @@
Subproject commit 077b98985354ca42b9a73fd49d3c8c5b8b42ddfe
Subproject commit 48d590749688928dda8e27e7e1e4e388057f4836