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

Delta Between Two Patch Sets: include.preload.js

Issue 29348917: Issue 4191 - Restore rules for reinjected stylesheets (Closed)
Left Patch Set: Addressed nits Created Aug. 11, 2016, 12:47 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | safari/include.youtube.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 315 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 }, 326 },
327 327
328 disconnect: function() 328 disconnect: function()
329 { 329 {
330 this.document.removeEventListener("DOMContentLoaded", this.trace); 330 this.document.removeEventListener("DOMContentLoaded", this.trace);
331 this.observer.disconnect(); 331 this.observer.disconnect();
332 clearTimeout(this.timeout); 332 clearTimeout(this.timeout);
333 } 333 }
334 }; 334 };
335 335
336 function runInPage(fn, arg) 336 function runInDocument(document, fn, arg)
337 { 337 {
338 var script = document.createElement("script"); 338 var script = document.createElement("script");
339 script.type = "application/javascript"; 339 script.type = "application/javascript";
340 script.async = false; 340 script.async = false;
341 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; 341 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");";
342 document.documentElement.appendChild(script); 342 document.documentElement.appendChild(script);
343 document.documentElement.removeChild(script); 343 document.documentElement.removeChild(script);
344 } 344 }
345 345
346 // 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
347 // 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
348 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket 348 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket
349 // connections from being opened. 349 // connections from being opened.
350 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 350 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353
351 function wrapWebSocket() 351 function wrapWebSocket(document)
352 { 352 {
353 if (typeof WebSocket == "undefined") 353 if (typeof WebSocket == "undefined")
354 return; 354 return;
355 355
356 var eventName = "abpws-" + Math.random().toString(36).substr(2); 356 var eventName = "abpws-" + Math.random().toString(36).substr(2);
357 357
358 document.addEventListener(eventName, function(event) 358 document.addEventListener(eventName, function(event)
359 { 359 {
360 ext.backgroundPage.sendMessage({ 360 ext.backgroundPage.sendMessage({
361 type: "websocket-request", 361 type: "websocket-request",
362 url: event.detail.url 362 url: event.detail.url
363 }, function (block) 363 }, function (block)
364 { 364 {
365 document.dispatchEvent( 365 document.dispatchEvent(
366 new CustomEvent(eventName + "-" + event.detail.url, {detail: block}) 366 new CustomEvent(eventName + "-" + event.detail.url, {detail: block})
367 ); 367 );
368 }); 368 });
369 }); 369 });
370 370
371 runInPage(function(eventName) 371 runInDocument(document, function(eventName)
372 { 372 {
373 // 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
374 // sabotaged by the website later in order to circumvent us. 374 // sabotaged by the website later in order to circumvent us.
375 var RealWebSocket = WebSocket; 375 var RealWebSocket = WebSocket;
376 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose); 376 var closeWebSocket = Function.prototype.call.bind(RealWebSocket.prototype.cl ose);
377 var addEventListener = document.addEventListener.bind(document); 377 var addEventListener = document.addEventListener.bind(document);
378 var removeEventListener = document.removeEventListener.bind(document); 378 var removeEventListener = document.removeEventListener.bind(document);
379 var dispatchEvent = document.dispatchEvent.bind(document); 379 var dispatchEvent = document.dispatchEvent.bind(document);
380 var CustomEvent = window.CustomEvent; 380 var CustomEvent = window.CustomEvent;
381 381
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 }, eventName); 423 }, eventName);
424 } 424 }
425 425
426 function init(document) 426 function init(document)
427 { 427 {
428 var shadow = null; 428 var shadow = null;
429 var style = null; 429 var style = null;
430 var observer = null; 430 var observer = null;
431 var tracer = null; 431 var tracer = null;
432 432
433 wrapWebSocket(); 433 wrapWebSocket(document);
434 434
435 function getPropertyFilters(callback) 435 function getPropertyFilters(callback)
436 { 436 {
437 ext.backgroundPage.sendMessage({ 437 ext.backgroundPage.sendMessage({
438 type: "filters.get", 438 type: "filters.get",
439 what: "cssproperties" 439 what: "cssproperties"
440 }, callback); 440 }, callback);
441 } 441 }
442 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters, 442 var propertyFilters = new CSSPropertyFilters(window, getPropertyFilters,
443 addElemHideSelectors); 443 addElemHideSelectors);
444 444
445 // 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
446 // the order of their own <style> tags (#309). 446 // the order of their own <style> tags (#309).
447 // 447 //
448 // However, creating a shadow root breaks running CSS transitions. So we 448 // However, creating a shadow root breaks running CSS transitions. So we
449 // have to create the shadow root before transistions might start (#452). 449 // have to create the shadow root before transistions might start (#452).
450 // 450 //
451 // Also, using shadow DOM causes issues on some Google websites, 451 // Also, using shadow DOM causes issues on some Google websites,
452 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687). 452 // including Google Docs, Gmail and Blogger (#1770, #2602, #2687).
453 if ("createShadowRoot" in document.documentElement && 453 if ("createShadowRoot" in document.documentElement &&
454 !/\.(?:google|blogger)\.com$/.test(document.domain)) 454 !/\.(?:google|blogger)\.com$/.test(document.domain))
455 { 455 {
456 shadow = document.documentElement.createShadowRoot(); 456 shadow = document.documentElement.createShadowRoot();
457 shadow.appendChild(document.createElement("shadow")); 457 shadow.appendChild(document.createElement("shadow"));
458 458
459 // Stop the website from messing with our shadowRoot 459 // Stop the website from messing with our shadowRoot
460 runInPage(function() 460 runInDocument(document, function()
461 { 461 {
462 var ourShadowRoot = document.documentElement.shadowRoot; 462 var ourShadowRoot = document.documentElement.shadowRoot;
463 var desc = Object.getOwnPropertyDescriptor(Element.prototype, "shadowRoot" ); 463 var desc = Object.getOwnPropertyDescriptor(Element.prototype, "shadowRoot" );
464 var shadowRoot = Function.prototype.call.bind(desc.get); 464 var shadowRoot = Function.prototype.call.bind(desc.get);
465 465
466 Object.defineProperty(Element.prototype, "shadowRoot", { 466 Object.defineProperty(Element.prototype, "shadowRoot", {
467 conifgurable: true, enumerable: true, get: function() 467 conifgurable: true, enumerable: true, get: function()
468 { 468 {
469 var shadow = shadowRoot(this); 469 var shadow = shadowRoot(this);
470 return shadow == ourShadowRoot ? null : shadow; 470 return shadow == ourShadowRoot ? null : shadow;
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 }, true); 608 }, true);
609 609
610 return updateStylesheet; 610 return updateStylesheet;
611 } 611 }
612 612
613 if (document instanceof HTMLDocument) 613 if (document instanceof HTMLDocument)
614 { 614 {
615 checkSitekey(); 615 checkSitekey();
616 window.updateStylesheet = init(document); 616 window.updateStylesheet = init(document);
617 } 617 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld