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); +} diff --git a/background.js b/background.js index 1f204e63..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) {