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

Unified Diff: include.preload.js

Issue 29348917: Issue 4191 - Restore rules for reinjected stylesheets (Closed)
Patch Set: Addressed nits Created Aug. 11, 2016, 12:47 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include.preload.js
diff --git a/include.preload.js b/include.preload.js
index f4ab60be781c88c27079d7df6f1c4e77a430188d..53931974c6135a833dd72bc93959fd5410319e19 100644
--- a/include.preload.js
+++ b/include.preload.js
@@ -17,7 +17,6 @@
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var SELECTOR_GROUP_SIZE = 200;
-var id = Math.random().toString(36).substr(2);
var typeMap = {
"img": "IMAGE",
@@ -334,22 +333,6 @@ ElementHidingTracer.prototype = {
}
};
-function reinjectStyleSheetWhenRemoved(document, style)
-{
- if (!MutationObserver)
- return null;
-
- var parentNode = style.parentNode;
- var observer = new MutationObserver(function()
- {
- if (style.parentNode != parentNode)
- parentNode.appendChild(style);
- });
-
- observer.observe(parentNode, {childList: true});
- return observer;
-}
-
function runInPage(fn, arg)
{
var script = document.createElement("script");
@@ -360,33 +343,6 @@ function runInPage(fn, arg)
document.documentElement.removeChild(script);
}
-function protectStyleSheet(document, style)
-{
- style.id = id;
-
- runInPage(function(id)
- {
- var style = document.getElementById(id) ||
- document.documentElement.shadowRoot.getElementById(id);
- style.removeAttribute("id");
-
- var disableables = [style, style.sheet];
- for (var i = 0; i < disableables.length; i++)
- Object.defineProperty(disableables[i], "disabled",
- {value: false, enumerable: true});
-
- ["deleteRule", "removeRule"].forEach(function(method)
- {
- var original = CSSStyleSheet.prototype[method];
- CSSStyleSheet.prototype[method] = function(index)
- {
- if (this != style.sheet)
- original.call(this, index);
- };
- });
- }, id);
-}
-
// Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore
// some ad networks are misusing them as a way to serve adverts and circumvent
// us. As a workaround we wrap WebSocket, preventing blocked WebSocket
@@ -397,7 +353,7 @@ function wrapWebSocket()
if (typeof WebSocket == "undefined")
return;
- var eventName = "abpws-" + id;
+ var eventName = "abpws-" + Math.random().toString(36).substr(2);
document.addEventListener(eventName, function(event)
{
@@ -499,6 +455,22 @@ function init(document)
{
shadow = document.documentElement.createShadowRoot();
shadow.appendChild(document.createElement("shadow"));
+
+ // Stop the website from messing with our shadowRoot
+ runInPage(function()
+ {
+ var ourShadowRoot = document.documentElement.shadowRoot;
+ var desc = Object.getOwnPropertyDescriptor(Element.prototype, "shadowRoot");
+ var shadowRoot = Function.prototype.call.bind(desc.get);
+
+ Object.defineProperty(Element.prototype, "shadowRoot", {
+ conifgurable: true, enumerable: true, get: function()
+ {
+ var shadow = shadowRoot(this);
+ return shadow == ourShadowRoot ? null : shadow;
+ }
+ });
+ }, null);
}
function addElemHideSelectors(selectors)
@@ -521,9 +493,6 @@ function init(document)
// <style> element to the shadow DOM.
if (!style.sheet)
return;
-
- observer = reinjectStyleSheetWhenRemoved(document, style);
- protectStyleSheet(document, style);
}
// If using shadow DOM, we have to add the ::content pseudo-element
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld