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

Side by Side Diff: include.preload.js

Issue 29575739: Issue 5864 - Remove previous style sheet before adding one (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Remove some code duplication Created Oct. 14, 2017, 9:52 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/cssInjection.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 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 { 335 {
336 document.removeEventListener("DOMContentLoaded", this.trace); 336 document.removeEventListener("DOMContentLoaded", this.trace);
337 this.observer.disconnect(); 337 this.observer.disconnect();
338 clearTimeout(this.timeout); 338 clearTimeout(this.timeout);
339 } 339 }
340 }; 340 };
341 341
342 function ElemHide() 342 function ElemHide()
343 { 343 {
344 this.shadow = this.createShadowTree(); 344 this.shadow = this.createShadowTree();
345 this.style = null; 345 this.styles = new Map();
346 this.tracer = null; 346 this.tracer = null;
347 this.inject = true; 347 this.inline = true;
348 this.emulatedPatterns = null; 348 this.emulatedPatterns = null;
349 this.injectedSelectors = [];
349 350
350 this.elemHideEmulation = new ElemHideEmulation( 351 this.elemHideEmulation = new ElemHideEmulation(
351 this.addSelectors.bind(this), 352 this.addSelectors.bind(this),
352 this.hideElements.bind(this) 353 this.hideElements.bind(this)
353 ); 354 );
354 } 355 }
355 ElemHide.prototype = { 356 ElemHide.prototype = {
356 selectorGroupSize: 200, 357 selectorGroupSize: 200,
357 358
358 createShadowTree() 359 createShadowTree()
(...skipping 13 matching lines...) Expand all
372 // Finally since some users have both AdBlock and Adblock Plus installed we 373 // Finally since some users have both AdBlock and Adblock Plus installed we
373 // have to consider how the two extensions interact. For example we want to 374 // have to consider how the two extensions interact. For example we want to
374 // avoid creating the shadowRoot twice. 375 // avoid creating the shadowRoot twice.
375 let shadow = document.documentElement.shadowRoot || 376 let shadow = document.documentElement.shadowRoot ||
376 document.documentElement.createShadowRoot(); 377 document.documentElement.createShadowRoot();
377 shadow.appendChild(document.createElement("shadow")); 378 shadow.appendChild(document.createElement("shadow"));
378 379
379 return shadow; 380 return shadow;
380 }, 381 },
381 382
382 injectSelectors(selectors, filters) 383 injectSelectors(selectors)
383 { 384 {
384 if (!this.style) 385 if (selectors.length == 0 && this.injectedSelectors.length == 0)
386 return;
387
388 chrome.runtime.sendMessage({
389 type: "elemhide.injectSelectors",
390 selectors,
391 oldSelectors: this.injectedSelectors
392 });
393
394 this.injectedSelectors = selectors;
395 },
396
397 addSelectorsInline(selectors, filters, groupName)
398 {
399 let style = this.styles.get(groupName);
400
401 if (style)
402 {
403 while (style.sheet.cssRules.length > 0)
404 style.sheet.deleteRule(0);
405 }
406
407 if (selectors.length == 0)
408 return;
409
410 if (!style)
385 { 411 {
386 // Create <style> element lazily, only if we add styles. Add it to 412 // Create <style> element lazily, only if we add styles. Add it to
387 // the shadow DOM if possible. Otherwise fallback to the <head> or 413 // the shadow DOM if possible. Otherwise fallback to the <head> or
388 // <html> element. If we have injected a style element before that 414 // <html> element. If we have injected a style element before that
389 // has been removed (the sheet property is null), create a new one. 415 // has been removed (the sheet property is null), create a new one.
390 this.style = document.createElement("style"); 416 style = document.createElement("style");
391 (this.shadow || document.head || 417 (this.shadow || document.head ||
392 document.documentElement).appendChild(this.style); 418 document.documentElement).appendChild(style);
393 419
394 // It can happen that the frame already navigated to a different 420 // It can happen that the frame already navigated to a different
395 // document while we were waiting for the background page to respond. 421 // document while we were waiting for the background page to respond.
396 // In that case the sheet property will stay null, after addind the 422 // In that case the sheet property will stay null, after addind the
397 // <style> element to the shadow DOM. 423 // <style> element to the shadow DOM.
398 if (!this.style.sheet) 424 if (!style.sheet)
399 return; 425 return;
426
427 this.styles.set(groupName, style);
400 } 428 }
401 429
402 // If using shadow DOM, we have to add the ::content pseudo-element 430 // If using shadow DOM, we have to add the ::content pseudo-element
403 // before each selector, in order to match elements within the 431 // before each selector, in order to match elements within the
404 // insertion point. 432 // insertion point.
405 let preparedSelectors = []; 433 let preparedSelectors = [];
406 if (this.shadow) 434 if (this.shadow)
407 { 435 {
408 for (let selector of selectors) 436 for (let selector of selectors)
409 { 437 {
(...skipping 10 matching lines...) Expand all
420 // Safari only allows 8192 primitive selectors to be injected at once[1], we 448 // Safari only allows 8192 primitive selectors to be injected at once[1], we
421 // therefore chunk the inserted selectors into groups of 200 to be safe. 449 // therefore chunk the inserted selectors into groups of 200 to be safe.
422 // (Chrome also has a limit, larger... but we're not certain exactly what it 450 // (Chrome also has a limit, larger... but we're not certain exactly what it
423 // is! Edge apparently has no such limit.) 451 // is! Edge apparently has no such limit.)
424 // [1] - https://github.com/WebKit/webkit/blob/1cb2227f6b2a1035f7bdc46e5ab69 debb75fc1de/Source/WebCore/css/RuleSet.h#L68 452 // [1] - https://github.com/WebKit/webkit/blob/1cb2227f6b2a1035f7bdc46e5ab69 debb75fc1de/Source/WebCore/css/RuleSet.h#L68
425 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) 453 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize)
426 { 454 {
427 let selector = preparedSelectors.slice( 455 let selector = preparedSelectors.slice(
428 i, i + this.selectorGroupSize 456 i, i + this.selectorGroupSize
429 ).join(", "); 457 ).join(", ");
430 this.style.sheet.insertRule(selector + "{display: none !important;}", 458 style.sheet.insertRule(selector + "{display: none !important;}",
431 this.style.sheet.cssRules.length); 459 style.sheet.cssRules.length);
432 } 460 }
433 }, 461 },
434 462
435 addSelectors(selectors, filters) 463 addSelectors(selectors, filters)
436 { 464 {
437 if (selectors.length == 0) 465 if (this.inline)
438 return;
439
440 if (this.inject)
441 { 466 {
442 // Insert the style rules inline if we have been instructed by the 467 // Insert the style rules inline if we have been instructed by the
443 // background page to do so. This is usually the case, except on platforms 468 // background page to do so. This is usually the case, except on platforms
444 // that do support user stylesheets via the chrome.tabs.insertCSS API 469 // that do support user stylesheets via the chrome.tabs.insertCSS API
445 // (Firefox 53 onwards for now and possibly Chrome in the near future). 470 // (Firefox 53 onwards for now and possibly Chrome in the near future).
446 // Once all supported platforms have implemented this API, we can remove 471 // Once all supported platforms have implemented this API, we can remove
447 // the code below. See issue #5090. 472 // the code below. See issue #5090.
448 // Related Chrome and Firefox issues: 473 // Related Chrome and Firefox issues:
449 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 474 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009
450 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 475 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
451 this.injectSelectors(selectors, filters); 476 this.addSelectorsInline(selectors, filters, "emulated");
452 } 477 }
453 else 478 else
454 { 479 {
455 chrome.runtime.sendMessage({ 480 this.injectSelectors(selectors);
456 type: "elemhide.injectSelectors",
457 selectors
458 });
459 } 481 }
460 482
461 if (this.tracer) 483 if (this.tracer)
462 this.tracer.addSelectors(selectors, filters); 484 this.tracer.addSelectors(selectors, filters);
463 }, 485 },
464 486
465 hideElements(elements, filters) 487 hideElements(elements, filters)
466 { 488 {
467 for (let element of elements) 489 for (let element of elements)
468 hideElement(element); 490 hideElement(element);
469 491
470 if (this.tracer) 492 if (this.tracer)
471 { 493 {
472 chrome.runtime.sendMessage({ 494 chrome.runtime.sendMessage({
473 type: "devtools.traceElemHide", 495 type: "devtools.traceElemHide",
474 selectors: [], 496 selectors: [],
475 filters 497 filters
476 }); 498 });
477 } 499 }
478 }, 500 },
479 501
480 apply() 502 apply()
481 { 503 {
482 chrome.runtime.sendMessage({type: "elemhide.getSelectors"}, response => 504 chrome.runtime.sendMessage({type: "elemhide.getSelectors"}, response =>
483 { 505 {
484 if (this.tracer) 506 if (this.tracer)
485 this.tracer.disconnect(); 507 this.tracer.disconnect();
486 this.tracer = null; 508 this.tracer = null;
487 509
488 if (this.style && this.style.parentElement)
489 this.style.parentElement.removeChild(this.style);
490 this.style = null;
491
492 if (response.trace) 510 if (response.trace)
493 this.tracer = new ElementHidingTracer(); 511 this.tracer = new ElementHidingTracer();
494 512
495 this.inject = response.inject; 513 this.inline = response.inline;
496 514
497 if (this.inject) 515 if (this.inline)
498 this.addSelectors(response.selectors); 516 this.addSelectorsInline(response.selectors);
499 else if (this.tracer) 517
518 if (this.tracer)
500 this.tracer.addSelectors(response.selectors); 519 this.tracer.addSelectors(response.selectors);
501 520
502 this.elemHideEmulation.apply(response.emulatedPatterns); 521 this.elemHideEmulation.apply(response.emulatedPatterns);
503 }); 522 });
504 } 523 }
505 }; 524 };
506 525
507 if (document instanceof HTMLDocument) 526 if (document instanceof HTMLDocument)
508 { 527 {
509 checkSitekey(); 528 checkSitekey();
510 529
511 elemhide = new ElemHide(); 530 elemhide = new ElemHide();
512 elemhide.apply(); 531 elemhide.apply();
513 532
514 document.addEventListener("error", event => 533 document.addEventListener("error", event =>
515 { 534 {
516 checkCollapse(event.target); 535 checkCollapse(event.target);
517 }, true); 536 }, true);
518 537
519 document.addEventListener("load", event => 538 document.addEventListener("load", event =>
520 { 539 {
521 let element = event.target; 540 let element = event.target;
522 if (/^i?frame$/.test(element.localName)) 541 if (/^i?frame$/.test(element.localName))
523 checkCollapse(element); 542 checkCollapse(element);
524 }, true); 543 }, true);
525 } 544 }
OLDNEW
« no previous file with comments | « no previous file | lib/cssInjection.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld