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

Side by Side Diff: include.preload.js

Issue 29349024: Issue 4298 - Enforce 'display: none' for ElemHide (Closed)
Patch Set: Block access to rules instead of attempting to enforce rule styles Created Aug. 5, 2016, 10:59 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 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 return null; 340 return null;
341 341
342 var rules = style.sheet.rules; 342 var rules = style.sheet.rules;
343 var parentNode = style.parentNode; 343 var parentNode = style.parentNode;
344 var observer = new MutationObserver(function() 344 var observer = new MutationObserver(function()
345 { 345 {
346 if (style.parentNode != parentNode) 346 if (style.parentNode != parentNode)
347 { 347 {
348 parentNode.appendChild(style); 348 parentNode.appendChild(style);
349 349
350 // Our style element's sheet is sometimes lost when the element is removed
351 // from the DOM. A new empty sheet is then created in its place so we have
352 // to restore the rules ourselves.
350 if (style.sheet.rules.length == 0) 353 if (style.sheet.rules.length == 0)
351 { 354 {
352 for (var i = 0; i < rules.length; i += 1) 355 for (var i = 0; i < rules.length; i += 1)
353 style.sheet.addRule(rules[i].selectorText, "display: none !important;" ); 356 style.sheet.addRule(rules[i].selectorText, "display: none !important;" );
354 357
355 style.id = id; 358 style.id = id;
356 injectJS( 359 injectJS(
357 function(id) 360 function(id)
358 { 361 {
359 var style = document.getElementById(id) || 362 var style = document.getElementById(id) ||
360 document.documentElement.shadowRoot.getElementById(id); 363 document.documentElement.shadowRoot.getElementById(id);
361 style.removeAttribute("id"); 364 style.removeAttribute("id");
365
362 Object.defineProperty(style.sheet, "disabled", 366 Object.defineProperty(style.sheet, "disabled",
363 {value: false, enumerable: true}); 367 {value: false, enumerable: true});
364 }, id 368 }, id
365 ); 369 );
366 } 370 }
367 } 371 }
368 }); 372 });
369 373
370 observer.observe(parentNode, {childList: true}); 374 observer.observe(parentNode, {childList: true});
371 return observer; 375 return observer;
(...skipping 15 matching lines...) Expand all
387 function protectStyleSheet(document, style) 391 function protectStyleSheet(document, style)
388 { 392 {
389 style.id = id; 393 style.id = id;
390 394
391 var protector = function(id) 395 var protector = function(id)
392 { 396 {
393 var style = document.getElementById(id) || 397 var style = document.getElementById(id) ||
394 document.documentElement.shadowRoot.getElementById(id); 398 document.documentElement.shadowRoot.getElementById(id);
395 style.removeAttribute("id"); 399 style.removeAttribute("id");
396 400
397 var i; 401 Object.defineProperty(style, "disabled",
398 var disableables = [style, style.sheet]; 402 {value: false, enumerable: true});
399 for (i = 0; i < disableables.length; i += 1) 403 Object.defineProperty(style.sheet, "disabled",
400 Object.defineProperty(disableables[i], "disabled", 404 {value: false, enumerable: true});
401 {value: false, enumerable: true});
402 405
403 var methods = ["deleteRule", "removeRule"]; 406 function wrapMethods(methodNames, descKey, defaultReturnValue)
404 for (i = 0; i < methods.length; i += 1)
405 { 407 {
406 if (methods[i] in CSSStyleSheet.prototype) 408 for (var i = 0; i < methodNames.length; i += 1)
407 { 409 {
408 (function(method) 410 if (methodNames[i] in CSSStyleSheet.prototype)
409 { 411 {
410 var original = CSSStyleSheet.prototype[method]; 412 (function(method)
411 CSSStyleSheet.prototype[method] = function(index)
412 { 413 {
413 if (this != style.sheet) 414 var desc = Object.getOwnPropertyDescriptor(CSSStyleSheet.prototype,
414 original.call(this, index); 415 method);
415 }; 416 var original = desc[descKey];
416 }(methods[i])); 417 desc[descKey] = function()
418 {
419 if (this != style.sheet)
420 return original.apply(this, arguments);
421 return defaultReturnValue;
422 };
423 Object.defineProperty(CSSStyleSheet.prototype, method, desc);
424 }(methodNames[i]));
425 }
417 } 426 }
418 } 427 }
428
429 wrapMethods(["deleteRule", "removeRule"], "value");
430 wrapMethods(["rules", "cssRules"], "get", []);
419 }; 431 };
420 432
421 injectJS(protector, id); 433 injectJS(protector, id);
422 } 434 }
423 435
424 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore 436 // Neither Chrome[1] nor Safari allow us to intercept WebSockets, and therefore
425 // some ad networks are misusing them as a way to serve adverts and circumvent 437 // some ad networks are misusing them as a way to serve adverts and circumvent
426 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket 438 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket
427 // connections from being opened. 439 // connections from being opened.
428 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 440 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 }, true); 697 }, true);
686 698
687 return updateStylesheet; 699 return updateStylesheet;
688 } 700 }
689 701
690 if (document instanceof HTMLDocument) 702 if (document instanceof HTMLDocument)
691 { 703 {
692 checkSitekey(); 704 checkSitekey();
693 window.updateStylesheet = init(document); 705 window.updateStylesheet = init(document);
694 } 706 }
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