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

Delta Between Two Patch Sets: include.preload.js

Issue 29575739: Issue 5864 - Remove previous style sheet before adding one (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Maintain separate style sheet for emulated selectors Created Oct. 14, 2017, 2:18 p.m.
Right Patch Set: Fix rebase Created Jan. 25, 2018, 2:37 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 | « no previous file | lib/cssInjection.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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 function checkCollapse(element) 158 function checkCollapse(element)
159 { 159 {
160 let mediatype = typeMap.get(element.localName); 160 let mediatype = typeMap.get(element.localName);
161 if (!mediatype) 161 if (!mediatype)
162 return; 162 return;
163 163
164 let urls = getURLsFromElement(element); 164 let urls = getURLsFromElement(element);
165 if (urls.length == 0) 165 if (urls.length == 0)
166 return; 166 return;
167 167
168 chrome.runtime.sendMessage( 168 browser.runtime.sendMessage(
169 { 169 {
170 type: "filters.collapse", 170 type: "filters.collapse",
171 urls, 171 urls,
172 mediatype, 172 mediatype,
173 baseURL: document.location.href 173 baseURL: document.location.href
174 }, 174 },
175 175
176 collapse => 176 collapse =>
177 { 177 {
178 if (collapse) 178 if (collapse)
179 { 179 {
180 hideElement(element); 180 hideElement(element);
181 } 181 }
182 } 182 }
183 ); 183 );
184 } 184 }
185 185
186 function checkSitekey() 186 function checkSitekey()
187 { 187 {
188 let attr = document.documentElement.getAttribute("data-adblockkey"); 188 let attr = document.documentElement.getAttribute("data-adblockkey");
189 if (attr) 189 if (attr)
190 chrome.runtime.sendMessage({type: "filters.addKey", token: attr}); 190 browser.runtime.sendMessage({type: "filters.addKey", token: attr});
191 } 191 }
192 192
193 function ElementHidingTracer() 193 function ElementHidingTracer()
194 { 194 {
195 this.selectors = []; 195 this.selectors = [];
196 this.changedNodes = []; 196 this.changedNodes = [];
197 this.timeout = null; 197 this.timeout = null;
198 this.observer = new MutationObserver(this.observe.bind(this)); 198 this.observer = new MutationObserver(this.observe.bind(this));
199 this.trace = this.trace.bind(this); 199 this.trace = this.trace.bind(this);
200 200
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 selectors.push(selector); 241 selectors.push(selector);
242 242
243 break nodes; 243 break nodes;
244 } 244 }
245 } 245 }
246 } 246 }
247 } 247 }
248 248
249 if (selectors.length > 0 || filters.length > 0) 249 if (selectors.length > 0 || filters.length > 0)
250 { 250 {
251 chrome.runtime.sendMessage({ 251 browser.runtime.sendMessage({
252 type: "devtools.traceElemHide", 252 type: "devtools.traceElemHide",
253 selectors, filters 253 selectors, filters
254 }); 254 });
255 } 255 }
256 }, 256 },
257 257
258 onTimeout() 258 onTimeout()
259 { 259 {
260 this.checkNodes(this.changedNodes, this.selectors); 260 this.checkNodes(this.changedNodes, this.selectors);
261 this.changedNodes = []; 261 this.changedNodes = [];
(...skipping 73 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.emulatedStyle = null;
347 this.tracer = null; 346 this.tracer = null;
348 this.inline = true; 347 this.inline = true;
349 this.emulatedPatterns = null; 348 this.emulatedPatterns = null;
350 this.injectedSelectors = [];
351 349
352 this.elemHideEmulation = new ElemHideEmulation( 350 this.elemHideEmulation = new ElemHideEmulation(
353 this.addSelectors.bind(this), 351 this.addSelectors.bind(this),
354 this.hideElements.bind(this) 352 this.hideElements.bind(this)
355 ); 353 );
356 } 354 }
357 ElemHide.prototype = { 355 ElemHide.prototype = {
358 selectorGroupSize: 200, 356 selectorGroupSize: 200,
359 357
360 createShadowTree() 358 createShadowTree()
(...skipping 13 matching lines...) Expand all
374 // Finally since some users have both AdBlock and Adblock Plus installed we 372 // Finally since some users have both AdBlock and Adblock Plus installed we
375 // have to consider how the two extensions interact. For example we want to 373 // have to consider how the two extensions interact. For example we want to
376 // avoid creating the shadowRoot twice. 374 // avoid creating the shadowRoot twice.
377 let shadow = document.documentElement.shadowRoot || 375 let shadow = document.documentElement.shadowRoot ||
378 document.documentElement.createShadowRoot(); 376 document.documentElement.createShadowRoot();
379 shadow.appendChild(document.createElement("shadow")); 377 shadow.appendChild(document.createElement("shadow"));
380 378
381 return shadow; 379 return shadow;
382 }, 380 },
383 381
384 injectSelectors(selectors) 382 addSelectorsInline(selectors, groupName)
385 { 383 {
386 if (selectors.length == 0 && this.injectedSelectors.length == 0) 384 let style = this.styles.get(groupName);
387 return; 385
388 386 if (style)
389 let message = {type: "elemhide.injectSelectors"}; 387 {
390 388 while (style.sheet.cssRules.length > 0)
391 if (selectors.length > 0) 389 style.sheet.deleteRule(0);
392 message.selectors = selectors;
393
394 if (this.injectedSelectors.length > 0)
395 message.replaceSelectors = this.injectedSelectors;
Manish Jethani 2017/10/14 14:23:22 Renamed for clarity.
396
397 chrome.runtime.sendMessage(message);
398 this.injectedSelectors = selectors;
399 },
400
401 addSelectorsInline(selectors, filters, emulated)
402 {
403 if (emulated && this.emulatedStyle)
Manish Jethani 2017/10/14 14:23:21 So there's a separate style sheet for emulated sel
404 {
405 this.emulatedStyle.parentNode.removeChild(this.emulatedStyle);
406 this.emulatedStyle = null;
407 } 390 }
408 391
409 if (selectors.length == 0) 392 if (selectors.length == 0)
410 return; 393 return;
411
412 let style = emulated ? this.emulatedStyle : this.style;
413 394
414 if (!style) 395 if (!style)
415 { 396 {
416 // Create <style> element lazily, only if we add styles. Add it to 397 // Create <style> element lazily, only if we add styles. Add it to
417 // the shadow DOM if possible. Otherwise fallback to the <head> or 398 // the shadow DOM if possible. Otherwise fallback to the <head> or
418 // <html> element. If we have injected a style element before that 399 // <html> element. If we have injected a style element before that
419 // has been removed (the sheet property is null), create a new one. 400 // has been removed (the sheet property is null), create a new one.
420 style = document.createElement("style"); 401 style = document.createElement("style");
421 (this.shadow || document.head || 402 (this.shadow || document.head ||
422 document.documentElement).appendChild(style); 403 document.documentElement).appendChild(style);
423 404
424 // It can happen that the frame already navigated to a different 405 // It can happen that the frame already navigated to a different
425 // document while we were waiting for the background page to respond. 406 // document while we were waiting for the background page to respond.
426 // In that case the sheet property will stay null, after addind the 407 // In that case the sheet property will stay null, after addind the
427 // <style> element to the shadow DOM. 408 // <style> element to the shadow DOM.
428 if (!style.sheet) 409 if (!style.sheet)
429 return; 410 return;
430 411
431 if (emulated) 412 this.styles.set(groupName, style);
432 this.emulatedStyle = style;
433 else
434 this.style = style;
435 } 413 }
436 414
437 // If using shadow DOM, we have to add the ::content pseudo-element 415 // If using shadow DOM, we have to add the ::content pseudo-element
438 // before each selector, in order to match elements within the 416 // before each selector, in order to match elements within the
439 // insertion point. 417 // insertion point.
440 let preparedSelectors = []; 418 let preparedSelectors = [];
441 if (this.shadow) 419 if (this.shadow)
442 { 420 {
443 for (let selector of selectors) 421 for (let selector of selectors)
444 { 422 {
(...skipping 10 matching lines...) Expand all
455 // Safari only allows 8192 primitive selectors to be injected at once[1], we 433 // Safari only allows 8192 primitive selectors to be injected at once[1], we
456 // therefore chunk the inserted selectors into groups of 200 to be safe. 434 // therefore chunk the inserted selectors into groups of 200 to be safe.
457 // (Chrome also has a limit, larger... but we're not certain exactly what it 435 // (Chrome also has a limit, larger... but we're not certain exactly what it
458 // is! Edge apparently has no such limit.) 436 // is! Edge apparently has no such limit.)
459 // [1] - https://github.com/WebKit/webkit/blob/1cb2227f6b2a1035f7bdc46e5ab69 debb75fc1de/Source/WebCore/css/RuleSet.h#L68 437 // [1] - https://github.com/WebKit/webkit/blob/1cb2227f6b2a1035f7bdc46e5ab69 debb75fc1de/Source/WebCore/css/RuleSet.h#L68
460 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) 438 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize)
461 { 439 {
462 let selector = preparedSelectors.slice( 440 let selector = preparedSelectors.slice(
463 i, i + this.selectorGroupSize 441 i, i + this.selectorGroupSize
464 ).join(", "); 442 ).join(", ");
465 style.sheet.insertRule(selector + "{display: none !important;}", 443 this.style.sheet.insertRule(selector + "{display: none !important;}",
466 style.sheet.cssRules.length); 444 this.style.sheet.cssRules.length);
467 } 445 }
468
469 if (this.tracer)
470 this.tracer.addSelectors(selectors, filters);
471 }, 446 },
472 447
473 addSelectors(selectors, filters) 448 addSelectors(selectors, filters)
474 { 449 {
475 if (this.inline) 450 if (this.inline)
476 { 451 {
477 // Insert the style rules inline if we have been instructed by the 452 // Insert the style rules inline if we have been instructed by the
478 // background page to do so. This is usually the case, except on platforms 453 // background page to do so. This is usually the case, except on platforms
479 // that do support user stylesheets via the chrome.tabs.insertCSS API 454 // that do support user stylesheets via the browser.tabs.insertCSS API
480 // (Firefox 53 onwards for now and possibly Chrome in the near future). 455 // (Firefox 53 onwards for now and possibly Chrome in the near future).
481 // Once all supported platforms have implemented this API, we can remove 456 // Once all supported platforms have implemented this API, we can remove
482 // the code below. See issue #5090. 457 // the code below. See issue #5090.
483 // Related Chrome and Firefox issues: 458 // Related Chrome and Firefox issues:
484 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 459 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009
485 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 460 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
486 this.addSelectorsInline(selectors, filters, true); 461 this.addSelectorsInline(selectors, "emulated");
Manish Jethani 2017/10/14 14:23:21 addSelectors is now only called for emulated selec
487 } 462 }
488 else 463 else
489 { 464 {
490 this.injectSelectors(selectors); 465 browser.runtime.sendMessage({
491 466 type: "elemhide.injectSelectors",
492 if (this.tracer && selectors.length > 0) 467 selectors,
493 this.tracer.addSelectors(selectors, filters); 468 groupName: "emulated"
494 } 469 });
470 }
471
472 if (this.tracer)
473 this.tracer.addSelectors(selectors, filters);
495 }, 474 },
496 475
497 hideElements(elements, filters) 476 hideElements(elements, filters)
498 { 477 {
499 for (let element of elements) 478 for (let element of elements)
500 hideElement(element); 479 hideElement(element);
501 480
502 if (this.tracer) 481 if (this.tracer)
503 { 482 {
504 chrome.runtime.sendMessage({ 483 browser.runtime.sendMessage({
505 type: "devtools.traceElemHide", 484 type: "devtools.traceElemHide",
506 selectors: [], 485 selectors: [],
507 filters 486 filters
508 }); 487 });
509 } 488 }
510 }, 489 },
511 490
512 apply() 491 apply()
513 { 492 {
514 chrome.runtime.sendMessage({type: "elemhide.getSelectors"}, response => 493 browser.runtime.sendMessage({type: "elemhide.getSelectors"}, response =>
515 { 494 {
516 if (this.tracer) 495 if (this.tracer)
517 this.tracer.disconnect(); 496 this.tracer.disconnect();
518 this.tracer = null; 497 this.tracer = null;
519 498
520 if (this.style && this.style.parentNode)
Manish Jethani 2017/10/14 14:23:22 This needs to be parentNode, not parentElement, be
Manish Jethani 2017/10/14 21:35:33 Actually, why is this needed? The styles here will
Manish Jethani 2017/10/15 21:29:12 OK, this code is called from composer.postload.js
521 this.style.parentNode.removeChild(this.style);
522 this.style = null;
523
524 if (response.trace) 499 if (response.trace)
525 this.tracer = new ElementHidingTracer(); 500 this.tracer = new ElementHidingTracer();
526 501
527 this.inline = response.inline; 502 this.inline = response.inline;
528 503
529 if (this.inline) 504 if (this.inline)
530 this.addSelectorsInline(response.selectors); 505 this.addSelectorsInline(response.selectors, "standard");
531 else if (this.tracer) 506
507 if (this.tracer)
532 this.tracer.addSelectors(response.selectors); 508 this.tracer.addSelectors(response.selectors);
533 509
534 this.elemHideEmulation.apply(response.emulatedPatterns); 510 this.elemHideEmulation.apply(response.emulatedPatterns);
535 }); 511 });
536 } 512 }
537 }; 513 };
538 514
539 if (document instanceof HTMLDocument) 515 if (document instanceof HTMLDocument)
540 { 516 {
541 checkSitekey(); 517 checkSitekey();
542 518
543 elemhide = new ElemHide(); 519 elemhide = new ElemHide();
544 elemhide.apply(); 520 elemhide.apply();
545 521
546 document.addEventListener("error", event => 522 document.addEventListener("error", event =>
547 { 523 {
548 checkCollapse(event.target); 524 checkCollapse(event.target);
549 }, true); 525 }, true);
550 526
551 document.addEventListener("load", event => 527 document.addEventListener("load", event =>
552 { 528 {
553 let element = event.target; 529 let element = event.target;
554 if (/^i?frame$/.test(element.localName)) 530 if (/^i?frame$/.test(element.localName))
555 checkCollapse(element); 531 checkCollapse(element);
556 }, true); 532 }, true);
557 } 533 }
534
535 window.checkCollapse = checkCollapse;
536 window.elemhide = elemhide;
537 window.typeMap = typeMap;
538 window.getURLsFromElement = getURLsFromElement;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld