Merge pull request #40 from hideheader/history-observer

Reload styles on `history.pushState`
This commit is contained in:
Jason Barnabe 2015-02-23 22:16:22 -06:00
commit c1a8872dad
2 changed files with 16 additions and 3 deletions

View File

@ -17,6 +17,10 @@ chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
for (var styleId in request.styles) { for (var styleId in request.styles) {
applySections(styleId, request.styles[styleId]); applySections(styleId, request.styles[styleId]);
} }
break;
case "styleReplaceAll":
replaceAll(request.styles);
break;
} }
}); });
@ -54,3 +58,10 @@ function applySections(styleId, sections) {
}).join("\n"))); }).join("\n")));
document.documentElement.appendChild(styleElement); document.documentElement.appendChild(styleElement);
} }
function replaceAll(newStyles) {
Array.prototype.forEach.call(document.querySelectorAll("STYLE.stylish"), function(style) {
style.parentNode.removeChild(style);
});
applyStyles(newStyles);
}

View File

@ -1,6 +1,8 @@
// This happens right away, sometimes so fast that the content script isn't even ready. That's // 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. // 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 // Until Chrome 41, we can't target a frame with a message
// (https://developer.chrome.com/extensions/tabs#method-sendMessage) // (https://developer.chrome.com/extensions/tabs#method-sendMessage)
// so a style affecting a page with an iframe will affect the main page as well. // 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; return;
} }
getStyles({matchUrl: data.url, enabled: true, asHash: true}, function(styleHash) { 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 // Don't show the badge for frames
if (data.frameId == 0) { if (data.frameId == 0) {
chrome.browserAction.setBadgeText({text: getBadgeText(Object.keys(styleHash)), tabId: data.tabId}); chrome.browserAction.setBadgeText({text: getBadgeText(Object.keys(styleHash)), tabId: data.tabId});
} }
}); });
}); }
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.method) { switch (request.method) {