From bde8ac3e8e0c8f53630d47923d8e183123153458 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Tue, 28 Aug 2018 21:09:03 +0200 Subject: [PATCH] Only use onHeadersReceived listener when necessary As mentioned in #214 --- lib/dataUrls.js | 61 +++++++++++++++++++++++++++++--------------- test/dataUrlTest.php | 4 +-- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/dataUrls.js b/lib/dataUrls.js index 0f9b11c..55eb7d4 100644 --- a/lib/dataUrls.js +++ b/lib/dataUrls.js @@ -41,28 +41,47 @@ } scope.init = function(){ - browser.webRequest.onHeadersReceived.addListener( - function(details){ + function listener(details){console.log("listener"); + const headers = details.responseHeaders; + if (settings.blockDataURLs){ const cspMatch = (blockBlob? "": "blob: ") + "filesystem: *"; - const headers = details.responseHeaders; - if (settings.blockDataURLs){ - logging.verbose("Adding CSP header to", details); - setHeader(headers, { - name: "Content-Security-Policy", - value: `object-src ${cspMatch}; frame-src ${cspMatch}` - // + "; report-to https://canvasblocker.invalid/; report-uri https://canvasblocker.invalid/" - }); - } - return { - responseHeaders: headers - }; - }, - { - urls: [""], - types: ["main_frame", "sub_frame", "object"] - }, - ["blocking", "responseHeaders"] - ); + logging.verbose("Adding CSP header to", details); + setHeader(headers, { + name: "Content-Security-Policy", + value: `object-src ${cspMatch}; frame-src ${cspMatch}` + // + "; report-to https://canvasblocker.invalid/; report-uri https://canvasblocker.invalid/" + }); + } + return { + responseHeaders: headers + }; + } + function addListener(){ + if (!browser.webRequest.onHeadersReceived.hasListener(listener)){ + browser.webRequest.onHeadersReceived.addListener( + listener, + { + urls: [""], + types: ["main_frame", "sub_frame", "object"] + }, + ["blocking", "responseHeaders"] + ); + } + } + function removeListener(){ + browser.webRequest.onHeadersReceived.removeListener(listener); + } + function adjustListener(){ + if (settings.blockDataURLs){ + addListener(); + } + else { + removeListener(); + } + } + + settings.onloaded(adjustListener); + settings.on("blockDataURLs", adjustListener); }; }()); \ No newline at end of file diff --git a/test/dataUrlTest.php b/test/dataUrlTest.php index e83ae2a..2771b36 100644 --- a/test/dataUrlTest.php +++ b/test/dataUrlTest.php @@ -29,7 +29,7 @@

Data-URL object