Use a common webNavigation listener

Use the same listener for `webNavigation.onCommitted` and `webNavigation.onHistoryStateUpdated`.
This commit is contained in:
hideheader 2015-02-23 17:48:27 -05:00
parent b377be8a18
commit c6340e9617

View File

@ -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});
}
});
});