Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: include.preload.js

Issue 29331619: Issue 2397 - Integrate CSS property filters into adblockpluschrome (Closed)
Patch Set: Adjust code now that CSSPropertyFilters is a class Created Nov. 30, 2015, 5:37 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « dependencies ('k') | metadata.common » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
diff --git a/include.preload.js b/include.preload.js
index 7030adc57c6e779603a247c2ca43f2037cb27dbe..f07382ac0c517b9ad2e2531fbe7aa4430b33c260 100644
--- a/include.preload.js
+++ b/include.preload.js
@@ -300,58 +300,81 @@ function init(document)
shadow.appendChild(document.createElement("shadow"));
}
- var updateStylesheet = function(reinject)
+ function addElemHideSelectors(selectors)
{
- ext.backgroundPage.sendMessage({type: "get-selectors"}, function(selectors)
+ if (selectors.length == 0)
+ return;
+
+ if (!style)
+ {
+ // Create <style> element lazily, only if we add styles. Add it to
+ // the shadow DOM if possible. Otherwise fallback to the <head> or
+ // <html> element. If we have injected a style element before that
+ // has been removed (the sheet property is null), create a new one.
+ style = document.createElement("style");
+ (shadow || document.head || document.documentElement).appendChild(style);
+
+ // It can happen that the frame already navigated to a different
+ // document while we were waiting for the background page to respond.
+ // In that case the sheet property will stay null, after addind the
+ // <style> element to the shadow DOM.
+ if (!style.sheet)
+ return;
+
+ observer = reinjectRulesWhenRemoved(document, style);
+ }
+
+ // If using shadow DOM, we have to add the ::content pseudo-element
+ // before each selector, in order to match elements within the
+ // insertion point.
+ if (shadow)
+ selectors = convertSelectorsForShadowDOM(selectors);
+
+ // WebKit (and Blink?) apparently chokes when the selector list in a
+ // CSS rule is huge. So we split the elemhide selectors into groups.
+ while (selectors.length > 0)
{
+ var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", ");
+ style.sheet.addRule(selector, "display: none !important;");
+ }
+ };
+
+ var updateStylesheet = function(propertyFilters)
kzar 2015/11/30 17:57:32 I deliberately added a typo in the propertFilters
kzar 2015/12/01 10:46:04 OK I think I understand now, this function is only
+ {
+ var selectors = null;
+ var CSSPropertyFiltersLoaded = false;
+
+ var checkLoaded = function()
+ {
+ if (!selectors || !CSSPropertyFiltersLoaded)
+ return;
+
if (observer)
- {
observer.disconnect();
- observer = null;
- }
+ observer = null;
if (style && style.parentElement)
- {
style.parentElement.removeChild(style);
- style = null;
- }
+ style = null;
- if (selectors.length > 0)
- {
- // Create <style> element lazily, only if we add styles. Add it to
- // the shadow DOM if possible. Otherwise fallback to the <head> or
- // <html> element. If we have injected a style element before that
- // has been removed (the sheet property is null), create a new one.
- style = document.createElement("style");
- (shadow || document.head || document.documentElement).appendChild(style);
-
- // It can happen that the frame already navigated to a different
- // document while we were waiting for the background page to respond.
- // In that case the sheet property will stay null, after adding the
- // <style> element to the shadow DOM.
- if (style.sheet)
- {
- // If using shadow DOM, we have to add the ::content pseudo-element
- // before each selector, in order to match elements within the
- // insertion point.
- if (shadow)
- selectors = convertSelectorsForShadowDOM(selectors);
-
- // WebKit (and Blink?) apparently chokes when the selector list in a
- // CSS rule is huge. So we split the elemhide selectors into groups.
- for (var i = 0; selectors.length > 0; i++)
- {
- var selector = selectors.splice(0, SELECTOR_GROUP_SIZE).join(", ");
- style.sheet.insertRule(selector + " { display: none !important; }", i);
- }
- }
+ addElemHideSelectors(selectors);
+ propertyFilters.apply();
+ };
- observer = reinjectRulesWhenRemoved(document, style);
- }
+ ext.backgroundPage.sendMessage({type: "get-selectors"}, function(response)
+ {
+ selectors = response;
+ checkLoaded();
+ });
+
+ propertyFilters.load(function()
+ {
+ CSSPropertyFiltersLoaded = true;
+ checkLoaded();
});
};
- updateStylesheet();
+ ext.backgroundPage.sendMessage({type: "get-selectors"}, addElemHideSelectors);
Sebastian Noack 2015/12/10 10:30:49 Any reason we use different logic on initilization
kzar 2015/12/10 15:16:20 No reason that I know of, Done.
document.addEventListener("error", function(event)
{
@@ -391,11 +414,16 @@ function init(document)
}
}, true);
- return updateStylesheet;
+ if (window.document == document)
Sebastian Noack 2015/12/10 10:30:49 Why don't you just put this code below, outside th
kzar 2015/12/10 15:16:20 Done.
+ {
+ window.updateStylesheet = updateStylesheet.bind(
+ null, new CSSPropertyFilters(window, addElemHideSelectors)
+ );
+ }
}
if (document instanceof HTMLDocument)
{
checkSitekey();
- window.updateStylesheet = init(document);
+ init(document);
}
« no previous file with comments | « dependencies ('k') | metadata.common » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld