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

Delta Between Two Patch Sets: include.preload.js

Issue 29893559: Issue 6999 - Generate style sheets in background page (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Apply patch for ElementHidingTracer Created Sept. 30, 2018, 8:35 a.m.
Right Patch Set: Pass selectors if and only if trace is true Created Oct. 2, 2018, 7:07 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « dependencies ('k') | lib/contentFiltering.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 }; 383 };
384 384
385 function ContentFiltering() 385 function ContentFiltering()
386 { 386 {
387 this.styles = new Map(); 387 this.styles = new Map();
388 this.tracer = null; 388 this.tracer = null;
389 389
390 this.elemHideEmulation = new ElemHideEmulation(this.hideElements.bind(this)); 390 this.elemHideEmulation = new ElemHideEmulation(this.hideElements.bind(this));
391 } 391 }
392 ContentFiltering.prototype = { 392 ContentFiltering.prototype = {
393 addStyleSheetInline(styleSheet, groupName = "standard", appendOnly = false) 393 addRulesInline(rules, groupName = "standard", appendOnly = false)
394 { 394 {
395 let style = this.styles.get(groupName); 395 let style = this.styles.get(groupName);
396
397 if (style && !appendOnly)
398 {
399 while (style.sheet.cssRules.length > 0)
400 style.sheet.deleteRule(0);
401 }
402
403 if (rules.length == 0)
404 return;
396 405
397 if (!style) 406 if (!style)
398 { 407 {
399 // Create <style> element lazily, only if we add styles. Add it to 408 // Create <style> element lazily, only if we add styles. Add it to
400 // the <head> or <html> element. If we have injected a style element 409 // the <head> or <html> element. If we have injected a style element
401 // before that has been removed (the sheet property is null), create a 410 // before that has been removed (the sheet property is null), create a
402 // new one. 411 // new one.
403 style = document.createElement("style"); 412 style = document.createElement("style");
404 (document.head || document.documentElement).appendChild(style); 413 (document.head || document.documentElement).appendChild(style);
405 414
406 // It can happen that the frame already navigated to a different 415 // It can happen that the frame already navigated to a different
407 // document while we were waiting for the background page to respond. 416 // document while we were waiting for the background page to respond.
408 // In that case the sheet property may stay null, after adding the 417 // In that case the sheet property may stay null, after adding the
409 // <style> element. 418 // <style> element.
410 if (!style.sheet) 419 if (!style.sheet)
411 return; 420 return;
412 421
413 this.styles.set(groupName, style); 422 this.styles.set(groupName, style);
414 } 423 }
415 424
416 if (appendOnly) 425 for (let rule of rules)
417 style.textContent += styleSheet; 426 style.sheet.insertRule(rule, style.sheet.cssRules.length);
418 else
419 style.textContent = styleSheet;
420 }, 427 },
421 428
422 addSelectors(selectors, groupName = "standard", appendOnly = false) 429 addSelectors(selectors, groupName = "standard", appendOnly = false)
423 { 430 {
424 browser.runtime.sendMessage({ 431 browser.runtime.sendMessage({
425 type: "content.injectSelectors", 432 type: "content.injectSelectors",
426 selectors, 433 selectors,
427 groupName, 434 groupName,
428 appendOnly 435 appendOnly
429 }, 436 },
430 styleSheet => 437 rules =>
431 { 438 {
432 if (styleSheet) 439 if (rules)
433 { 440 {
434 // Insert the style sheet inline if we have been instructed by the 441 // Insert the rules inline if we have been instructed by the background
435 // background page to do so. This is rarely the case, except on 442 // page to do so. This is rarely the case, except on platforms that do
436 // platforms that do not support user stylesheets via the 443 // not support user stylesheets via the browser.tabs.insertCSS API
437 // browser.tabs.insertCSS API (Firefox <53, Chrome <66, and Edge). 444 // (Firefox <53, Chrome <66, and Edge).
438 // Once all supported platforms have implemented this API, we can remove 445 // Once all supported platforms have implemented this API, we can remove
439 // the code below. See issue #5090. 446 // the code below. See issue #5090.
440 // Related Chrome and Firefox issues: 447 // Related Chrome and Firefox issues:
441 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 448 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009
442 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 449 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
443 this.addStyleSheetInline(styleSheet, groupName, appendOnly); 450 this.addRulesInline(rules, groupName, appendOnly);
444 } 451 }
445 }); 452 });
446 }, 453 },
447 454
448 hideElements(elements, filters) 455 hideElements(elements, filters)
449 { 456 {
450 for (let element of elements) 457 for (let element of elements)
451 hideElement(element); 458 hideElement(element);
452 459
453 if (this.tracer) 460 if (this.tracer)
(...skipping 15 matching lines...) Expand all
469 response => 476 response =>
470 { 477 {
471 if (this.tracer) 478 if (this.tracer)
472 this.tracer.disconnect(); 479 this.tracer.disconnect();
473 this.tracer = null; 480 this.tracer = null;
474 481
475 if (response.trace) 482 if (response.trace)
476 this.tracer = new ElementHidingTracer(); 483 this.tracer = new ElementHidingTracer();
477 484
478 if (response.inline) 485 if (response.inline)
479 this.addStyleSheetInline(response.styleSheet.code); 486 this.addRulesInline(response.rules);
480 487
481 if (this.tracer) 488 if (this.tracer)
482 this.tracer.addSelectors(response.styleSheet.selectors); 489 this.tracer.addSelectors(response.selectors);
483 490
484 this.elemHideEmulation.apply(response.emulatedPatterns); 491 this.elemHideEmulation.apply(response.emulatedPatterns);
485 }); 492 });
486 } 493 }
487 }; 494 };
488 495
489 if (document instanceof HTMLDocument) 496 if (document instanceof HTMLDocument)
490 { 497 {
491 checkSitekey(); 498 checkSitekey();
492 499
(...skipping 10 matching lines...) Expand all
503 let element = event.target; 510 let element = event.target;
504 if (/^i?frame$/.test(element.localName)) 511 if (/^i?frame$/.test(element.localName))
505 checkCollapse(element); 512 checkCollapse(element);
506 }, true); 513 }, true);
507 } 514 }
508 515
509 window.checkCollapse = checkCollapse; 516 window.checkCollapse = checkCollapse;
510 window.contentFiltering = contentFiltering; 517 window.contentFiltering = contentFiltering;
511 window.typeMap = typeMap; 518 window.typeMap = typeMap;
512 window.getURLsFromElement = getURLsFromElement; 519 window.getURLsFromElement = getURLsFromElement;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld