| Index: include.preload.js |
| =================================================================== |
| --- a/include.preload.js |
| +++ b/include.preload.js |
| @@ -403,43 +403,68 @@ |
| this.elemHideEmulation = new ElemHideEmulation( |
| () => {}, |
| this.hideElements.bind(this) |
| ); |
| } |
| ContentFiltering.prototype = { |
| selectorGroupSize: 1024, |
| + createStyleElement() |
| + { |
| + // Create <style> element lazily, only if we add styles. Add it 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. |
| + let style = document.createElement("style"); |
| + (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 may stay null, after adding the |
| + // <style> element. |
| + if (!style.sheet) |
| + return null; |
| + |
| + return style; |
| + }, |
| + |
| + addStyleSheet(styleSheet, groupName) |
| + { |
| + let style = this.styles.get(groupName); |
| + |
| + if (!style) |
| + { |
| + style = this.createStyleElement(); |
| + if (!style) |
| + return; |
| + |
| + this.styles.set(groupName, style); |
| + } |
| + |
| + style.textContent = styleSheet; |
| + }, |
| + |
| addSelectorsInline(selectors, groupName, appendOnly = false) |
| { |
| let style = this.styles.get(groupName); |
| if (style && !appendOnly) |
| { |
| while (style.sheet.cssRules.length > 0) |
| style.sheet.deleteRule(0); |
| } |
| if (selectors.length == 0) |
| return; |
| if (!style) |
| { |
| - // Create <style> element lazily, only if we add styles. Add it 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"); |
| - (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 may stay null, after adding the |
| - // <style> element. |
| - if (!style.sheet) |
| + style = this.createStyleElement(); |
| + if (!style) |
| return; |
| this.styles.set(groupName, style); |
| } |
| // Chromium's Blink engine supports only up to 8,192 simple selectors, and |
| // even fewer compound selectors, in a rule. The exact number of selectors |
| // that would work depends on their sizes (e.g. "#foo .bar" has a |
| @@ -518,17 +543,17 @@ |
| this.tracer = null; |
| if (response.trace) |
| this.tracer = new ElementHidingTracer(); |
| this.inline = response.inline; |
| if (this.inline) |
| - this.addSelectorsInline(response.selectors, "standard"); |
| + this.addStyleSheet(response.styleSheet, "standard"); |
| if (this.tracer) |
| this.tracer.addSelectors(response.selectors); |
| this.elemHideEmulation.apply(response.emulatedPatterns); |
| }); |
| } |
| }; |