From 7614d99dbfa2b6de772bbfe967e95f4ab3d33788 Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Mon, 10 Sep 2018 13:43:50 +0200 Subject: [PATCH] add header to outgoing requests; closes #8 --- chromelogger.js | 15 ++++++++++++++- manifest.json | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/chromelogger.js b/chromelogger.js index d052570..c516db9 100644 --- a/chromelogger.js +++ b/chromelogger.js @@ -86,6 +86,10 @@ _deactivateTitle(tabId); } + function _tabIsActive(tabId) { + return tabsWithExtensionEnabled.indexOf(tabId) !== -1; + } + function _activateTitle(tabId) { chrome.browserAction.getTitle({tabId: tabId}, function(title) { chrome.browserAction.setTitle({ @@ -185,8 +189,17 @@ chrome.tabs.onCreated.addListener(_handleTabEvent); chrome.tabs.onUpdated.addListener(_handleTabUpdated); + chrome.webRequest.onBeforeSendHeaders.addListener(function(details) { + if (_tabIsActive(details.tabId)) { + // Add a header that enables the server-side to detect when ChromeLogger is enabled: + details.requestHeaders.push({ name: "X-Chromelogger", value: "1" }); + } + + return {requestHeaders: details.requestHeaders}; + }, {urls: [""]}, ["blocking", "requestHeaders"]); + chrome.webRequest.onResponseStarted.addListener(function(details) { - if (tabsWithExtensionEnabled.indexOf(details.tabId) !== -1) { + if (_tabIsActive(details.tabId)) { chrome.tabs.sendMessage(details.tabId, {name: "header_update", details: details}, function(response) { // Previously this would only queue up the header updates // if the content script did not send back a response. This diff --git a/manifest.json b/manifest.json index c768e58..b763980 100644 --- a/manifest.json +++ b/manifest.json @@ -30,6 +30,7 @@ "permissions": [ "webRequest", + "webRequestBlocking", "tabs", "http://*/*", "https://*/*"