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: Prevent access to our shadowRoot Created Aug. 11, 2016, 12:23 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 | no next file » | 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
(...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 }, 327 },
328 328
329 disconnect: function() 329 disconnect: function()
330 { 330 {
331 this.document.removeEventListener("DOMContentLoaded", this.trace); 331 this.document.removeEventListener("DOMContentLoaded", this.trace);
332 this.observer.disconnect(); 332 this.observer.disconnect();
333 clearTimeout(this.timeout); 333 clearTimeout(this.timeout);
334 } 334 }
335 }; 335 };
336 336
337 function reinjectStyleSheetWhenRemoved(document, style)
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) 337 function runInPage(fn, arg)
354 { 338 {
355 var script = document.createElement("script"); 339 var script = document.createElement("script");
356 script.type = "application/javascript"; 340 script.type = "application/javascript";
357 script.async = false; 341 script.async = false;
358 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; 342 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");";
359 document.documentElement.appendChild(script); 343 document.documentElement.appendChild(script);
360 document.documentElement.removeChild(script); 344 document.documentElement.removeChild(script);
361 } 345 }
362 346
363 function protectStyleSheet(document, style)
364 {
365 style.id = id;
Sebastian Noack 2016/08/11 12:38:57 Since the id is only used once (for the WebSocket)
kzar 2016/08/11 12:48:18 Done.
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 347 // 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 348 // 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 349 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket
393 // connections from being opened. 350 // connections from being opened.
394 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 351 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353
395 function wrapWebSocket() 352 function wrapWebSocket()
396 { 353 {
397 if (typeof WebSocket == "undefined") 354 if (typeof WebSocket == "undefined")
398 return; 355 return;
399 356
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 // However, creating a shadow root breaks running CSS transitions. So we 449 // However, creating a shadow root breaks running CSS transitions. So we
493 // have to create the shadow root before transistions might start (#452). 450 // have to create the shadow root before transistions might start (#452).
494 // 451 //
495 // Also, using shadow DOM causes issues on some Google websites, 452 // Also, using shadow DOM causes issues on some Google websites,
496 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). 453 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687).
497 if ("createShadowRoot" in document.documentElement && 454 if ("createShadowRoot" in document.documentElement &&
498 !/\.(?:google|blogger)\.com$/.test(document.domain)) 455 !/\.(?:google|blogger)\.com$/.test(document.domain))
499 { 456 {
500 shadow = document.documentElement.createShadowRoot(); 457 shadow = document.documentElement.createShadowRoot();
501 shadow.appendChild(document.createElement("shadow")); 458 shadow.appendChild(document.createElement("shadow"));
459
460 // Stop the website from messing with our shadowRoot
461 runInPage(function()
Sebastian Noack 2016/08/11 12:48:11 You have to pass the document to runInPage(), to m
kzar 2016/08/11 13:04:46 Done.
462 {
463 var ourShadowRoot = document.documentElement.shadowRoot;
464 var desc = Object.getOwnPropertyDescriptor(Element.prototype, "shadowRoot" );
465 var shadowRoot = Function.prototype.call.bind(desc.get);
466
467 Object.defineProperty(Element.prototype, "shadowRoot", {
468 conifgurable: true, enumerable: true, get: function()
469 {
470 var shadow = shadowRoot(this);
471 return shadow === ourShadowRoot ? null : shadow;
Sebastian Noack 2016/08/11 12:38:57 Nit: As per Mozilla's coding practices, we prefer
Sebastian Noack 2016/08/11 12:42:37 (The other way around, I meant, == is preferred ov
kzar 2016/08/11 12:48:18 Done.
472 }
473 });
474 }, "");
Sebastian Noack 2016/08/11 12:38:57 Nit: I'd rather pass null, than creating an empty
kzar 2016/08/11 12:48:18 Done.
502 } 475 }
503 476
504 function addElemHideSelectors(selectors) 477 function addElemHideSelectors(selectors)
505 { 478 {
506 if (selectors.length == 0) 479 if (selectors.length == 0)
507 return; 480 return;
508 481
509 if (!style) 482 if (!style)
510 { 483 {
511 // Create <style> element lazily, only if we add styles. Add it to 484 // Create <style> element lazily, only if we add styles. Add it to
512 // the shadow DOM if possible. Otherwise fallback to the <head> or 485 // the shadow DOM if possible. Otherwise fallback to the <head> or
513 // <html> element. If we have injected a style element before that 486 // <html> element. If we have injected a style element before that
514 // has been removed (the sheet property is null), create a new one. 487 // has been removed (the sheet property is null), create a new one.
515 style = document.createElement("style"); 488 style = document.createElement("style");
516 (shadow || document.head || document.documentElement).appendChild(style); 489 (shadow || document.head || document.documentElement).appendChild(style);
517 490
518 // It can happen that the frame already navigated to a different 491 // It can happen that the frame already navigated to a different
519 // document while we were waiting for the background page to respond. 492 // document while we were waiting for the background page to respond.
520 // In that case the sheet property will stay null, after addind the 493 // In that case the sheet property will stay null, after addind the
521 // <style> element to the shadow DOM. 494 // <style> element to the shadow DOM.
522 if (!style.sheet) 495 if (!style.sheet)
523 return; 496 return;
524
525 observer = reinjectStyleSheetWhenRemoved(document, style);
526 protectStyleSheet(document, style);
527 } 497 }
528 498
529 // If using shadow DOM, we have to add the ::content pseudo-element 499 // If using shadow DOM, we have to add the ::content pseudo-element
530 // before each selector, in order to match elements within the 500 // before each selector, in order to match elements within the
531 // insertion point. 501 // insertion point.
532 if (shadow) 502 if (shadow)
533 { 503 {
534 var preparedSelectors = []; 504 var preparedSelectors = [];
535 for (var i = 0; i < selectors.length; i++) 505 for (var i = 0; i < selectors.length; i++)
536 { 506 {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 }, true); 609 }, true);
640 610
641 return updateStylesheet; 611 return updateStylesheet;
642 } 612 }
643 613
644 if (document instanceof HTMLDocument) 614 if (document instanceof HTMLDocument)
645 { 615 {
646 checkSitekey(); 616 checkSitekey();
647 window.updateStylesheet = init(document); 617 window.updateStylesheet = init(document);
648 } 618 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld