From eefd6bde58c46469c779f074bdb327bc5368004c Mon Sep 17 00:00:00 2001 From: Peter Snyder Date: Sat, 14 Oct 2017 23:23:40 -0500 Subject: [PATCH] add ability to log what specific methods were blocked --- background_scripts/background.js | 20 ++++++++++----- config/index.html | 7 ++++-- config/js/components/domain-rules.vue.js | 4 +-- config/js/components/logging-settings.vue.js | 26 ++++++++++++++++++++ config/js/config.js | 13 +++++----- config/js/state.js | 18 ++++++++++++++ content_scripts/src/instrument.js | 12 ++++++--- content_scripts/src/proxyblock.js | 10 ++++++-- lib/storage.js | 18 +++++++++----- 9 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 config/js/components/logging-settings.vue.js diff --git a/background_scripts/background.js b/background_scripts/background.js index 74533d5..71491d1 100644 --- a/background_scripts/background.js +++ b/background_scripts/background.js @@ -11,12 +11,14 @@ // (or the default option, "(default)"), to an array of standards // that should be blocked on matching domains. let domainRules; + let shouldLog; // The extension depends on this fetch happening before the DOM on any // pages is loaded. The Chrome and Firefox docs *do not* promise this, // but in testing this is always the case. - storageLib.get(function (loadedDomainRules) { - domainRules = loadedDomainRules; + storageLib.get(function (storedValues) { + domainRules = storedValues.domainRules; + shouldLog = storedValues.shouldLog; }); // Manage the state of the browser activity, by displaying the number @@ -66,7 +68,7 @@ // Listen for updates to the domain rules from the config page. // The two types of messages that are sent to the background page are - // "rulesUpdate", which comes from the config page, indicating the domain + // "stateUpdate", which comes from the config page, indicating the domain // blocking / matching rules have changed, and the "rulesForDomains" // message, which comes from the browserAction popup, and is a request // for information about "here are the domains of the frames on the @@ -74,8 +76,9 @@ rootObject.runtime.onMessage.addListener(function (request, ignore, sendResponse) { const [label, data] = request; - if (label === "rulesUpdate") { - domainRules = data; + if (label === "stateUpdate") { + domainRules = data.domainRules; + shouldLog = data.shouldLog; return; } @@ -127,7 +130,12 @@ details.responseHeaders.push({ name: "Set-Cookie", - value: `web-api-manager=${packedValues}` + value: `wam-standards=${packedValues}` + }); + + details.responseHeaders.push({ + name: "Set-Cookie", + value: `wam-log=${shouldLog ? "true" : "false"}` }); return { diff --git a/config/index.html b/config/index.html index e5171d4..b20c4c0 100644 --- a/config/index.html +++ b/config/index.html @@ -22,8 +22,10 @@
-
+
+ +
@@ -37,11 +39,12 @@ - + + diff --git a/config/js/components/domain-rules.vue.js b/config/js/components/domain-rules.vue.js index 83e688a..6bca531 100644 --- a/config/js/components/domain-rules.vue.js +++ b/config/js/components/domain-rules.vue.js @@ -6,7 +6,7 @@ Vue.component("domain-rules", { props: ["domainNames", "selectedDomain"], template: ` -
+
- +
`, data: function () { diff --git a/config/js/components/logging-settings.vue.js b/config/js/components/logging-settings.vue.js new file mode 100644 index 0000000..7aa515a --- /dev/null +++ b/config/js/components/logging-settings.vue.js @@ -0,0 +1,26 @@ +/*jslint es6: true, this: true*/ +/*global window, Vue*/ +(function () { + "use strict"; + + Vue.component("logging-settings", { + props: ["shouldLog"], + template: ` +
+
+ +
+
+ `, + methods: { + shouldLogChanged: function () { + this.$root.$data.setShouldLog(this.shouldLog); + } + } + }); +}()); \ No newline at end of file diff --git a/config/js/config.js b/config/js/config.js index 4f86cfb..a73dee6 100644 --- a/config/js/config.js +++ b/config/js/config.js @@ -11,9 +11,9 @@ const state = stateLib.generateStateObject(defaultDomain, standards); - const onSettingsLoaded = function (loadedDomainRules) { + const onSettingsLoaded = function (storedSettings) { - state.setDomainRules(loadedDomainRules); + state.populateFromStorage(storedSettings); const vm = new Vue({ el: doc.body, @@ -21,18 +21,17 @@ }); const updateStoredSettings = function () { - storageLib.set(state.domainRules, function () { - rootObject.runtime.sendMessage(["rulesUpdate", state.domainRules]); + storageLib.set(state.toStorage(), function () { + rootObject.runtime.sendMessage(["stateUpdate", state.toStorage()]); }); }; vm.$watch("selectedStandards", updateStoredSettings); vm.$watch("domainNames", updateStoredSettings); + vm.$watch("shouldLog", updateStoredSettings); }; - const onPageLoad = function () { + window.onload = function () { storageLib.get(onSettingsLoaded); }; - - window.onload = onPageLoad; }()); \ No newline at end of file diff --git a/config/js/state.js b/config/js/state.js index b2d37ba..17cf6f7 100644 --- a/config/js/state.js +++ b/config/js/state.js @@ -4,6 +4,7 @@ "use strict"; const defaultDomain = "(default)"; + const {packingLib, standards} = window.WEB_API_MANAGER; const generateStateObject = function (initialDomain, standards) { @@ -12,8 +13,21 @@ standards: standards, domainRules: {}, domainNames: [], + shouldLog: false, selectedStandards: [], + toStorage: function () { + return { + domainRules: this.domainRules, + shouldLog: this.shouldLog + }; + }, + + populateFromStorage: function (storedValues) { + this.setDomainRules(storedValues.domainRules); + this.setShouldLog(storedValues.shouldLog); + }, + setDomainRules: function (newDomainRules) { this.domainRules = newDomainRules; this.domainNames = Object.keys(newDomainRules); @@ -47,6 +61,10 @@ this.domainNames = Object.keys(this.domainRules); this.selectedDomain = newDomainRule; this.selectedStandards = this.domainRules[newDomainRule]; + }, + + setShouldLog: function (shouldLog) { + this.shouldLog = shouldLog; } }; diff --git a/content_scripts/src/instrument.js b/content_scripts/src/instrument.js index f4e10ba..fdf125b 100644 --- a/content_scripts/src/instrument.js +++ b/content_scripts/src/instrument.js @@ -8,18 +8,22 @@ const script = document.createElement('script'); const rootElm = document.head || document.documentElement; - const cookieKey = "web-api-manager"; + const standardsCookieKey = "wam-standards"; const {packingLib, standards} = window.WEB_API_MANAGER; const options = Object.keys(standards); - const packedValues = Cookies.get(cookieKey); + const packedValues = Cookies.get(standardsCookieKey); const standardsToBlock = packingLib.unpack(options, packedValues); - Cookies.remove(cookieKey); + Cookies.remove(standardsCookieKey); + + const shouldLogCookieKey = "wam-log"; + const shouldLog = Cookies.get(shouldLogCookieKey); + Cookies.remove(shouldLogCookieKey); const code = ` window.WEB_API_MANAGER_PAGE = { standards: ${JSON.stringify(standards)}, toBlock: ${JSON.stringify(standardsToBlock)}, - shouldLog: false + shouldLog: ${shouldLog} }; ###-INJECTED-PROXY-BLOCKING-CODE-### `; diff --git a/content_scripts/src/proxyblock.js b/content_scripts/src/proxyblock.js index 2ca7c97..8fd8769 100644 --- a/content_scripts/src/proxyblock.js +++ b/content_scripts/src/proxyblock.js @@ -10,6 +10,7 @@ const shouldLog = settings.shouldLog; const standardsToBlock = settings.toBlock; const standardDefinitions = settings.standards; + const hostName = window.location.hostname; const defaultFunction = function () {}; const funcPropNames = Object.getOwnPropertyNames(defaultFunction); @@ -59,9 +60,12 @@ let hasBeenLogged = false; const logKeyPath = function () { - if (keyPath !== undefined && hasBeenLogged === false) { + + if (keyPath !== undefined && + hasBeenLogged === false && + shouldLog) { hasBeenLogged = true; - console.info(keyPath); + console.log("Blocked '" + keyPath + "' on '" + hostName + "'"); } }; @@ -128,6 +132,7 @@ } try { + if (shouldLog === true) { parentRef[lastPropertyName] = createBlockingProxy(keyPath); return true; @@ -136,6 +141,7 @@ parentRef[lastPropertyName] = defaultBlockingProxy; return true; } catch (e) { + if (shouldLog) { console.log("Error instrumenting " + keyPath + ": " + e); } diff --git a/lib/storage.js b/lib/storage.js index e14a576..af38e3d 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -4,23 +4,29 @@ "use strict"; const rootObject = window.browser || window.chrome; - const webApiManagerKeySettingsKey = "webApiManagerDomainRules"; + const webApiManagerKeySettingsKey = "webApiManager"; const storageObject = rootObject.storage; const get = function (callback) { storageObject.local.get(webApiManagerKeySettingsKey, function (results) { - let loadedDomainRules = results && results[webApiManagerKeySettingsKey]; + let loadedValues = results && results[webApiManagerKeySettingsKey]; // If there are no currently saved domain rules, then create // a stubbed out one, using an empty blocking rule set. - if (!loadedDomainRules || Object.keys(loadedDomainRules).length === 0) { - loadedDomainRules = { - "(default)": [] + if (!loadedValues || + !loadedValues.domainRules || + Object.keys(loadedValues.domainRules).length === 0) { + + loadedValues = { + domainRules: { + "(default)": [] + }, + shouldLog: false }; } - callback(loadedDomainRules); + callback(loadedValues); }); };