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

Side by Side Diff: include.preload.js

Issue 29893559: Issue 6999 - Generate style sheets in background page (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Remove individual rule injection Created Sept. 28, 2018, 8:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/contentFiltering.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 }, 218 },
219 collapse => 219 collapse =>
220 { 220 {
221 if (collapse) 221 if (collapse)
222 { 222 {
223 if (selector) 223 if (selector)
224 { 224 {
225 if (!collapsingSelectors.has(selector)) 225 if (!collapsingSelectors.has(selector))
226 { 226 {
227 collapsingSelectors.add(selector); 227 collapsingSelectors.add(selector);
228 contentFiltering.addSelectors([selector], null, "collapsing", true); 228 contentFiltering.addSelectors([selector], "collapsing", true);
229 } 229 }
230 } 230 }
231 else 231 else
232 { 232 {
233 hideElement(element); 233 hideElement(element);
234 } 234 }
235 } 235 }
236 } 236 }
237 ); 237 );
238 } 238 }
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 this.styles = new Map(); 398 this.styles = new Map();
399 this.tracer = null; 399 this.tracer = null;
400 this.inline = true; 400 this.inline = true;
401 401
402 this.elemHideEmulation = new ElemHideEmulation( 402 this.elemHideEmulation = new ElemHideEmulation(
403 () => {}, 403 () => {},
404 this.hideElements.bind(this) 404 this.hideElements.bind(this)
405 ); 405 );
406 } 406 }
407 ContentFiltering.prototype = { 407 ContentFiltering.prototype = {
408 selectorGroupSize: 1024, 408 addStyleSheetInline(styleSheet, groupName = "standard", appendOnly = false)
409
410 addSelectorsInline(selectors, groupName, appendOnly = false)
411 { 409 {
412 let style = this.styles.get(groupName); 410 let style = this.styles.get(groupName);
413 411
414 if (style && !appendOnly)
415 {
416 while (style.sheet.cssRules.length > 0)
417 style.sheet.deleteRule(0);
418 }
419
420 if (selectors.length == 0)
421 return;
422
423 if (!style) 412 if (!style)
424 { 413 {
425 // Create <style> element lazily, only if we add styles. Add it to 414 // Create <style> element lazily, only if we add styles. Add it to
426 // the <head> or <html> element. If we have injected a style element 415 // the <head> or <html> element. If we have injected a style element
427 // before that has been removed (the sheet property is null), create a 416 // before that has been removed (the sheet property is null), create a
428 // new one. 417 // new one.
429 style = document.createElement("style"); 418 style = document.createElement("style");
430 (document.head || document.documentElement).appendChild(style); 419 (document.head || document.documentElement).appendChild(style);
431 420
432 // It can happen that the frame already navigated to a different 421 // It can happen that the frame already navigated to a different
433 // document while we were waiting for the background page to respond. 422 // document while we were waiting for the background page to respond.
434 // In that case the sheet property may stay null, after adding the 423 // In that case the sheet property may stay null, after adding the
435 // <style> element. 424 // <style> element.
436 if (!style.sheet) 425 if (!style.sheet)
437 return; 426 return;
438 427
439 this.styles.set(groupName, style); 428 this.styles.set(groupName, style);
440 } 429 }
441 430
442 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 431 if (appendOnly)
443 // even fewer compound selectors, in a rule. The exact number of selectors 432 style.textContent += styleSheet;
444 // that would work depends on their sizes (e.g. "#foo .bar" has a 433 else
445 // size of 2). Since we don't know the sizes of the selectors here, we 434 style.textContent = styleSheet;
446 // simply split them into groups of 1,024, based on the reasonable
447 // assumption that the average selector won't have a size greater than 8.
448 // The alternative would be to calculate the sizes of the selectors and
449 // divide them up accordingly, but this approach is more efficient and has
450 // worked well in practice. In theory this could still lead to some
451 // selectors not working on Chromium, but it is highly unlikely.
452 // See issue #6298 and https://crbug.com/804179
453 for (let i = 0; i < selectors.length; i += this.selectorGroupSize)
454 {
455 let selector = selectors.slice(i, i + this.selectorGroupSize).join(", ");
456 style.sheet.insertRule(selector + "{display: none !important;}",
457 style.sheet.cssRules.length);
458 }
459 }, 435 },
460 436
461 addSelectors(selectors, filters, groupName = "emulated", appendOnly = false) 437 addSelectors(selectors, groupName = "standard", appendOnly = false)
462 { 438 {
463 if (this.inline) 439 if (this.inline)
464 { 440 {
465 // Insert the style rules inline if we have been instructed by the 441 // Insert the style rules inline if we have been instructed by the
466 // background page to do so. This is usually the case, except on platforms 442 // background page to do so. This is rarely the case, except on platforms
467 // that do support user stylesheets via the browser.tabs.insertCSS API 443 // that do not support user stylesheets via the browser.tabs.insertCSS API
468 // (Firefox 53 onwards for now and possibly Chrome in the near future). 444 // (Firefox <53, Chrome <66, and Edge).
469 // Once all supported platforms have implemented this API, we can remove 445 // Once all supported platforms have implemented this API, we can remove
470 // the code below. See issue #5090. 446 // the code below. See issue #5090.
471 // Related Chrome and Firefox issues: 447 // Related Chrome and Firefox issues:
472 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 448 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009
473 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 449 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
474 this.addSelectorsInline(selectors, groupName, appendOnly); 450 this.addStyleSheetInline(selectors.join(", ") +
451 " {display: none !important;}\n",
452 groupName,
453 appendOnly);
475 } 454 }
476 else 455 else
477 { 456 {
478 browser.runtime.sendMessage({ 457 browser.runtime.sendMessage({
479 type: "content.injectSelectors", 458 type: "content.injectSelectors",
480 selectors, 459 selectors,
481 groupName, 460 groupName,
482 appendOnly 461 appendOnly
483 }); 462 });
484 } 463 }
485
486 // Only trace selectors that are based directly on hiding filters
487 // (i.e. leave out collapsing selectors).
488 if (this.tracer && groupName != "collapsing")
489 this.tracer.addSelectors(selectors, filters);
490 }, 464 },
491 465
492 hideElements(elements, filters) 466 hideElements(elements, filters)
493 { 467 {
494 for (let element of elements) 468 for (let element of elements)
495 hideElement(element); 469 hideElement(element);
496 470
497 if (this.tracer) 471 if (this.tracer)
498 { 472 {
499 browser.runtime.sendMessage({ 473 browser.runtime.sendMessage({
(...skipping 15 matching lines...) Expand all
515 if (this.tracer) 489 if (this.tracer)
516 this.tracer.disconnect(); 490 this.tracer.disconnect();
517 this.tracer = null; 491 this.tracer = null;
518 492
519 if (response.trace) 493 if (response.trace)
520 this.tracer = new ElementHidingTracer(); 494 this.tracer = new ElementHidingTracer();
521 495
522 this.inline = response.inline; 496 this.inline = response.inline;
523 497
524 if (this.inline) 498 if (this.inline)
525 this.addSelectorsInline(response.selectors, "standard"); 499 this.addStyleSheetInline(response.styleSheet);
526 500
527 if (this.tracer) 501 if (this.tracer)
528 this.tracer.addSelectors(response.selectors); 502 this.tracer.addSelectors(response.selectors);
529 503
530 this.elemHideEmulation.apply(response.emulatedPatterns); 504 this.elemHideEmulation.apply(response.emulatedPatterns);
531 }); 505 });
532 } 506 }
533 }; 507 };
534 508
535 if (document instanceof HTMLDocument) 509 if (document instanceof HTMLDocument)
(...skipping 13 matching lines...) Expand all
549 let element = event.target; 523 let element = event.target;
550 if (/^i?frame$/.test(element.localName)) 524 if (/^i?frame$/.test(element.localName))
551 checkCollapse(element); 525 checkCollapse(element);
552 }, true); 526 }, true);
553 } 527 }
554 528
555 window.checkCollapse = checkCollapse; 529 window.checkCollapse = checkCollapse;
556 window.contentFiltering = contentFiltering; 530 window.contentFiltering = contentFiltering;
557 window.typeMap = typeMap; 531 window.typeMap = typeMap;
558 window.getURLsFromElement = getURLsFromElement; 532 window.getURLsFromElement = getURLsFromElement;
OLDNEW
« 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