OLD | NEW |
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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 521 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
532 propagateHandlerBehaviorChange | 532 propagateHandlerBehaviorChange |
533 ); | 533 ); |
534 browser.webRequest.handlerBehaviorChanged(); | 534 browser.webRequest.handlerBehaviorChanged(); |
535 | 535 |
536 handlerBehaviorChangedQuota--; | 536 handlerBehaviorChangedQuota--; |
537 setTimeout(() => { handlerBehaviorChangedQuota++; }, 600000); | 537 setTimeout(() => { handlerBehaviorChangedQuota++; }, 600000); |
538 } | 538 } |
539 } | 539 } |
540 | 540 |
541 ext.webRequest = { | 541 ext.webRequest = { |
542 onBeforeRequest: new ext._EventTarget(), | |
543 handlerBehaviorChanged() | 542 handlerBehaviorChanged() |
544 { | 543 { |
545 // Defer handlerBehaviorChanged() until navigation occurs. | 544 // Defer handlerBehaviorChanged() until navigation occurs. |
546 // There wouldn't be any visible effect when calling it earlier, | 545 // There wouldn't be any visible effect when calling it earlier, |
547 // but it's an expensive operation and that way we avoid to call | 546 // but it's an expensive operation and that way we avoid to call |
548 // it multiple times, if multiple filters are added/removed. | 547 // it multiple times, if multiple filters are added/removed. |
549 let {onBeforeNavigate} = browser.webNavigation; | 548 let {onBeforeNavigate} = browser.webNavigation; |
550 if (!onBeforeNavigate.hasListener(propagateHandlerBehaviorChange)) | 549 if (!onBeforeNavigate.hasListener(propagateHandlerBehaviorChange)) |
551 onBeforeNavigate.addListener(propagateHandlerBehaviorChange); | 550 onBeforeNavigate.addListener(propagateHandlerBehaviorChange); |
552 } | 551 } |
(...skipping 16 matching lines...) Expand all Loading... |
569 frames.set(detail.frameId, frame); | 568 frames.set(detail.frameId, frame); |
570 | 569 |
571 if (detail.parentFrameId != -1) | 570 if (detail.parentFrameId != -1) |
572 frame.parent = frames.get(detail.parentFrameId); | 571 frame.parent = frames.get(detail.parentFrameId); |
573 } | 572 } |
574 } | 573 } |
575 }); | 574 }); |
576 }); | 575 }); |
577 }); | 576 }); |
578 | 577 |
579 browser.webRequest.onBeforeRequest.addListener(details => | |
580 { | |
581 // The high-level code isn't interested in requests that aren't | |
582 // related to a tab or requests loading a top-level document, | |
583 // those should never be blocked. | |
584 if (details.type == "main_frame") | |
585 return; | |
586 | |
587 // Filter out requests from non web protocols. Ideally, we'd explicitly | |
588 // specify the protocols we are interested in (i.e. http://, https://, | |
589 // ws:// and wss://) with the url patterns, given below, when adding this | |
590 // listener. But unfortunately, Chrome <=57 doesn't support the WebSocket | |
591 // protocol and is causing an error if it is given. | |
592 let url = new URL(details.url); | |
593 if (url.protocol != "http:" && url.protocol != "https:" && | |
594 url.protocol != "ws:" && url.protocol != "wss:") | |
595 return; | |
596 | |
597 if (details.originUrl) | |
598 { | |
599 // Firefox-only currently, ignore requests initiated by the browser and | |
600 // extensions. | |
601 let originUrl = new URL(details.originUrl); | |
602 if (originUrl.protocol == "chrome:" || | |
603 originUrl.protocol == "moz-extension:") | |
604 { | |
605 return; | |
606 } | |
607 } | |
608 | |
609 // We are looking for the frame that contains the element which | |
610 // has triggered this request. For most requests (e.g. images) we | |
611 // can just use the request's frame ID, but for subdocument requests | |
612 // (e.g. iframes) we must instead use the request's parent frame ID. | |
613 let {frameId, type} = details; | |
614 if (type == "sub_frame") | |
615 frameId = details.parentFrameId; | |
616 | |
617 // Sometimes requests are not associated with a browser tab and | |
618 // in this case we want to still be able to view the url being called. | |
619 let frame = null; | |
620 let page = null; | |
621 if (details.tabId != -1) | |
622 { | |
623 frame = ext.getFrame(details.tabId, frameId); | |
624 page = new Page({id: details.tabId}); | |
625 } | |
626 | |
627 if (ext.webRequest.onBeforeRequest._dispatch( | |
628 url, type, page, frame).includes(false)) | |
629 return {cancel: true}; | |
630 }, {urls: ["<all_urls>"]}, ["blocking"]); | |
631 | |
632 | 578 |
633 /* Message passing */ | 579 /* Message passing */ |
634 | 580 |
635 browser.runtime.onMessage.addListener((message, rawSender, sendResponse) => | 581 browser.runtime.onMessage.addListener((message, rawSender, sendResponse) => |
636 { | 582 { |
637 let sender = {}; | 583 let sender = {}; |
638 | 584 |
639 // Add "page" and "frame" if the message was sent by a content script. | 585 // Add "page" and "frame" if the message was sent by a content script. |
640 // If sent by popup or the background page itself, there is no "tab". | 586 // If sent by popup or the background page itself, there is no "tab". |
641 if ("tab" in rawSender) | 587 if ("tab" in rawSender) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
692 ext.windows = { | 638 ext.windows = { |
693 create(createData, callback) | 639 create(createData, callback) |
694 { | 640 { |
695 browser.windows.create(createData, createdWindow => | 641 browser.windows.create(createData, createdWindow => |
696 { | 642 { |
697 afterTabLoaded(callback)(createdWindow.tabs[0]); | 643 afterTabLoaded(callback)(createdWindow.tabs[0]); |
698 }); | 644 }); |
699 } | 645 } |
700 }; | 646 }; |
701 } | 647 } |
OLD | NEW |