Index: lib/cssInjection.js |
=================================================================== |
--- a/lib/cssInjection.js |
+++ b/lib/cssInjection.js |
@@ -152,57 +152,88 @@ |
// style sheet now. |
if (oldStyleSheet && oldStyleSheet != styleSheet) |
removeStyleSheet(tabId, frameId, oldStyleSheet); |
frame.injectedStyleSheets.set(groupName, styleSheet); |
return true; |
} |
-port.on("elemhide.getSelectors", (message, sender) => |
+function doElementHiding(page, frame) |
{ |
let selectors = []; |
let emulatedPatterns = []; |
- let trace = devtools && devtools.hasPanel(sender.page); |
+ let trace = devtools && devtools.hasPanel(page); |
let inline = !userStyleSheetsSupported; |
- if (!checkWhitelisted(sender.page, sender.frame, |
+ if (!checkWhitelisted(page, frame, |
RegExpFilter.typeMap.DOCUMENT | |
RegExpFilter.typeMap.ELEMHIDE)) |
{ |
- let hostname = extractHostFromFrame(sender.frame); |
- let specificOnly = checkWhitelisted(sender.page, sender.frame, |
+ let hostname = extractHostFromFrame(frame); |
+ let specificOnly = checkWhitelisted(page, frame, |
RegExpFilter.typeMap.GENERICHIDE); |
selectors = ElemHide.getSelectorsForDomain( |
hostname, |
specificOnly ? ElemHide.SPECIFIC_ONLY : ElemHide.ALL_MATCHING |
); |
for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) |
emulatedPatterns.push({selector: filter.selector, text: filter.text}); |
} |
- if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, |
+ if (!inline && !updateFrameStyles(page.id, frame.id, |
selectors, "standard")) |
{ |
inline = true; |
} |
- let response = {trace, inline, emulatedPatterns}; |
+ let message = {type: "elemhide.apply", trace, inline, emulatedPatterns}; |
if (trace || inline) |
- response.selectors = selectors; |
+ message.selectors = selectors; |
// If we can't remove user style sheets using tabs.removeCSS, we'll only keep |
// adding them, which could cause problems with emulation filters as |
// described in issue #5864. Instead, we can just ask the content script to |
// add styles for emulation filters inline. |
if (!styleSheetRemovalSupported) |
- response.inlineEmulated = true; |
+ message.inlineEmulated = true; |
+ |
+ if (!message.selectors && message.emulatedPatterns.length == 0) |
+ return; |
+ |
+ // The content script is loaded at about the same time as we get the |
+ // webNavigation.onCommitted event; sometimes we have to retry a couple of |
+ // times before we get through. |
+ browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) |
+ .catch(() => |
+ browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) |
+ ) |
+ .catch(() => |
+ browser.tabs.sendMessage(page.id, message, {frameId: frame.id}) |
+ ); |
+} |
- return response; |
+browser.webNavigation.onCommitted.addListener(details => |
+{ |
+ if (!/^(https?:\/\/|about:blank\b|about:srcdoc\b)/.test(details.url)) |
+ return; |
+ |
+ let page = new ext.Page({id: details.tabId, url: details.url}); |
+ let frame = ext.getFrame(details.tabId, details.frameId); |
+ |
+ if (!frame) |
+ return; |
+ |
+ doElementHiding(page, frame); |
+}); |
+ |
+port.on("elemhide.needApply", (message, sender) => |
+{ |
+ doElementHiding(sender.page, sender.frame); |
}); |
port.on("elemhide.injectSelectors", (message, sender) => |
{ |
updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, |
message.groupName); |
}); |