From 73e55d0f5f635880c2e0ed157dfcac92365208bd Mon Sep 17 00:00:00 2001 From: Charly Lacoste Date: Tue, 14 Apr 2020 15:02:11 +0200 Subject: [PATCH 1/4] [FIX]Ignore cache requests & no track incognito host Issue #15 Merge of Jolg42's work with recent evolution for Chrome + add testing. --- script.js | 13 ++++++++++--- test/test-script.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/script.js b/script.js index a37020c3..870dce83 100644 --- a/script.js +++ b/script.js @@ -24,8 +24,14 @@ isChrome = () => { }; headersReceivedListener = (requestDetails) => { + // Do not count bytes from requests from local cache + if (requestDetails.fromCache) return + + let CONST_INCOGNITO = 'Incognito'; + if (isChrome()) { - const origin = extractHostname(!requestDetails.initiator ? requestDetails.url : requestDetails.initiator); + // If Incognito request we do not track the domain origin + const origin = (requestDetails.incognito ? CONST_INCOGNITO : extractHostname(!requestDetails.initiator ? requestDetails.url : requestDetails.initiator)); const responseHeadersContentLength = requestDetails.responseHeaders.find(element => element.name.toLowerCase() === "content-length"); const contentLength = undefined === responseHeadersContentLength ? {value: 0} : responseHeadersContentLength; @@ -38,7 +44,8 @@ headersReceivedListener = (requestDetails) => { let filter = browser.webRequest.filterResponseData(requestDetails.requestId); filter.ondata = event => { - const origin = extractHostname(!requestDetails.originUrl ? requestDetails.url : requestDetails.originUrl); + // If Incognito request we do not track the domain origin + const origin = (requestDetails.incognito ? CONST_INCOGNITO : extractHostname(!requestDetails.originUrl ? requestDetails.url : requestDetails.originUrl)); setByteLengthPerOrigin(origin, event.data.byteLength); filter.write(event.data); @@ -51,7 +58,7 @@ headersReceivedListener = (requestDetails) => { return {}; }; -setBrowserIcon = (type) => { +const setBrowserIcon = (type) => { chrome.browserAction.setIcon({path: `icons/icon-${type}-48.png`}); }; diff --git a/test/test-script.js b/test/test-script.js index a3c69197..d5a0aa34 100644 --- a/test/test-script.js +++ b/test/test-script.js @@ -156,6 +156,7 @@ describe('headersReceivedListener', function () { let requestDetails = {}; // backup for spied methods const DOMAIN_NAME = 'http://www.spotify.com'; + const INCOGNITO_CONST = 'Test'; const extractHostNameBackup = extractHostname; const setByteLengthPerOriginBackup = setByteLengthPerOrigin; @@ -210,6 +211,7 @@ describe('headersReceivedListener', function () { done(); }); + it('should call extractHostname with Initiator when it is provided from parameter (Chrome Browser behavior)', function (done) { extractHostname = chai.spy(); @@ -221,6 +223,31 @@ describe('headersReceivedListener', function () { done(); }); + it('should use incognito as Origin when Incognito is provided from parameter (Chrome Browser behavior)', function (done) { + extractHostname = chai.spy(); + setByteLengthPerOrigin = chai.spy(); + + requestDetails.incognito = INCOGNITO_CONST; + + headersReceivedListener(requestDetails); + + expect(extractHostname).to.not.have.been.called(); + expect(setByteLengthPerOrigin).to.have.been.called.with('Incognito'); + done(); + }); + + it('should use incognito as Origin when Incognito is provided from parameter (Mozilla Firefox Browser behavior)', function (done) { + extractHostname = chai.spy(); + setByteLengthPerOrigin = chai.spy(); + + requestDetails.incognito = INCOGNITO_CONST; + headersReceivedListener(requestDetails); + + expect(extractHostname).to.not.have.been.called(); + expect(setByteLengthPerOrigin).to.have.been.called.with('Incognito'); + done(); + }); + it('should call extractHostname with url when neither Initiator nor originUrl is not provided from from parameter', function (done) { extractHostname = chai.spy(); @@ -253,4 +280,15 @@ describe('headersReceivedListener', function () { expect(setByteLengthPerOrigin).to.have.been.called.with('www.spotify.com', 0); done(); }); + + it('should not call setByteLengthPerOrigin when fromCache is defined', function(done){ + setByteLengthPerOrigin = chai.spy(); + + requestDetails.fromCache = "test"; + requestDetails.responseHeaders = []; + headersReceivedListener(requestDetails); + + expect(setByteLengthPerOrigin).to.not.have.been.called(); + done(); + }); }); \ No newline at end of file From f2ddff3786a4a79b972e56d586883950a7b935af Mon Sep 17 00:00:00 2001 From: Guignol <38350760+cguignol@users.noreply.github.com> Date: Thu, 16 Apr 2020 16:20:15 +0200 Subject: [PATCH 2/4] Clean Code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Joël Galeran --- script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.js b/script.js index 870dce83..a7ce1950 100644 --- a/script.js +++ b/script.js @@ -45,7 +45,7 @@ headersReceivedListener = (requestDetails) => { filter.ondata = event => { // If Incognito request we do not track the domain origin - const origin = (requestDetails.incognito ? CONST_INCOGNITO : extractHostname(!requestDetails.originUrl ? requestDetails.url : requestDetails.originUrl)); + const origin = (requestDetails.incognito ? CONST_INCOGNITO : extractHostname(requestDetails.originUrl ? requestDetails.originUrl : requestDetails.url)); setByteLengthPerOrigin(origin, event.data.byteLength); filter.write(event.data); From 0ab11659c59940f386e74fb70f3bb28e0e0b725f Mon Sep 17 00:00:00 2001 From: Guignol <38350760+cguignol@users.noreply.github.com> Date: Thu, 16 Apr 2020 16:20:27 +0200 Subject: [PATCH 3/4] Clean Code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Joël Galeran --- script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.js b/script.js index a7ce1950..1b93816a 100644 --- a/script.js +++ b/script.js @@ -31,7 +31,7 @@ headersReceivedListener = (requestDetails) => { if (isChrome()) { // If Incognito request we do not track the domain origin - const origin = (requestDetails.incognito ? CONST_INCOGNITO : extractHostname(!requestDetails.initiator ? requestDetails.url : requestDetails.initiator)); + const origin = (requestDetails.incognito ? CONST_INCOGNITO : extractHostname(requestDetails.initiator ? requestDetails.initiator : requestDetails.url)); const responseHeadersContentLength = requestDetails.responseHeaders.find(element => element.name.toLowerCase() === "content-length"); const contentLength = undefined === responseHeadersContentLength ? {value: 0} : responseHeadersContentLength; From d961ae66da5583e2465650d2bb51f27e3be032e8 Mon Sep 17 00:00:00 2001 From: Guignol <38350760+cguignol@users.noreply.github.com> Date: Thu, 16 Apr 2020 16:20:38 +0200 Subject: [PATCH 4/4] Clean Code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Joël Galeran --- script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.js b/script.js index 1b93816a..fd3b0b14 100644 --- a/script.js +++ b/script.js @@ -27,7 +27,7 @@ headersReceivedListener = (requestDetails) => { // Do not count bytes from requests from local cache if (requestDetails.fromCache) return - let CONST_INCOGNITO = 'Incognito'; + const CONST_INCOGNITO = 'Incognito'; if (isChrome()) { // If Incognito request we do not track the domain origin