summaryrefslogtreecommitdiff
path: root/src/content
diff options
context:
space:
mode:
authorhackademix2018-08-15 16:41:55 +0200
committerhackademix2018-08-16 23:43:36 +0200
commit57d883d63e9b8232e275dac7eec24f84c040dd24 (patch)
treea4a9f05f0744ccef23e31643f80c400abe0dab37 /src/content
parent2c75eedadddd8bda0522cddaf119e1e5c621c7c2 (diff)
downloadnoscript-57d883d63e9b8232e275dac7eec24f84c040dd24.tar.gz
noscript-57d883d63e9b8232e275dac7eec24f84c040dd24.tar.xz
noscript-57d883d63e9b8232e275dac7eec24f84c040dd24.zip
Policy serialization using the contentScripts API.
Diffstat (limited to 'src/content')
-rw-r--r--src/content/content.js30
-rw-r--r--src/content/media.js103
-rw-r--r--src/content/webglHook.js11
3 files changed, 85 insertions, 59 deletions
diff --git a/src/content/content.js b/src/content/content.js
index 886c363..37d0d32 100644
--- a/src/content/content.js
+++ b/src/content/content.js
@@ -1,7 +1,34 @@
'use strict';
// debug = () => {}; // REL_ONLY
-
+{
+ let listenersMap = new Map();
+ var ns = {
+ on(eventName, listener) {
+ let listeners = listenersMap.get(eventName);
+ if (!listeners) listenersMap.set(eventName, listeners = new Set());
+ listeners.add(listener);
+ },
+ detach(eventName, listener) {
+ let listeners = listenersMap.get(eventName);
+ if (listeners) listeners.delete(listener);
+ },
+ fire(eventName) {
+ let listeners = listenersMap.get(eventName);
+ if (listeners) {
+ for (let l of listeners) {
+ l(this);
+ }
+ }
+ },
+ perms: { DEFAULT: null, CURRENT: null },
+ allows(cap) {
+ let perms = this.perms.CURRENT;
+ return perms && perms.capabilities.includes(cap);
+ }
+ }
+}
+
var canScript = true, shouldScript = false;
let now = () => performance.now() + performance.timeOrigin;
@@ -24,7 +51,6 @@ function probe() {
var _ = browser.i18n.getMessage;
-
var embeddingDocument = false;
var seen = {
diff --git a/src/content/media.js b/src/content/media.js
index ffa67e0..5da239f 100644
--- a/src/content/media.js
+++ b/src/content/media.js
@@ -1,60 +1,59 @@
-{
- debug("Media Hook (blocked %s)", !!window.mediaBlocker, document.URL, document.documentElement && document.documentElement.innerHTML); // DEV_ONLY
- (() => {
- let unpatched = new Map();
- function patch(obj, methodName, replacement) {
- let methods = unpatched.get(obj) || {};
- methods[methodName] = obj[methodName];
- exportFunction(replacement, obj, {defineAs: methodName});
- unpatched.set(obj, methods);
+ns.on("perms", ns => {
+ debug("Media Hook", document.URL, document.documentElement && document.documentElement.innerHTML, ns.perms.CURRENT); // DEV_ONLY
+ let mediaBlocker = !ns.allows("media");
+ let unpatched = new Map();
+ function patch(obj, methodName, replacement) {
+ let methods = unpatched.get(obj) || {};
+ methods[methodName] = obj[methodName];
+ exportFunction(replacement, obj, {defineAs: methodName});
+ unpatched.set(obj, methods);
+ }
+ let urlMap = new WeakMap();
+ patch(window.URL, "createObjectURL", function(o, ...args) {
+ let url = unpatched.get(window.URL).createObjectURL.call(this, o, ...args);
+ if (o instanceof MediaSource) {
+ let urls = urlMap.get(o);
+ if (!urls) urlMap.set(o, urls = new Set());
+ urls.add(url);
}
- let urlMap = new WeakMap();
- patch(window.URL, "createObjectURL", function(o, ...args) {
- let url = unpatched.get(window.URL).createObjectURL.call(this, o, ...args);
- if (o instanceof MediaSource) {
- let urls = urlMap.get(o);
- if (!urls) urlMap.set(o, urls = new Set());
- urls.add(url);
- }
- return url;
- });
+ return url;
+ });
- patch(window.MediaSource.prototype, "addSourceBuffer", function(mime, ...args) {
- let ms = this;
- let urls = urlMap.get(ms);
+ patch(window.MediaSource.prototype, "addSourceBuffer", function(mime, ...args) {
+ let ms = this;
+ let urls = urlMap.get(ms);
- let request = {
- id: "noscript-media",
- type: "media",
- url: document.URL,
- documentUrl: document.URL,
- embeddingDocument: true,
- };
- seen.record({policyType: "media", request, allowed: false});
- notifyPage();
+ let request = {
+ id: "noscript-media",
+ type: "media",
+ url: document.URL,
+ documentUrl: document.URL,
+ embeddingDocument: true,
+ };
+ seen.record({policyType: "media", request, allowed: false});
+ notifyPage();
- if (window.mediaBlocker) {
- (async () => {
- let me = Array.from(document.querySelectorAll("video,audio"))
- .find(e => e.srcObject === ms || urls && urls.has(e.src));
+ if (mediaBlocker) {
+ (async () => {
+ let me = Array.from(document.querySelectorAll("video,audio"))
+ .find(e => e.srcObject === ms || urls && urls.has(e.src));
- if (!me) return;
- let exposedMime = `${mime} (MSE)`;
+ if (!me) return;
+ let exposedMime = `${mime} (MSE)`;
- try {
- let ph = PlaceHolder.create("media", request);
- ph.replace(me);
- PlaceHolder.listen();
- } catch (e) {
- error(e);
- }
- })();
- throw new Error(`${exposedMime} blocked by NoScript`);
- }
+ try {
+ let ph = PlaceHolder.create("media", request);
+ ph.replace(me);
+ PlaceHolder.listen();
+ } catch (e) {
+ error(e);
+ }
+ })();
+ throw new Error(`${exposedMime} blocked by NoScript`);
+ }
- return unpatched.get(window.MediaSource.prototype).addSourceBuffer.call(ms, mime, ...args);
- });
+ return unpatched.get(window.MediaSource.prototype).addSourceBuffer.call(ms, mime, ...args);
+ });
- })();
- document.URL;
-}
+})();
+document.URL;
diff --git a/src/content/webglHook.js b/src/content/webglHook.js
index d4c064a..67f31eb 100644
--- a/src/content/webglHook.js
+++ b/src/content/webglHook.js
@@ -1,5 +1,6 @@
-{
- debug("WebGL Hook", document.URL, document.documentElement && document.documentElement.innerHTML); // DEV_ONLY
+ns.on("perms", ns => {
+ debug("WebGL Hook", document.URL, document.documentElement && document.documentElement.innerHTML, ns.perms.CURRENT); // DEV_ONLY
+ if (ns.allows("webgl")) return;
let proto = HTMLCanvasElement.prototype;
let getContext = proto.getContext;
exportFunction(function(type, ...rest) {
@@ -23,6 +24,6 @@
return {};
}
return getContext.call(this, type, ...rest);
- }, proto, {defineAs: "getContext"});
- document.URL;
-}
+ }, proto, {defineAs: "getContext"});
+});
+document.URL;