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: Updated implementation Created Sept. 28, 2018, 1:23 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 389 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 selectorGroupSize: 1024,
409 409
410 getStyleElement(groupName)
411 {
412 let style = this.styles.get(groupName);
413 if (style)
414 return style;
415
416 // Create <style> element lazily, only if we add styles. Add it to
417 // the <head> or <html> element. If we have injected a style element
418 // before that has been removed (the sheet property is null), create a
419 // new one.
420 let style = document.createElement("style");
421 (document.head || document.documentElement).appendChild(style);
422
423 // It can happen that the frame already navigated to a different
424 // document while we were waiting for the background page to respond.
425 // In that case the sheet property may stay null, after adding the
426 // <style> element.
427 if (!style.sheet)
428 return null;
429
430 this.styles.set(groupName, style);
431
432 return style;
433 },
434
435 addStyleSheetInline(styleSheet, groupName = "standard")
436 {
437 let style = this.getStyleElement(groupName);
438 if (style)
439 style.textContent = styleSheet;
440 },
441
410 addSelectorsInline(selectors, groupName, appendOnly = false) 442 addSelectorsInline(selectors, groupName, appendOnly = false)
411 { 443 {
412 let style = this.styles.get(groupName); 444 let style = this.getStyleElement(groupName);
445 if (!style)
446 return;
413 447
414 if (style && !appendOnly) 448 if (!appendOnly)
415 { 449 {
416 while (style.sheet.cssRules.length > 0) 450 while (style.sheet.cssRules.length > 0)
417 style.sheet.deleteRule(0); 451 style.sheet.deleteRule(0);
418 } 452 }
419 453
420 if (selectors.length == 0)
421 return;
422
423 if (!style)
424 {
425 // 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
427 // before that has been removed (the sheet property is null), create a
428 // new one.
429 style = document.createElement("style");
430 (document.head || document.documentElement).appendChild(style);
431
432 // It can happen that the frame already navigated to a different
433 // document while we were waiting for the background page to respond.
434 // In that case the sheet property may stay null, after adding the
435 // <style> element.
436 if (!style.sheet)
437 return;
438
439 this.styles.set(groupName, style);
440 }
441
442 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 454 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
443 // even fewer compound selectors, in a rule. The exact number of selectors 455 // even fewer compound selectors, in a rule. The exact number of selectors
444 // that would work depends on their sizes (e.g. "#foo .bar" has a 456 // that would work depends on their sizes (e.g. "#foo .bar" has a
445 // size of 2). Since we don't know the sizes of the selectors here, we 457 // size of 2). Since we don't know the sizes of the selectors here, we
446 // simply split them into groups of 1,024, based on the reasonable 458 // 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. 459 // 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 460 // 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 461 // 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 462 // worked well in practice. In theory this could still lead to some
451 // selectors not working on Chromium, but it is highly unlikely. 463 // selectors not working on Chromium, but it is highly unlikely.
452 // See issue #6298 and https://crbug.com/804179 464 // See issue #6298 and https://crbug.com/804179
453 for (let i = 0; i < selectors.length; i += this.selectorGroupSize) 465 for (let i = 0; i < selectors.length; i += this.selectorGroupSize)
Sebastian Noack 2018/09/28 15:33:41 What is this code still good for if stylesheets ar
Manish Jethani 2018/09/28 16:04:44 This is for element collapsing. If this.inline is
Sebastian Noack 2018/09/28 16:09:06 Please correct me if I'm wrong, but wouldn't it si
Manish Jethani 2018/09/28 16:12:51 Yes, I agree. What's more is that it's going to be
Sebastian Noack 2018/09/28 16:32:50 Sounds good. Mind implementing those changes?
Manish Jethani 2018/09/28 20:20:23 I thought about this a little more. It's always go
454 { 466 {
455 let selector = selectors.slice(i, i + this.selectorGroupSize).join(", "); 467 let selector = selectors.slice(i, i + this.selectorGroupSize).join(", ");
456 style.sheet.insertRule(selector + "{display: none !important;}", 468 style.sheet.insertRule(selector + "{display: none !important;}",
457 style.sheet.cssRules.length); 469 style.sheet.cssRules.length);
458 } 470 }
459 }, 471 },
460 472
461 addSelectors(selectors, filters, groupName = "emulated", appendOnly = false) 473 addSelectors(selectors, filters, groupName = "emulated", appendOnly = false)
462 { 474 {
463 if (this.inline) 475 if (this.inline)
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 if (this.tracer) 527 if (this.tracer)
516 this.tracer.disconnect(); 528 this.tracer.disconnect();
517 this.tracer = null; 529 this.tracer = null;
518 530
519 if (response.trace) 531 if (response.trace)
520 this.tracer = new ElementHidingTracer(); 532 this.tracer = new ElementHidingTracer();
521 533
522 this.inline = response.inline; 534 this.inline = response.inline;
523 535
524 if (this.inline) 536 if (this.inline)
525 this.addSelectorsInline(response.selectors, "standard"); 537 this.addStyleSheetInline(response.styleSheet);
526 538
527 if (this.tracer) 539 if (this.tracer)
528 this.tracer.addSelectors(response.selectors); 540 this.tracer.addSelectors(response.selectors);
529 541
530 this.elemHideEmulation.apply(response.emulatedPatterns); 542 this.elemHideEmulation.apply(response.emulatedPatterns);
531 }); 543 });
532 } 544 }
533 }; 545 };
534 546
535 if (document instanceof HTMLDocument) 547 if (document instanceof HTMLDocument)
(...skipping 13 matching lines...) Expand all
549 let element = event.target; 561 let element = event.target;
550 if (/^i?frame$/.test(element.localName)) 562 if (/^i?frame$/.test(element.localName))
551 checkCollapse(element); 563 checkCollapse(element);
552 }, true); 564 }, true);
553 } 565 }
554 566
555 window.checkCollapse = checkCollapse; 567 window.checkCollapse = checkCollapse;
556 window.contentFiltering = contentFiltering; 568 window.contentFiltering = contentFiltering;
557 window.typeMap = typeMap; 569 window.typeMap = typeMap;
558 window.getURLsFromElement = getURLsFromElement; 570 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