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

Side by Side Diff: include.preload.js

Issue 29348917: Issue 4191 - Restore rules for reinjected stylesheets (Closed)
Patch Set: runInDocument Created Aug. 11, 2016, 1:03 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 | safari/include.youtube.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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver; 18 var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
19 var SELECTOR_GROUP_SIZE = 200; 19 var SELECTOR_GROUP_SIZE = 200;
20 var id = Math.random().toString(36).substr(2);
21 20
22 var typeMap = { 21 var typeMap = {
23 "img": "IMAGE", 22 "img": "IMAGE",
24 "input": "IMAGE", 23 "input": "IMAGE",
25 "picture": "IMAGE", 24 "picture": "IMAGE",
26 "audio": "MEDIA", 25 "audio": "MEDIA",
27 "video": "MEDIA", 26 "video": "MEDIA",
28 "frame": "SUBDOCUMENT", 27 "frame": "SUBDOCUMENT",
29 "iframe": "SUBDOCUMENT", 28 "iframe": "SUBDOCUMENT",
30 "object": "OBJECT", 29 "object": "OBJECT",
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 }, 326 },
328 327
329 disconnect: function() 328 disconnect: function()
330 { 329 {
331 this.document.removeEventListener("DOMContentLoaded", this.trace); 330 this.document.removeEventListener("DOMContentLoaded", this.trace);
332 this.observer.disconnect(); 331 this.observer.disconnect();
333 clearTimeout(this.timeout); 332 clearTimeout(this.timeout);
334 } 333 }
335 }; 334 };
336 335
337 function reinjectStyleSheetWhenRemoved(document, style) 336 function runInDocument(document, fn, arg)
338 {
339 if (!MutationObserver)
340 return null;
341
342 var parentNode = style.parentNode;
343 var observer = new MutationObserver(function()
344 {
345 if (style.parentNode != parentNode)
346 parentNode.appendChild(style);
347 });
348
349 observer.observe(parentNode, {childList: true});
350 return observer;
351 }
352
353 function runInPage(fn, arg)
354 { 337 {
355 var script = document.createElement("script"); 338 var script = document.createElement("script");
356 script.type = "application/javascript"; 339 script.type = "application/javascript";
357 script.async = false; 340 script.async = false;
358 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; 341 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");";
359 document.documentElement.appendChild(script); 342 document.documentElement.appendChild(script);
360 document.documentElement.removeChild(script); 343 document.documentElement.removeChild(script);
361 } 344 }
362 345
363 function protectStyleSheet(document, style)
364 {
365 style.id = id;
366
367 runInPage(function(id)
368 {
369 var style = document.getElementById(id) ||
370 document.documentElement.shadowRoot.getElementById(id);
371 style.removeAttribute("id");
372
373 var disableables = [style, style.sheet];
374 for (var i = 0; i < disableables.length; i++)
375 Object.defineProperty(disableables[i], "disabled",
376 {value: false, enumerable: true});
377
378 ["deleteRule", "removeRule"].forEach(function(method)
379 {
380 var original = CSSStyleSheet.prototype[method];
381 CSSStyleSheet.prototype[method] = function(index)
382 {
383 if (this != style.sheet)
384 original.call(this, index);
385 };
386 });
387 }, id);
388 }
389
390 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore 346 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore
391 // some ad networks are misusing them as a way to serve adverts and circumvent 347 // some ad networks are misusing them as a way to serve adverts and circumvent
392 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket 348 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket
393 // connections from being opened. 349 // connections from being opened.
394 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 350 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353
395 function wrapWebSocket() 351 function wrapWebSocket(document)
396 { 352 {
397 if (typeof WebSocket == "undefined") 353 if (typeof WebSocket == "undefined")
398 return; 354 return;
399 355
400 var eventName = "abpws-" + id; 356 var eventName = "abpws-" + Math.random().toString(36).substr(2);
401 357
402 document.addEventListener(eventName, function(event) 358 document.addEventListener(eventName, function(event)
403 { 359 {
404 ext.backgroundPage.sendMessage({ 360 ext.backgroundPage.sendMessage({
405 type: "websocket-request", 361 type: "websocket-request",
406 url: event.detail.url 362 url: event.detail.url
407 }, function (block) 363 }, function (block)
408 { 364 {
409 document.dispatchEvent( 365 document.dispatchEvent(
410 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) 366 new CustomEvent(eventName + "-" + event.detail.url, {detail: block})
411 ); 367 );
412 }); 368 });
413 }); 369 });
414 370
415 runInPage(function(eventName) 371 runInDocument(document, function(eventName)
416 { 372 {
417 // As far as possible we must track everything we use that could be 373 // As far as possible we must track everything we use that could be
418 // sabotaged by the website later in order to circumvent us. 374 // sabotaged by the website later in order to circumvent us.
419 var RealWebSocket = WebSocket; 375 var RealWebSocket = WebSocket;
420 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose); 376 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose);
421 var addEventListener = document.addEventListener.bind(document); 377 var addEventListener = document.addEventListener.bind(document);
422 var removeEventListener = document.removeEventListener.bind(document); 378 var removeEventListener = document.removeEventListener.bind(document);
423 var dispatchEvent = document.dispatchEvent.bind(document); 379 var dispatchEvent = document.dispatchEvent.bind(document);
424 var CustomEvent = window.CustomEvent; 380 var CustomEvent = window.CustomEvent;
425 381
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 }, eventName); 423 }, eventName);
468 } 424 }
469 425
470 function init(document) 426 function init(document)
471 { 427 {
472 var shadow = null; 428 var shadow = null;
473 var style = null; 429 var style = null;
474 var observer = null; 430 var observer = null;
475 var tracer = null; 431 var tracer = null;
476 432
477 wrapWebSocket(); 433 wrapWebSocket(document);
478 434
479 function getPropertyFilters(callback) 435 function getPropertyFilters(callback)
480 { 436 {
481 ext.backgroundPage.sendMessage({ 437 ext.backgroundPage.sendMessage({
482 type: "filters.get", 438 type: "filters.get",
483 what: "cssproperties" 439 what: "cssproperties"
484 }, callback); 440 }, callback);
485 } 441 }
486 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, 442 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters,
487 addElemHideSelectors); 443 addElemHideSelectors);
488 444
489 // Use Shadow DOM if available to don't mess with web pages that rely on 445 // Use Shadow DOM if available to don't mess with web pages that rely on
490 // the order of their own <style> tags (#309). 446 // the order of their own <style> tags (#309).
491 // 447 //
492 // However, creating a shadow root breaks running CSS transitions. So we 448 // However, creating a shadow root breaks running CSS transitions. So we
493 // have to create the shadow root before transistions might start (#452). 449 // have to create the shadow root before transistions might start (#452).
494 // 450 //
495 // Also, using shadow DOM causes issues on some Google websites, 451 // Also, using shadow DOM causes issues on some Google websites,
496 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). 452 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687).
497 if ("createShadowRoot" in document.documentElement && 453 if ("createShadowRoot" in document.documentElement &&
498 !/\.(?:google|blogger)\.com$/.test(document.domain)) 454 !/\.(?:google|blogger)\.com$/.test(document.domain))
499 { 455 {
500 shadow = document.documentElement.createShadowRoot(); 456 shadow = document.documentElement.createShadowRoot();
501 shadow.appendChild(document.createElement("shadow")); 457 shadow.appendChild(document.createElement("shadow"));
458
459 // Stop the website from messing with our shadowRoot
460 runInDocument(document, function()
461 {
462 var ourShadowRoot = document.documentElement.shadowRoot;
463 var desc = Object.getOwnPropertyDescriptor(Element.prototype, "shadowRoot" );
464 var shadowRoot = Function.prototype.call.bind(desc.get);
465
466 Object.defineProperty(Element.prototype, "shadowRoot", {
467 conifgurable: true, enumerable: true, get: function()
468 {
469 var shadow = shadowRoot(this);
470 return shadow == ourShadowRoot ? null : shadow;
471 }
472 });
473 }, null);
502 } 474 }
503 475
504 function addElemHideSelectors(selectors) 476 function addElemHideSelectors(selectors)
505 { 477 {
506 if (selectors.length == 0) 478 if (selectors.length == 0)
507 return; 479 return;
508 480
509 if (!style) 481 if (!style)
510 { 482 {
511 // Create <style> element lazily, only if we add styles. Add it to 483 // Create <style> element lazily, only if we add styles. Add it to
512 // the shadow DOM if possible. Otherwise fallback to the <head> or 484 // the shadow DOM if possible. Otherwise fallback to the <head> or
513 // <html> element. If we have injected a style element before that 485 // <html> element. If we have injected a style element before that
514 // has been removed (the sheet property is null), create a new one. 486 // has been removed (the sheet property is null), create a new one.
515 style = document.createElement("style"); 487 style = document.createElement("style");
516 (shadow || document.head || document.documentElement).appendChild(style); 488 (shadow || document.head || document.documentElement).appendChild(style);
517 489
518 // It can happen that the frame already navigated to a different 490 // It can happen that the frame already navigated to a different
519 // document while we were waiting for the background page to respond. 491 // document while we were waiting for the background page to respond.
520 // In that case the sheet property will stay null, after addind the 492 // In that case the sheet property will stay null, after addind the
521 // <style> element to the shadow DOM. 493 // <style> element to the shadow DOM.
522 if (!style.sheet) 494 if (!style.sheet)
523 return; 495 return;
524
525 observer = reinjectStyleSheetWhenRemoved(document, style);
526 protectStyleSheet(document, style);
527 } 496 }
528 497
529 // If using shadow DOM, we have to add the ::content pseudo-element 498 // If using shadow DOM, we have to add the ::content pseudo-element
530 // before each selector, in order to match elements within the 499 // before each selector, in order to match elements within the
531 // insertion point. 500 // insertion point.
532 if (shadow) 501 if (shadow)
533 { 502 {
534 var preparedSelectors = []; 503 var preparedSelectors = [];
535 for (var i = 0; i < selectors.length; i++) 504 for (var i = 0; i < selectors.length; i++)
536 { 505 {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 }, true); 608 }, true);
640 609
641 return updateStylesheet; 610 return updateStylesheet;
642 } 611 }
643 612
644 if (document instanceof HTMLDocument) 613 if (document instanceof HTMLDocument)
645 { 614 {
646 checkSitekey(); 615 checkSitekey();
647 window.updateStylesheet = init(document); 616 window.updateStylesheet = init(document);
648 } 617 }
OLDNEW
« no previous file with comments | « no previous file | safari/include.youtube.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld