From c67c9d3f54b5cc7a0cccedbc79f865336f7fb390 Mon Sep 17 00:00:00 2001 From: hideheader Date: Mon, 23 Feb 2015 00:14:22 -0500 Subject: [PATCH 1/3] New message `styleReplaceAll` `styleReplaceAll` removes all existing user styles, then applies the styles in the message payload. --- apply.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/apply.js b/apply.js index cba52c65..cd8bf82c 100644 --- a/apply.js +++ b/apply.js @@ -17,6 +17,10 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { for (var styleId in request.styles) { applySections(styleId, request.styles[styleId]); } + break; + case "styleReplaceAll": + replaceAll(request.styles); + break; } }); @@ -54,3 +58,10 @@ function applySections(styleId, sections) { }).join("\n"))); document.documentElement.appendChild(styleElement); } + +function replaceAll(newStyles) { + Array.prototype.forEach.call(document.querySelectorAll("STYLE.stylish"), function(style) { + style.parentNode.removeChild(style); + }); + applyStyles(newStyles); +} From b377be8a18f4be84bfaa64ec3c874ef0f627e3c5 Mon Sep 17 00:00:00 2001 From: hideheader Date: Mon, 23 Feb 2015 00:25:24 -0500 Subject: [PATCH 2/3] Reload styles on `history.pushState` --- background.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/background.js b/background.js index 1f204e63..6d07794e 100644 --- a/background.js +++ b/background.js @@ -319,3 +319,20 @@ chrome.tabs.onAttached.addListener(function(tabId, data) { } }); }); + +chrome.webNavigation.onHistoryStateUpdated.addListener(function (details) { + var now = new Date(); + console.log("%conHistoryStateUpdate %s: %s(%s)\n url=%s", "color:olive", now.toTimeString().substr(0,8), + details.transitionType, details.transitionQualifiers.join(" "), details.url); + var request = { + enabled: true, + matchUrl: details.url, + asHash: true + }; + getStyles(request, function(r) { + chrome.tabs.sendMessage(details.tabId, {method: "styleReplaceAll", styles: r}); + if (details.frameId === 0 && localStorage["show-badge"] === "true") { + chrome.browserAction.setBadgeText({text: getBadgeText(Object.keys(r)), tabId: details.tabId}); + } + }); +}); From c6340e9617b9352045ba3c00c24135181b665cb7 Mon Sep 17 00:00:00 2001 From: hideheader Date: Mon, 23 Feb 2015 17:48:27 -0500 Subject: [PATCH 3/3] Use a common `webNavigation` listener Use the same listener for `webNavigation.onCommitted` and `webNavigation.onHistoryStateUpdated`. --- background.js | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/background.js b/background.js index 6d07794e..8e554c21 100644 --- a/background.js +++ b/background.js @@ -1,6 +1,8 @@ // This happens right away, sometimes so fast that the content script isn't even ready. That's // why the content script also asks for this stuff. -chrome.webNavigation.onCommitted.addListener(function(data) { +chrome.webNavigation.onCommitted.addListener(webNavigationListener.bind(this, "styleApply")); +chrome.webNavigation.onHistoryStateUpdated.addListener(webNavigationListener.bind(this, "styleReplaceAll")); +function webNavigationListener(method, data) { // Until Chrome 41, we can't target a frame with a message // (https://developer.chrome.com/extensions/tabs#method-sendMessage) // so a style affecting a page with an iframe will affect the main page as well. @@ -9,13 +11,13 @@ chrome.webNavigation.onCommitted.addListener(function(data) { return; } getStyles({matchUrl: data.url, enabled: true, asHash: true}, function(styleHash) { - chrome.tabs.sendMessage(data.tabId, {method: "styleApply", styles: styleHash}); + chrome.tabs.sendMessage(data.tabId, {method: method, styles: styleHash}); // Don't show the badge for frames if (data.frameId == 0) { chrome.browserAction.setBadgeText({text: getBadgeText(Object.keys(styleHash)), tabId: data.tabId}); } }); -}); +} chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { switch (request.method) { @@ -319,20 +321,3 @@ chrome.tabs.onAttached.addListener(function(tabId, data) { } }); }); - -chrome.webNavigation.onHistoryStateUpdated.addListener(function (details) { - var now = new Date(); - console.log("%conHistoryStateUpdate %s: %s(%s)\n url=%s", "color:olive", now.toTimeString().substr(0,8), - details.transitionType, details.transitionQualifiers.join(" "), details.url); - var request = { - enabled: true, - matchUrl: details.url, - asHash: true - }; - getStyles(request, function(r) { - chrome.tabs.sendMessage(details.tabId, {method: "styleReplaceAll", styles: r}); - if (details.frameId === 0 && localStorage["show-badge"] === "true") { - chrome.browserAction.setBadgeText({text: getBadgeText(Object.keys(r)), tabId: details.tabId}); - } - }); -});