| LEFT | RIGHT |
| 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-2017 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 |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 function runInPageContext(fn, arg) | 330 function runInPageContext(fn, arg) |
| 331 { | 331 { |
| 332 let script = document.createElement("script"); | 332 let script = document.createElement("script"); |
| 333 script.type = "application/javascript"; | 333 script.type = "application/javascript"; |
| 334 script.async = false; | 334 script.async = false; |
| 335 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; | 335 script.textContent = "(" + fn + ")(" + JSON.stringify(arg) + ");"; |
| 336 document.documentElement.appendChild(script); | 336 document.documentElement.appendChild(script); |
| 337 document.documentElement.removeChild(script); | 337 document.documentElement.removeChild(script); |
| 338 } | 338 } |
| 339 | 339 |
| 340 // Chrome doesn't allow us to intercept WebSockets[1], and therefore | 340 // Before Chrome 58 the webRequest API didn't allow us to intercept |
| 341 // some ad networks are misusing them as a way to serve adverts and circumvent | 341 // WebSockets[1], and therefore some ad networks are misusing them as a way to |
| 342 // us. As a workaround we wrap WebSocket, preventing blocked WebSocket | 342 // serve adverts and circumvent us. As a workaround we wrap WebSocket, |
| 343 // connections from being opened. | 343 // preventing blocked WebSocket connections from being opened. |
| 344 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 | 344 // [1] - https://bugs.chromium.org/p/chromium/issues/detail?id=129353 |
| 345 function wrapWebSocket() | 345 function wrapWebSocket() |
| 346 { | 346 { |
| 347 let randomEventName = "abpws-" + Math.random().toString(36).substr(2); | 347 let randomEventName = "abpws-" + Math.random().toString(36).substr(2); |
| 348 | 348 |
| 349 document.addEventListener(randomEventName, event => | 349 document.addEventListener(randomEventName, event => |
| 350 { | 350 { |
| 351 ext.backgroundPage.sendMessage({ | 351 ext.backgroundPage.sendMessage({ |
| 352 type: "request.websocket", | 352 type: "request.websocket", |
| 353 url: event.detail.url | 353 url: event.detail.url |
| (...skipping 30 matching lines...) Expand all Loading... |
| 384 | 384 |
| 385 dispatchEvent(new RealCustomEvent(eventName, {detail: {url}})); | 385 dispatchEvent(new RealCustomEvent(eventName, {detail: {url}})); |
| 386 } | 386 } |
| 387 | 387 |
| 388 function WrappedWebSocket(url, ...args) | 388 function WrappedWebSocket(url, ...args) |
| 389 { | 389 { |
| 390 // Throw correct exceptions if the constructor is used improperly. | 390 // Throw correct exceptions if the constructor is used improperly. |
| 391 if (!(this instanceof WrappedWebSocket)) return RealWebSocket(); | 391 if (!(this instanceof WrappedWebSocket)) return RealWebSocket(); |
| 392 if (arguments.length < 1) return new RealWebSocket(); | 392 if (arguments.length < 1) return new RealWebSocket(); |
| 393 | 393 |
| 394 let websocket; | 394 let websocket = new RealWebSocket(url, ...args); |
| 395 if (arguments.length == 1) | |
| 396 websocket = new RealWebSocket(url); | |
| 397 else | |
| 398 websocket = new RealWebSocket(url, args[0]); | |
| 399 | 395 |
| 400 checkRequest(websocket.url, blocked => | 396 checkRequest(websocket.url, blocked => |
| 401 { | 397 { |
| 402 if (blocked) | 398 if (blocked) |
| 403 closeWebSocket(websocket); | 399 closeWebSocket(websocket); |
| 404 }); | 400 }); |
| 405 | 401 |
| 406 return websocket; | 402 return websocket; |
| 407 } | 403 } |
| 408 WrappedWebSocket.prototype = RealWebSocket.prototype; | 404 WrappedWebSocket.prototype = RealWebSocket.prototype; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 if (!ourShadowRoot) | 466 if (!ourShadowRoot) |
| 471 return; | 467 return; |
| 472 let desc = Object.getOwnPropertyDescriptor(Element.prototype, | 468 let desc = Object.getOwnPropertyDescriptor(Element.prototype, |
| 473 "shadowRoot"); | 469 "shadowRoot"); |
| 474 let shadowRoot = Function.prototype.call.bind(desc.get); | 470 let shadowRoot = Function.prototype.call.bind(desc.get); |
| 475 | 471 |
| 476 Object.defineProperty(Element.prototype, "shadowRoot", { | 472 Object.defineProperty(Element.prototype, "shadowRoot", { |
| 477 configurable: true, enumerable: true, get() | 473 configurable: true, enumerable: true, get() |
| 478 { | 474 { |
| 479 let thisShadow = shadowRoot(this); | 475 let thisShadow = shadowRoot(this); |
| 480 return thisShadow == ourShadowRoot ? null : shadow; | 476 return thisShadow == ourShadowRoot ? null : thisShadow; |
| 481 } | 477 } |
| 482 }); | 478 }); |
| 483 }, null); | 479 }, null); |
| 484 } | 480 } |
| 485 | 481 |
| 486 return shadow; | 482 return shadow; |
| 487 }, | 483 }, |
| 488 | 484 |
| 489 addSelectors(selectors, filters) | 485 addSelectors(selectors, filters) |
| 490 { | 486 { |
| 491 if (selectors.length == 0) | 487 if (selectors.length == 0) |
| 492 return; | 488 return; |
| 493 | 489 |
| 494 if (!this.style) | 490 if (!this.style) |
| 495 { | 491 { |
| 496 // Create <style> element lazily, only if we add styles. Add it to | 492 // Create <style> element lazily, only if we add styles. Add it to |
| 497 // the shadow DOM if possible. Otherwise fallback to the <head> or | 493 // the shadow DOM if possible. Otherwise fallback to the <head> or |
| 498 // <html> element. If we have injected a style element before that | 494 // <html> element. If we have injected a style element before that |
| 499 // has been removed (the sheet property is null), create a new one. | 495 // has been removed (the sheet property is null), create a new one. |
| 500 this.style = document.createElement("style"); | 496 this.style = document.createElement("style"); |
| 501 (this.shadow || document.head || | 497 (this.shadow || document.head || |
| 502 document.documentElement).appendChild(this.style); | 498 document.documentElement).appendChild(this.style); |
| 503 | 499 |
| 504 // It can happen that the frame already navigated to a different | 500 // It can happen that the frame already navigated to a different |
| 505 // document while we were waiting for the background page to respond. | 501 // document while we were waiting for the background page to respond. |
| 506 // In that case the sheet property will stay null, after addind the | 502 // In that case the sheet property will stay null, after addind the |
| 507 // <style> element to the shadow DOM. | 503 // <style> element to the shadow DOM. |
| 508 if (!this.style.sheet) | 504 if (!this.style.sheet) |
| 509 return; | 505 return; |
| 510 } | 506 } |
| 511 | 507 |
| 512 // If using shadow DOM, we have to add the ::content pseudo-element | 508 // If using shadow DOM, we have to add the ::content pseudo-element |
| 513 // before each selector, in order to match elements within the | 509 // before each selector, in order to match elements within the |
| 514 // insertion point. | 510 // insertion point. |
| 515 let preparedSelectors = []; | 511 let preparedSelectors = []; |
| 516 if (this.shadow) | 512 if (this.shadow) |
| 517 { | 513 { |
| 518 for (let selector of selectors) | 514 for (let selector of selectors) |
| 519 { | 515 { |
| 520 let subSelectors = splitSelector(selector); | 516 let subSelectors = splitSelector(selector); |
| 521 for (let subSelector of subSelectors) | 517 for (let subSelector of subSelectors) |
| 522 preparedSelectors.push("::content " + subSelector); | 518 preparedSelectors.push("::content " + subSelector); |
| 523 } | 519 } |
| 524 } | 520 } |
| 525 else | 521 else |
| 522 { |
| 526 preparedSelectors = selectors; | 523 preparedSelectors = selectors; |
| 524 } |
| 527 | 525 |
| 528 // Safari only allows 8192 primitive selectors to be injected at once[1], we | 526 // Safari only allows 8192 primitive selectors to be injected at once[1], we |
| 529 // therefore chunk the inserted selectors into groups of 200 to be safe. | 527 // therefore chunk the inserted selectors into groups of 200 to be safe. |
| 530 // (Chrome also has a limit, larger... but we're not certain exactly what it | 528 // (Chrome also has a limit, larger... but we're not certain exactly what it |
| 531 // is! Edge apparently has no such limit.) | 529 // is! Edge apparently has no such limit.) |
| 532 // [1] - https://github.com/WebKit/webkit/blob/1cb2227f6b2a1035f7bdc46e5ab69
debb75fc1de/Source/WebCore/css/RuleSet.h#L68 | 530 // [1] - https://github.com/WebKit/webkit/blob/1cb2227f6b2a1035f7bdc46e5ab69
debb75fc1de/Source/WebCore/css/RuleSet.h#L68 |
| 533 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) | 531 for (let i = 0; i < preparedSelectors.length; i += this.selectorGroupSize) |
| 534 { | 532 { |
| 535 let selector = preparedSelectors.slice( | 533 let selector = preparedSelectors.slice( |
| 536 i, i + this.selectorGroupSize | 534 i, i + this.selectorGroupSize |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 checkCollapse(event.target); | 575 checkCollapse(event.target); |
| 578 }, true); | 576 }, true); |
| 579 | 577 |
| 580 document.addEventListener("load", event => | 578 document.addEventListener("load", event => |
| 581 { | 579 { |
| 582 let element = event.target; | 580 let element = event.target; |
| 583 if (/^i?frame$/.test(element.localName)) | 581 if (/^i?frame$/.test(element.localName)) |
| 584 checkCollapse(element); | 582 checkCollapse(element); |
| 585 }, true); | 583 }, true); |
| 586 } | 584 } |
| LEFT | RIGHT |