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

Unified Diff: include.preload.js

Issue 29893559: Issue 6999 - Generate style sheets in background page (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Sept. 27, 2018, 5:35 a.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 | « no previous file | lib/contentFiltering.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
===================================================================
--- a/include.preload.js
+++ b/include.preload.js
@@ -12,17 +12,16 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
-let {splitSelector} = require("./adblockpluscore/lib/common");
let {ElemHideEmulation} =
require("./adblockpluscore/lib/content/elemHideEmulation");
// This variable is also used by our other content scripts.
let contentFiltering;
const typeMap = new Map([
["img", "IMAGE"],
@@ -391,156 +390,32 @@
document.removeEventListener("DOMContentLoaded", this.trace);
this.observer.disconnect();
clearTimeout(this.timeout);
}
};
function ContentFiltering()
{
- this.shadow = this.createShadowTree();
- this.styles = new Map();
this.tracer = null;
- this.inline = true;
- this.inlineEmulated = true;
this.elemHideEmulation = new ElemHideEmulation(
this.addSelectors.bind(this),
this.hideElements.bind(this)
);
}
ContentFiltering.prototype = {
- selectorGroupSize: 1024,
-
- createShadowTree()
- {
- // Use Shadow DOM if available as to not mess with with web pages that
- // rely on the order of their own <style> tags (#309). However, creating
- // a shadow root breaks running CSS transitions. So we have to create
- // the shadow root before transistions might start (#452).
- if (!("createShadowRoot" in document.documentElement))
- return null;
-
- // Both Firefox and Chrome 66+ support user style sheets, so we can avoid
- // creating an unnecessary shadow root on these platforms.
- let match = /\bChrome\/(\d+)/.exec(navigator.userAgent);
- if (!match || match[1] >= 66)
- return null;
-
- // Using shadow DOM causes issues on some Google websites,
- // including Google Docs, Gmail and Blogger (#1770, #2602, #2687).
- if (/\.(?:google|blogger)\.com$/.test(document.domain))
- return null;
-
- // Finally since some users have both AdBlock and Adblock Plus installed we
- // have to consider how the two extensions interact. For example we want to
- // avoid creating the shadowRoot twice.
- let shadow = document.documentElement.shadowRoot ||
- document.documentElement.createShadowRoot();
- shadow.appendChild(document.createElement("content"));
-
- return shadow;
- },
-
- 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 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");
- (this.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;
-
- this.styles.set(groupName, 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.
- let preparedSelectors = [];
- if (this.shadow)
- {
- for (let selector of selectors)
- {
- let subSelectors = splitSelector(selector);
- for (let subSelector of subSelectors)
- preparedSelectors.push("::content " + subSelector);
- }
- }
- else
- {
- preparedSelectors = selectors;
- }
-
- // 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
- // size of 2). Since we don't know the sizes of the selectors here, we
- // simply split them into groups of 1,024, based on the reasonable
- // assumption that the average selector won't have a size greater than 8.
- // The alternative would be to calculate the sizes of the selectors and
- // divide them up accordingly, but this approach is more efficient and has
- // worked well in practice. In theory this could still lead to some
- // selectors not working on Chromium, but it is highly unlikely.
- // See issue #6298 and https://crbug.com/804179
- for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize)
- {
- let selector = preparedSelectors.slice(
- i, i + this.selectorGroupSize
- ).join(", ");
- style.sheet.insertRule(selector + "{display: none !important;}",
- style.sheet.cssRules.length);
- }
- },
-
addSelectors(selectors, filters, groupName = "emulated", appendOnly = false)
{
- if (this.inline || this.inlineEmulated)
- {
- // Insert the style rules inline if we have been instructed by the
- // background page to do so. This is usually the case, except on platforms
- // that do support user stylesheets via the browser.tabs.insertCSS API
- // (Firefox 53 onwards for now and possibly Chrome in the near future).
- // Once all supported platforms have implemented this API, we can remove
- // the code below. See issue #5090.
- // Related Chrome and Firefox issues:
- // https://bugs.chromium.org/p/chromium/issues/detail?id=632009
- // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
- this.addSelectorsInline(selectors, groupName, appendOnly);
- }
- else
- {
- browser.runtime.sendMessage({
- type: "elemhide.injectSelectors",
- selectors,
- groupName,
- appendOnly
- });
- }
+ browser.runtime.sendMessage({
+ type: "elemhide.injectSelectors",
+ selectors,
+ groupName,
+ appendOnly
+ });
// Only trace selectors that are based directly on hiding filters
// (i.e. leave out collapsing selectors).
if (this.tracer && groupName != "collapsing")
this.tracer.addSelectors(selectors, filters);
},
hideElements(elements, filters)
@@ -568,30 +443,19 @@
{
if (this.tracer)
this.tracer.disconnect();
this.tracer = null;
if (response.trace)
this.tracer = new ElementHidingTracer();
- this.inline = response.inline;
- this.inlineEmulated = !!response.inlineEmulated;
-
- if (this.inline)
- this.addSelectorsInline(response.selectors, "standard");
-
if (this.tracer)
this.tracer.addSelectors(response.selectors);
- // Prefer CSS selectors for -abp-has and -abp-contains unless the
- // background page has asked us to use inline styles.
- this.elemHideEmulation.useInlineStyles = this.inline ||
- this.inlineEmulated;
-
this.elemHideEmulation.apply(response.emulatedPatterns);
});
}
};
if (document instanceof HTMLDocument)
{
checkSitekey();
« no previous file with comments | « no previous file | lib/contentFiltering.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld