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: Updated implementation Created Sept. 28, 2018, 1:23 p.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 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 }, 218 },
219 collapse => 219 collapse =>
220 { 220 {
221 if (collapse) 221 if (collapse)
222 { 222 {
223 if (selector) 223 if (selector)
224 { 224 {
225 if (!collapsingSelectors.has(selector)) 225 if (!collapsingSelectors.has(selector))
226 { 226 {
227 collapsingSelectors.add(selector); 227 collapsingSelectors.add(selector);
228 contentFiltering.addSelectors([selector], null, "collapsing", true); 228 contentFiltering.addSelectors([selector], "collapsing", true);
229 } 229 }
230 } 230 }
231 else 231 else
232 { 232 {
233 hideElement(element); 233 hideElement(element);
234 } 234 }
235 } 235 }
236 } 236 }
237 ); 237 );
238 } 238 }
(...skipping 12 matching lines...) Expand all
251 this.timeout = null; 251 this.timeout = null;
252 this.observer = new MutationObserver(this.observe.bind(this)); 252 this.observer = new MutationObserver(this.observe.bind(this));
253 this.trace = this.trace.bind(this); 253 this.trace = this.trace.bind(this);
254 254
255 if (document.readyState == "loading") 255 if (document.readyState == "loading")
256 document.addEventListener("DOMContentLoaded", this.trace); 256 document.addEventListener("DOMContentLoaded", this.trace);
257 else 257 else
258 this.trace(); 258 this.trace();
259 } 259 }
260 ElementHidingTracer.prototype = { 260 ElementHidingTracer.prototype = {
261 addSelectors(selectors, filters) 261 addSelectors(selectors)
262 { 262 {
263 let pairs = selectors.map((sel, i) => [sel, filters && filters[i]]);
264
265 if (document.readyState != "loading") 263 if (document.readyState != "loading")
266 this.checkNodes([document], pairs); 264 this.checkNodes([document], selectors);
267 265
268 this.selectors.push(...pairs); 266 this.selectors.push(...selectors);
269 }, 267 },
270 268
271 checkNodes(nodes, pairs) 269 checkNodes(nodes, selectors)
272 { 270 {
273 let selectors = []; 271 let effectiveSelectors = [];
274 let filters = []; 272
275 273 for (let selector of selectors)
276 for (let [selector, filter] of pairs)
277 { 274 {
278 nodes: for (let node of nodes) 275 nodes: for (let node of nodes)
279 { 276 {
280 for (let element of node.querySelectorAll(selector)) 277 for (let element of node.querySelectorAll(selector))
281 { 278 {
282 // Only consider selectors that actually have an effect on the 279 // Only consider selectors that actually have an effect on the
283 // computed styles, and aren't overridden by rules with higher 280 // computed styles, and aren't overridden by rules with higher
284 // priority, or haven't been circumvented in a different way. 281 // priority, or haven't been circumvented in a different way.
285 if (getComputedStyle(element).display == "none") 282 if (getComputedStyle(element).display == "none")
286 { 283 {
287 // For regular element hiding, we don't know the exact filter, 284 effectiveSelectors.push(selector);
288 // but the background page can find it with the given selector.
289 // In case of element hiding emulation, the generated selector
290 // we got here is different from the selector part of the filter,
291 // but in this case we can send the whole filter text instead.
292 if (filter)
293 filters.push(filter);
294 else
295 selectors.push(selector);
296
297 break nodes; 285 break nodes;
298 } 286 }
299 } 287 }
300 } 288 }
301 } 289 }
302 290
303 if (selectors.length > 0 || filters.length > 0) 291 if (effectiveSelectors.length > 0)
304 { 292 {
305 browser.runtime.sendMessage({ 293 browser.runtime.sendMessage({
306 type: "hitLogger.traceElemHide", 294 type: "hitLogger.traceElemHide",
307 selectors, filters 295 selectors: effectiveSelectors,
296 filters: []
308 }); 297 });
309 } 298 }
310 }, 299 },
311 300
312 onTimeout() 301 onTimeout()
313 { 302 {
314 this.checkNodes(this.changedNodes, this.selectors); 303 this.checkNodes(this.changedNodes, this.selectors);
315 this.changedNodes = []; 304 this.changedNodes = [];
316 this.timeout = null; 305 this.timeout = null;
317 }, 306 },
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 document.removeEventListener("DOMContentLoaded", this.trace); 379 document.removeEventListener("DOMContentLoaded", this.trace);
391 this.observer.disconnect(); 380 this.observer.disconnect();
392 clearTimeout(this.timeout); 381 clearTimeout(this.timeout);
393 } 382 }
394 }; 383 };
395 384
396 function ContentFiltering() 385 function ContentFiltering()
397 { 386 {
398 this.styles = new Map(); 387 this.styles = new Map();
399 this.tracer = null; 388 this.tracer = null;
400 this.inline = true; 389
401 390 this.elemHideEmulation = new ElemHideEmulation(this.hideElements.bind(this));
402 this.elemHideEmulation = new ElemHideEmulation(
403 () => {},
404 this.hideElements.bind(this)
405 );
406 } 391 }
407 ContentFiltering.prototype = { 392 ContentFiltering.prototype = {
408 selectorGroupSize: 1024, 393 addRulesInline(rules, groupName = "standard", appendOnly = false)
409
410 getStyleElement(groupName)
411 { 394 {
412 let style = this.styles.get(groupName); 395 let style = this.styles.get(groupName);
413 if (style) 396
414 return style; 397 if (style && !appendOnly)
415
416 // Create <style> element lazily, only if we add styles. Add it to
417 // the <head> or <html> element. If we have injected a style element
418 // before that has been removed (the sheet property is null), create a
419 // new one.
420 let style = document.createElement("style");
421 (document.head || document.documentElement).appendChild(style);
422
423 // It can happen that the frame already navigated to a different
424 // document while we were waiting for the background page to respond.
425 // In that case the sheet property may stay null, after adding the
426 // <style> element.
427 if (!style.sheet)
428 return null;
429
430 this.styles.set(groupName, style);
431
432 return style;
433 },
434
435 addStyleSheetInline(styleSheet, groupName = "standard")
436 {
437 let style = this.getStyleElement(groupName);
438 if (style)
439 style.textContent = styleSheet;
440 },
441
442 addSelectorsInline(selectors, groupName, appendOnly = false)
443 {
444 let style = this.getStyleElement(groupName);
445 if (!style)
446 return;
447
448 if (!appendOnly)
449 { 398 {
450 while (style.sheet.cssRules.length > 0) 399 while (style.sheet.cssRules.length > 0)
451 style.sheet.deleteRule(0); 400 style.sheet.deleteRule(0);
452 } 401 }
453 402
454 // Chromium's Blink engine supports only up to 8,192 simple selectors, and 403 if (rules.length == 0)
455 // even fewer compound selectors, in a rule. The exact number of selectors 404 return;
456 // that would work depends on their sizes (e.g. "#foo .bar" has a 405
457 // size of 2). Since we don't know the sizes of the selectors here, we 406 if (!style)
458 // simply split them into groups of 1,024, based on the reasonable 407 {
459 // assumption that the average selector won't have a size greater than 8. 408 // Create <style> element lazily, only if we add styles. Add it to
460 // The alternative would be to calculate the sizes of the selectors and 409 // the <head> or <html> element. If we have injected a style element
461 // divide them up accordingly, but this approach is more efficient and has 410 // before that has been removed (the sheet property is null), create a
462 // worked well in practice. In theory this could still lead to some 411 // new one.
463 // selectors not working on Chromium, but it is highly unlikely. 412 style = document.createElement("style");
464 // See issue #6298 and https://crbug.com/804179 413 (document.head || document.documentElement).appendChild(style);
465 for (let i = 0; i < selectors.length; i += this.selectorGroupSize) 414
Sebastian Noack 2018/09/28 15:33:41 What is this code still good for if stylesheets ar
Manish Jethani 2018/09/28 16:04:44 This is for element collapsing. If this.inline is
Sebastian Noack 2018/09/28 16:09:06 Please correct me if I'm wrong, but wouldn't it si
Manish Jethani 2018/09/28 16:12:51 Yes, I agree. What's more is that it's going to be
Sebastian Noack 2018/09/28 16:32:50 Sounds good. Mind implementing those changes?
Manish Jethani 2018/09/28 20:20:23 I thought about this a little more. It's always go
466 { 415 // It can happen that the frame already navigated to a different
467 let selector = selectors.slice(i, i + this.selectorGroupSize).join(", "); 416 // document while we were waiting for the background page to respond.
468 style.sheet.insertRule(selector + "{display: none !important;}", 417 // In that case the sheet property may stay null, after adding the
469 style.sheet.cssRules.length); 418 // <style> element.
470 } 419 if (!style.sheet)
471 }, 420 return;
472 421
473 addSelectors(selectors, filters, groupName = "emulated", appendOnly = false) 422 this.styles.set(groupName, style);
474 { 423 }
475 if (this.inline) 424
476 { 425 for (let rule of rules)
477 // Insert the style rules inline if we have been instructed by the 426 style.sheet.insertRule(rule, style.sheet.cssRules.length);
478 // background page to do so. This is usually the case, except on platforms 427 },
479 // that do support user stylesheets via the browser.tabs.insertCSS API 428
480 // (Firefox 53 onwards for now and possibly Chrome in the near future). 429 addSelectors(selectors, groupName = "standard", appendOnly = false)
481 // Once all supported platforms have implemented this API, we can remove 430 {
482 // the code below. See issue #5090. 431 browser.runtime.sendMessage({
483 // Related Chrome and Firefox issues: 432 type: "content.injectSelectors",
484 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009 433 selectors,
485 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026 434 groupName,
486 this.addSelectorsInline(selectors, groupName, appendOnly); 435 appendOnly
487 } 436 },
488 else 437 rules =>
489 { 438 {
490 browser.runtime.sendMessage({ 439 if (rules)
491 type: "elemhide.injectSelectors", 440 {
492 selectors, 441 // Insert the rules inline if we have been instructed by the background
493 groupName, 442 // page to do so. This is rarely the case, except on platforms that do
494 appendOnly 443 // not support user stylesheets via the browser.tabs.insertCSS API
495 }); 444 // (Firefox <53, Chrome <66, and Edge).
496 } 445 // Once all supported platforms have implemented this API, we can remove
497 446 // the code below. See issue #5090.
498 // Only trace selectors that are based directly on hiding filters 447 // Related Chrome and Firefox issues:
499 // (i.e. leave out collapsing selectors). 448 // https://bugs.chromium.org/p/chromium/issues/detail?id=632009
500 if (this.tracer && groupName != "collapsing") 449 // https://bugzilla.mozilla.org/show_bug.cgi?id=1310026
501 this.tracer.addSelectors(selectors, filters); 450 this.addRulesInline(rules, groupName, appendOnly);
451 }
452 });
502 }, 453 },
503 454
504 hideElements(elements, filters) 455 hideElements(elements, filters)
505 { 456 {
506 for (let element of elements) 457 for (let element of elements)
507 hideElement(element); 458 hideElement(element);
508 459
509 if (this.tracer) 460 if (this.tracer)
510 { 461 {
511 browser.runtime.sendMessage({ 462 browser.runtime.sendMessage({
(...skipping 12 matching lines...) Expand all
524 }, 475 },
525 response => 476 response =>
526 { 477 {
527 if (this.tracer) 478 if (this.tracer)
528 this.tracer.disconnect(); 479 this.tracer.disconnect();
529 this.tracer = null; 480 this.tracer = null;
530 481
531 if (response.trace) 482 if (response.trace)
532 this.tracer = new ElementHidingTracer(); 483 this.tracer = new ElementHidingTracer();
533 484
534 this.inline = response.inline; 485 if (response.inline)
535 486 this.addRulesInline(response.rules);
536 if (this.inline)
537 this.addStyleSheetInline(response.styleSheet);
538 487
539 if (this.tracer) 488 if (this.tracer)
540 this.tracer.addSelectors(response.selectors); 489 this.tracer.addSelectors(response.selectors);
541 490
542 this.elemHideEmulation.apply(response.emulatedPatterns); 491 this.elemHideEmulation.apply(response.emulatedPatterns);
543 }); 492 });
544 } 493 }
545 }; 494 };
546 495
547 if (document instanceof HTMLDocument) 496 if (document instanceof HTMLDocument)
(...skipping 13 matching lines...) Expand all
561 let element = event.target; 510 let element = event.target;
562 if (/^i?frame$/.test(element.localName)) 511 if (/^i?frame$/.test(element.localName))
563 checkCollapse(element); 512 checkCollapse(element);
564 }, true); 513 }, true);
565 } 514 }
566 515
567 window.checkCollapse = checkCollapse; 516 window.checkCollapse = checkCollapse;
568 window.contentFiltering = contentFiltering; 517 window.contentFiltering = contentFiltering;
569 window.typeMap = typeMap; 518 window.typeMap = typeMap;
570 window.getURLsFromElement = getURLsFromElement; 519 window.getURLsFromElement = getURLsFromElement;
LEFTRIGHT

Powered by Google App Engine
This is Rietveld