From 3ddf81263505392f279226f24d3120ebf53cc419 Mon Sep 17 00:00:00 2001 From: tophf Date: Thu, 27 Jul 2017 17:37:05 +0300 Subject: [PATCH] Don't count styles on NTP in Chrome 61+, see #122 --- background/background.js | 44 ++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/background/background.js b/background/background.js index 8899a1f4..2bb57e80 100644 --- a/background/background.js +++ b/background/background.js @@ -14,17 +14,24 @@ dbExec().catch((...args) => { // register all listeners chrome.runtime.onMessage.addListener(onRuntimeMessage); -chrome.webNavigation.onBeforeNavigate.addListener(data => - webNavigationListener(null, data)); +{ + const listener = + URLS.chromeProtectsNTP + ? webNavigationListenerChrome + : webNavigationListener; -chrome.webNavigation.onCommitted.addListener(data => - webNavigationListener('styleApply', data)); + chrome.webNavigation.onBeforeNavigate.addListener(data => + listener(null, data)); -chrome.webNavigation.onHistoryStateUpdated.addListener(data => - webNavigationListener('styleReplaceAll', data)); + chrome.webNavigation.onCommitted.addListener(data => + listener('styleApply', data)); -chrome.webNavigation.onReferenceFragmentUpdated.addListener(data => - webNavigationListener('styleReplaceAll', data)); + chrome.webNavigation.onHistoryStateUpdated.addListener(data => + listener('styleReplaceAll', data)); + + chrome.webNavigation.onReferenceFragmentUpdated.addListener(data => + listener('styleReplaceAll', data)); +} chrome.tabs.onAttached.addListener(tabId => { // When an edit page gets attached or detached, remember its state @@ -236,10 +243,31 @@ function webNavigationListener(method, {url, tabId, frameId}) { } +function webNavigationListenerChrome(method, data) { + // Chrome 61.0.3161+ doesn't run content scripts on NTP + if ( + !data.url.startsWith('https://www.google.') || + !data.url.includes('/_/chrome/newtab?') + ) { + webNavigationListener(method, data); + return; + } + getTab(data.tabId).then(tab => { + if (tab.url === 'chrome://newtab/') { + data.url = tab.url; + } + webNavigationListener(method, data); + }); +} + + function updateIcon(tab, styles) { if (tab.id < 0) { return; } + if (URLS.chromeProtectsNTP && tab.url === 'chrome://newtab/') { + styles = {}; + } if (styles) { stylesReceived(styles); return;