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

Side by Side Diff: include.preload.js

Issue 29894568: Issue 6999 - Generate style sheets in background page (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Sept. 28, 2018, 12:27 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 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 this.inline = true; 401 this.inline = true;
402 402
403 this.elemHideEmulation = new ElemHideEmulation( 403 this.elemHideEmulation = new ElemHideEmulation(
404 () => {}, 404 () => {},
405 this.hideElements.bind(this) 405 this.hideElements.bind(this)
406 ); 406 );
407 } 407 }
408 ContentFiltering.prototype = { 408 ContentFiltering.prototype = {
409 selectorGroupSize: 1024, 409 selectorGroupSize: 1024,
410 410
411 createStyleElement()
412 {
413 // Create <style> element lazily, only if we add styles. Add it to
414 // the <head> or <html> element. If we have injected a style element
415 // before that has been removed (the sheet property is null), create a
416 // new one.
417 let style = document.createElement("style");
418 (document.head || document.documentElement).appendChild(style);
419
420 // It can happen that the frame already navigated to a different
421 // document while we were waiting for the background page to respond.
422 // In that case the sheet property may stay null, after adding the
423 // <style> element.
424 if (!style.sheet)
425 return null;
426
427 return style;
428 },
429
430 addStyleSheet(styleSheet, groupName)
431 {
432 let style = this.styles.get(groupName);
433
434 if (!style)
435 {
436 style = this.createStyleElement();
437 if (!style)
438 return;
439
440 this.styles.set(groupName, style);
441 }
442
443 style.textContent = styleSheet;
444 },
445
411 addSelectorsInline(selectors, groupName, appendOnly = false) 446 addSelectorsInline(selectors, groupName, appendOnly = false)
412 { 447 {
413 let style = this.styles.get(groupName); 448 let style = this.styles.get(groupName);
414 449
415 if (style && !appendOnly) 450 if (style && !appendOnly)
416 { 451 {
417 while (style.sheet.cssRules.length > 0) 452 while (style.sheet.cssRules.length > 0)
418 style.sheet.deleteRule(0); 453 style.sheet.deleteRule(0);
419 } 454 }
420 455
421 if (selectors.length == 0) 456 if (selectors.length == 0)
422 return; 457 return;
423 458
424 if (!style) 459 if (!style)
425 { 460 {
426 // Create <style> element lazily, only if we add styles. Add it to 461 style = this.createStyleElement();
427 // the <head> or <html> element. If we have injected a style element 462 if (!style)
428 // before that has been removed (the sheet property is null), create a
429 // new one.
430 style = document.createElement("style");
431 (document.head || document.documentElement).appendChild(style);
432
433 // It can happen that the frame already navigated to a different
434 // document while we were waiting for the background page to respond.
435 // In that case the sheet property may stay null, after adding the
436 // <style> element.
437 if (!style.sheet)
438 return; 463 return;
439 464
440 this.styles.set(groupName, style); 465 this.styles.set(groupName, style);
441 } 466 }
442 467
443 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 468 // Chromium's Blink engine supports only up to 8,192 simple selectors, and
444 // even fewer compound selectors, in a rule. The exact number of selectors 469 // even fewer compound selectors, in a rule. The exact number of selectors
445 // that would work depends on their sizes (e.g. "#foo .bar" has a 470 // that would work depends on their sizes (e.g. "#foo .bar" has a
446 // size of 2). Since we don't know the sizes of the selectors here, we 471 // size of 2). Since we don't know the sizes of the selectors here, we
447 // simply split them into groups of 1,024, based on the reasonable 472 // simply split them into groups of 1,024, based on the reasonable
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 if (this.tracer) 541 if (this.tracer)
517 this.tracer.disconnect(); 542 this.tracer.disconnect();
518 this.tracer = null; 543 this.tracer = null;
519 544
520 if (response.trace) 545 if (response.trace)
521 this.tracer = new ElementHidingTracer(); 546 this.tracer = new ElementHidingTracer();
522 547
523 this.inline = response.inline; 548 this.inline = response.inline;
524 549
525 if (this.inline) 550 if (this.inline)
526 this.addSelectorsInline(response.selectors, "standard"); 551 this.addStyleSheet(response.styleSheet, "standard");
527 552
528 if (this.tracer) 553 if (this.tracer)
529 this.tracer.addSelectors(response.selectors); 554 this.tracer.addSelectors(response.selectors);
530 555
531 this.elemHideEmulation.apply(response.emulatedPatterns); 556 this.elemHideEmulation.apply(response.emulatedPatterns);
532 }); 557 });
533 } 558 }
534 }; 559 };
535 560
536 if (document instanceof HTMLDocument) 561 if (document instanceof HTMLDocument)
(...skipping 13 matching lines...) Expand all
550 let element = event.target; 575 let element = event.target;
551 if (/^i?frame$/.test(element.localName)) 576 if (/^i?frame$/.test(element.localName))
552 checkCollapse(element); 577 checkCollapse(element);
553 }, true); 578 }, true);
554 } 579 }
555 580
556 window.checkCollapse = checkCollapse; 581 window.checkCollapse = checkCollapse;
557 window.contentFiltering = contentFiltering; 582 window.contentFiltering = contentFiltering;
558 window.typeMap = typeMap; 583 window.typeMap = typeMap;
559 window.getURLsFromElement = getURLsFromElement; 584 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