|
| 1 | +/******************************************************************************* |
| 2 | +
|
| 3 | + uBlock - a browser extension to block requests. |
| 4 | + Copyright (C) 2018 The uBlock authors |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see {http://www.gnu.org/licenses/}. |
| 18 | +
|
| 19 | + Home: https://github.com/gorhill/uBlock |
| 20 | + */ |
| 21 | + |
| 22 | +/* global self, safari, SafariBrowserTab, µBlock */ |
| 23 | + |
| 24 | +// For background page |
| 25 | + |
| 26 | +'use strict'; |
| 27 | + |
| 28 | +/******************************************************************************/ |
| 29 | + |
| 30 | +vAPI.net = {}; |
| 31 | + |
| 32 | +vAPI.net.registerListeners = function() { |
| 33 | + var µb = µBlock, |
| 34 | + µburi = µb.URI; |
| 35 | + |
| 36 | + // Until Safari has more specific events, those are instead handled |
| 37 | + // in the onBeforeRequestAdapter; clean them up so they're garbage-collected |
| 38 | + vAPI.net.onBeforeSendHeaders = null; |
| 39 | + |
| 40 | + var onBeforeRequest = vAPI.net.onBeforeRequest, |
| 41 | + onBeforeRequestClient = onBeforeRequest.callback, |
| 42 | + onHeadersReceivedClient = vAPI.net.onHeadersReceived.callback; |
| 43 | + |
| 44 | + // https://github.com/el1t/uBlock-Safari/issues/32 |
| 45 | + // Ignore directives |
| 46 | + var shouldBlockResponseHeader = { |
| 47 | + script: /script-src/, |
| 48 | + worker: /child-src/ |
| 49 | + }; |
| 50 | + |
| 51 | + var onBeforeRequestAdapter = function(e) { |
| 52 | + if ( e.name !== 'canLoad' ) { |
| 53 | + return; |
| 54 | + } |
| 55 | + e.stopPropagation && e.stopPropagation(); |
| 56 | + switch ( e.message.type ) { |
| 57 | + case 'main_frame': |
| 58 | + vAPI.tabs.onNavigation({ |
| 59 | + url: e.message.url, |
| 60 | + frameId: 0, |
| 61 | + tabId: vAPI.tabs.getTabId(e.target).toString() |
| 62 | + }); |
| 63 | + e.message.hostname = µburi.hostnameFromURI(e.message.url); |
| 64 | + e.message.tabId = vAPI.tabs.getTabId(e.target); |
| 65 | + e.message.responseHeaders = []; |
| 66 | + onBeforeRequestClient(e.message); |
| 67 | + var blockVerdict = onHeadersReceivedClient(e.message); |
| 68 | + blockVerdict = blockVerdict && blockVerdict.responseHeaders && blockVerdict.responseHeaders[0] && |
| 69 | + shouldBlockResponseHeader.script.test(blockVerdict.responseHeaders[0].value); |
| 70 | + e.message = { |
| 71 | + shouldBlock: blockVerdict === true |
| 72 | + }; |
| 73 | + return; |
| 74 | + case 'popup': |
| 75 | + var openerTabId = vAPI.tabs.getTabId(e.target).toString(); |
| 76 | + var shouldBlock = !!vAPI.tabs.onPopupUpdated('preempt', openerTabId, e.message.url); |
| 77 | + if ( !shouldBlock ) { |
| 78 | + vAPI.tabs.popupCandidate = openerTabId; |
| 79 | + } |
| 80 | + e.message = { |
| 81 | + shouldBlock: shouldBlock |
| 82 | + }; |
| 83 | + break; |
| 84 | + case 'popstate': |
| 85 | + // No return value/message |
| 86 | + vAPI.tabs.onUpdated(vAPI.tabs.getTabId(e.target), { |
| 87 | + url: e.message.url |
| 88 | + }, { |
| 89 | + url: e.message.url |
| 90 | + }); |
| 91 | + break; |
| 92 | + case 'worker': |
| 93 | + e.message.type = 'sub_frame'; |
| 94 | + e.message.hostname = µburi.hostnameFromURI(e.message.url); |
| 95 | + e.message.tabId = vAPI.tabs.getTabId(e.target); |
| 96 | + e.message.responseHeaders = []; |
| 97 | + var blockVerdict = onHeadersReceivedClient(e.message); |
| 98 | + blockVerdict = blockVerdict && blockVerdict.responseHeaders && blockVerdict.responseHeaders[0] && |
| 99 | + shouldBlockResponseHeader.worker.test(blockVerdict.responseHeaders[0].value); |
| 100 | + e.message = { |
| 101 | + shouldBlock: blockVerdict === true |
| 102 | + } |
| 103 | + return; |
| 104 | + default: |
| 105 | + e.message.hostname = µburi.hostnameFromURI(e.message.url); |
| 106 | + e.message.tabId = vAPI.tabs.getTabId(e.target); |
| 107 | + var blockVerdict = onBeforeRequestClient(e.message) || {}; |
| 108 | + blockVerdict.shouldBlock = blockVerdict.cancel === true || blockVerdict.redirectUrl !== undefined; |
| 109 | + e.message = blockVerdict; |
| 110 | + return; |
| 111 | + } |
| 112 | + }; |
| 113 | + safari.application.addEventListener('message', onBeforeRequestAdapter, true); |
| 114 | +}; |
0 commit comments