From f85303065c2868aa639351ff59055903c2ff1e25 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Wed, 22 Feb 2017 17:46:17 +0100 Subject: [PATCH] Only interfecpt constructors that are present. Fixes #108. --- lib/intercept.js | 95 +++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/lib/intercept.js b/lib/intercept.js index 8eccb8a..dc3b929 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -41,56 +41,59 @@ var changedFunction = changedFunctions[name]; if (changedFunction.getStatus(undefined, siteStatus).active){ (Array.isArray(changedFunction.object)? changedFunction.object: [changedFunction.object]).forEach(function(object){ - var original = window.wrappedJSObject[object].prototype[name]; - - Object.defineProperty( - window.wrappedJSObject[object].prototype, - name, - { - enumerable: true, - configureable: false, - get: function(){ - var url = getURL(window); - if (!url){ - return undef; - } - var error = new Error(); - if (checkStack(error.stack)){ - return original; - } - var funcStatus = changedFunction.getStatus(this, siteStatus); - - if (funcStatus.active){ - if (funcStatus.mode === "ask"){ - funcStatus.mode = ask({window: window, type: changedFunction.type, canvas: this, errorStack: error.stack}); + var constructor = window.wrappedJSObject[object]; + if (constructor){ + var original = constructor.prototype[name]; + + Object.defineProperty( + constructor.prototype, + name, + { + enumerable: true, + configureable: false, + get: function(){ + var url = getURL(window); + if (!url){ + return undef; } - switch (funcStatus.mode){ - case "allow": - return original; - case "fake": - setRandomSupplyByType(prefs("rng")); - var fake = changedFunction.fakeGenerator(prefs, function(messageId){ - notify({url, errorStack: error.stack, messageId}); - }, original); - switch (fake){ - case true: - return original; - case false: - return undef; - default: - return exportFunction(fake, window.wrappedJSObject); - } - //case "block": - default: - return undef; + var error = new Error(); + if (checkStack(error.stack)){ + return original; + } + var funcStatus = changedFunction.getStatus(this, siteStatus); + + if (funcStatus.active){ + if (funcStatus.mode === "ask"){ + funcStatus.mode = ask({window: window, type: changedFunction.type, canvas: this, errorStack: error.stack}); + } + switch (funcStatus.mode){ + case "allow": + return original; + case "fake": + setRandomSupplyByType(prefs("rng")); + var fake = changedFunction.fakeGenerator(prefs, function(messageId){ + notify({url, errorStack: error.stack, messageId}); + }, original); + switch (fake){ + case true: + return original; + case false: + return undef; + default: + return exportFunction(fake, window.wrappedJSObject); + } + //case "block": + default: + return undef; + } + } + else { + return original; } - } - else { - return original; } } - } - ); + ); + } }); } });