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 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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.style = null; |
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 { |
| 385 if (selectors.length == 0 && this.injectedSelectors.length == 0) |
| 386 return; |
| 387 |
| 388 let message = {type: "elemhide.injectSelectors"}; |
| 389 |
| 390 if (selectors.length > 0) |
| 391 message.selectors = selectors; |
| 392 |
| 393 if (this.injectedSelectors.length > 0) |
| 394 message.previousSelectors = this.injectedSelectors; |
| 395 |
| 396 chrome.runtime.sendMessage(message); |
| 397 |
| 398 this.injectedSelectors = selectors; |
| 399 }, |
| 400 |
| 401 addSelectorsInline(selectors) |
| 402 { |
| 403 if (selectors.length == 0) |
| 404 return; |
| 405 |
384 if (!this.style) | 406 if (!this.style) |
385 { | 407 { |
386 // 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 |
387 // the shadow DOM if possible. Otherwise fallback to the <head> or | 409 // the shadow DOM if possible. Otherwise fallback to the <head> or |
388 // <html> element. If we have injected a style element before that | 410 // <html> element. If we have injected a style element before that |
389 // has been removed (the sheet property is null), create a new one. | 411 // has been removed (the sheet property is null), create a new one. |
390 this.style = document.createElement("style"); | 412 this.style = document.createElement("style"); |
391 (this.shadow || document.head || | 413 (this.shadow || document.head || |
392 document.documentElement).appendChild(this.style); | 414 document.documentElement).appendChild(this.style); |
393 | 415 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 let selector = preparedSelectors.slice( | 449 let selector = preparedSelectors.slice( |
428 i, i + this.selectorGroupSize | 450 i, i + this.selectorGroupSize |
429 ).join(", "); | 451 ).join(", "); |
430 this.style.sheet.insertRule(selector + "{display: none !important;}", | 452 this.style.sheet.insertRule(selector + "{display: none !important;}", |
431 this.style.sheet.cssRules.length); | 453 this.style.sheet.cssRules.length); |
432 } | 454 } |
433 }, | 455 }, |
434 | 456 |
435 addSelectors(selectors, filters) | 457 addSelectors(selectors, filters) |
436 { | 458 { |
437 if (selectors.length == 0) | 459 if (this.inline) |
438 return; | |
439 | |
440 if (this.inject) | |
441 { | 460 { |
442 // Insert the style rules inline if we have been instructed by the | 461 // 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 | 462 // 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 | 463 // that do support user stylesheets via the chrome.tabs.insertCSS API |
445 // (Firefox 53 onwards for now and possibly Chrome in the near future). | 464 // (Firefox 53 onwards for now and possibly Chrome in the near future). |
446 // Once all supported platforms have implemented this API, we can remove | 465 // Once all supported platforms have implemented this API, we can remove |
447 // the code below. See issue #5090. | 466 // the code below. See issue #5090. |
448 // Related Chrome and Firefox issues: | 467 // Related Chrome and Firefox issues: |
449 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 | 468 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 |
450 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 | 469 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 |
451 this.injectSelectors(selectors, filters); | 470 this.addSelectorsInline(selectors); |
452 } | 471 } |
453 else | 472 else |
454 { | 473 { |
455 chrome.runtime.sendMessage({ | 474 this.injectSelectors(selectors); |
456 type: "elemhide.injectSelectors", | |
457 selectors | |
458 }); | |
459 } | 475 } |
460 | 476 |
| 477 if (selectors.length == 0) |
| 478 return; |
| 479 |
461 if (this.tracer) | 480 if (this.tracer) |
462 this.tracer.addSelectors(selectors, filters); | 481 this.tracer.addSelectors(selectors, filters); |
463 }, | 482 }, |
464 | 483 |
465 hideElements(elements, filters) | 484 hideElements(elements, filters) |
466 { | 485 { |
467 for (let element of elements) | 486 for (let element of elements) |
468 hideElement(element); | 487 hideElement(element); |
469 | 488 |
470 if (this.tracer) | 489 if (this.tracer) |
(...skipping 14 matching lines...) Expand all Loading... |
485 this.tracer.disconnect(); | 504 this.tracer.disconnect(); |
486 this.tracer = null; | 505 this.tracer = null; |
487 | 506 |
488 if (this.style && this.style.parentElement) | 507 if (this.style && this.style.parentElement) |
489 this.style.parentElement.removeChild(this.style); | 508 this.style.parentElement.removeChild(this.style); |
490 this.style = null; | 509 this.style = null; |
491 | 510 |
492 if (response.trace) | 511 if (response.trace) |
493 this.tracer = new ElementHidingTracer(); | 512 this.tracer = new ElementHidingTracer(); |
494 | 513 |
495 this.inject = response.inject; | 514 this.inline = response.inline; |
496 | 515 |
497 if (this.inject) | 516 if (this.inline) |
498 this.addSelectors(response.selectors); | 517 this.addSelectors(response.selectors); |
499 else if (this.tracer) | 518 else 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 } |
OLD | NEW |