Left: | ||
Right: |
OLD | NEW |
---|---|
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 Loading... | |
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(); |
Manish Jethani
2017/10/14 21:35:33
Now it's a 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 Loading... | |
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({ | |
Manish Jethani
2017/10/14 21:35:33
Keeping it simple.
| |
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); | |
Manish Jethani
2017/10/14 21:35:33
Using CSSStyleSheet.deleteRule here.
| |
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 Loading... | |
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 } |
461 | |
462 if (this.tracer) | |
463 this.tracer.addSelectors(selectors, filters); | |
433 }, | 464 }, |
434 | 465 |
435 addSelectors(selectors, filters) | 466 addSelectors(selectors, filters) |
436 { | 467 { |
437 if (selectors.length == 0) | 468 if (this.inline) |
438 return; | |
439 | |
440 if (this.inject) | |
441 { | 469 { |
442 // Insert the style rules inline if we have been instructed by the | 470 // 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 | 471 // 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 | 472 // that do support user stylesheets via the chrome.tabs.insertCSS API |
445 // (Firefox 53 onwards for now and possibly Chrome in the near future). | 473 // (Firefox 53 onwards for now and possibly Chrome in the near future). |
446 // Once all supported platforms have implemented this API, we can remove | 474 // Once all supported platforms have implemented this API, we can remove |
447 // the code below. See issue #5090. | 475 // the code below. See issue #5090. |
448 // Related Chrome and Firefox issues: | 476 // Related Chrome and Firefox issues: |
449 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 | 477 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 |
450 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 | 478 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 |
451 this.injectSelectors(selectors, filters); | 479 this.addSelectorsInline(selectors, filters, "emulated"); |
452 } | 480 } |
453 else | 481 else |
454 { | 482 { |
455 chrome.runtime.sendMessage({ | 483 this.injectSelectors(selectors); |
456 type: "elemhide.injectSelectors", | 484 |
457 selectors | 485 if (this.tracer && selectors.length > 0) |
458 }); | 486 this.tracer.addSelectors(selectors, filters); |
459 } | 487 } |
460 | |
461 if (this.tracer) | |
462 this.tracer.addSelectors(selectors, filters); | |
463 }, | 488 }, |
464 | 489 |
465 hideElements(elements, filters) | 490 hideElements(elements, filters) |
466 { | 491 { |
467 for (let element of elements) | 492 for (let element of elements) |
468 hideElement(element); | 493 hideElement(element); |
469 | 494 |
470 if (this.tracer) | 495 if (this.tracer) |
471 { | 496 { |
472 chrome.runtime.sendMessage({ | 497 chrome.runtime.sendMessage({ |
473 type: "devtools.traceElemHide", | 498 type: "devtools.traceElemHide", |
474 selectors: [], | 499 selectors: [], |
475 filters | 500 filters |
476 }); | 501 }); |
477 } | 502 } |
478 }, | 503 }, |
479 | 504 |
480 apply() | 505 apply() |
481 { | 506 { |
482 chrome.runtime.sendMessage({type: "elemhide.getSelectors"}, response => | 507 chrome.runtime.sendMessage({type: "elemhide.getSelectors"}, response => |
483 { | 508 { |
484 if (this.tracer) | 509 if (this.tracer) |
485 this.tracer.disconnect(); | 510 this.tracer.disconnect(); |
486 this.tracer = null; | 511 this.tracer = null; |
487 | 512 |
488 if (this.style && this.style.parentElement) | |
489 this.style.parentElement.removeChild(this.style); | |
490 this.style = null; | |
491 | |
492 if (response.trace) | 513 if (response.trace) |
493 this.tracer = new ElementHidingTracer(); | 514 this.tracer = new ElementHidingTracer(); |
494 | 515 |
495 this.inject = response.inject; | 516 this.inline = response.inline; |
496 | 517 |
497 if (this.inject) | 518 if (this.inline) |
498 this.addSelectors(response.selectors); | 519 this.addSelectorsInline(response.selectors); |
Manish Jethani
2017/10/14 21:35:33
Note that we don't have to pass the group name her
| |
499 else if (this.tracer) | 520 else if (this.tracer) |
500 this.tracer.addSelectors(response.selectors); | 521 this.tracer.addSelectors(response.selectors); |
501 | 522 |
502 this.elemHideEmulation.apply(response.emulatedPatterns); | 523 this.elemHideEmulation.apply(response.emulatedPatterns); |
503 }); | 524 }); |
504 } | 525 } |
505 }; | 526 }; |
506 | 527 |
507 if (document instanceof HTMLDocument) | 528 if (document instanceof HTMLDocument) |
508 { | 529 { |
509 checkSitekey(); | 530 checkSitekey(); |
510 | 531 |
511 elemhide = new ElemHide(); | 532 elemhide = new ElemHide(); |
512 elemhide.apply(); | 533 elemhide.apply(); |
513 | 534 |
514 document.addEventListener("error", event => | 535 document.addEventListener("error", event => |
515 { | 536 { |
516 checkCollapse(event.target); | 537 checkCollapse(event.target); |
517 }, true); | 538 }, true); |
518 | 539 |
519 document.addEventListener("load", event => | 540 document.addEventListener("load", event => |
520 { | 541 { |
521 let element = event.target; | 542 let element = event.target; |
522 if (/^i?frame$/.test(element.localName)) | 543 if (/^i?frame$/.test(element.localName)) |
523 checkCollapse(element); | 544 checkCollapse(element); |
524 }, true); | 545 }, true); |
525 } | 546 } |
OLD | NEW |