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: Rebased. Created Aug. 11, 2016, 11:03 a.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
347 function reinjectStyleSheetWhenRemoved(document, style)
348 {
349 if (!MutationObserver)
350 return null;
351
352 var rules = style.sheet.rules;
353 var parentNode = style.parentNode;
354 var observer = new MutationObserver(function()
355 {
356 if (style.parentNode != parentNode)
357 {
358 parentNode.appendChild(style);
359
360 if (style.sheet.rules.length == 0)
361 {
362 for (var i = 0; i < rules.length; i++)
363 style.sheet.addRule(rules[i].selectorText, "display: none !important;" );
364
365 style.id = id;
366 runInPage(
367 function(id)
368 {
369 var style = document.getElementById(id) ||
370 document.documentElement.shadowRoot.getElementById(id);
371 style.removeAttribute("id");
372 Object.defineProperty(style.sheet, "disabled",
373 {value: false, enumerable: true});
374 }, id
375 );
376 }
377 }
378 });
379
380 observer.observe(parentNode, {childList: true});
381 return observer;
382 }
383
363 function protectStyleSheet(document, style) 384 function protectStyleSheet(document, style)
364 { 385 {
365 style.id = id; 386 style.id = id;
366 387
367 runInPage(function(id) 388 runInPage(function(id)
368 { 389 {
369 var style = document.getElementById(id) || 390 var style = document.getElementById(id) ||
370 document.documentElement.shadowRoot.getElementById(id); 391 document.documentElement.shadowRoot.getElementById(id);
371 style.removeAttribute("id"); 392 style.removeAttribute("id");
372 393
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
639 }, true); 660 }, true);
640 661
641 return updateStylesheet; 662 return updateStylesheet;
642 } 663 }
643 664
644 if (document instanceof HTMLDocument) 665 if (document instanceof HTMLDocument)
645 { 666 {
646 checkSitekey(); 667 checkSitekey();
647 window.updateStylesheet = init(document); 668 window.updateStylesheet = init(document);
648 } 669 }
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