|
1 | | -(function (exports) { |
| 1 | +(function (global) { |
2 | 2 | "use strict"; |
3 | | - /*global unblocker*/ |
4 | 3 |
|
5 | 4 | // todo: |
6 | | - // - fetch |
7 | | - // - history API |
8 | 5 | // - postMessage |
9 | 6 | // - open |
10 | 7 | // - split each part into separate files (?) |
11 | 8 | // - wrap other JS and provide proxies to fix writes to window.location and document.cookie |
12 | 9 | // - will require updating contentTypes.html.includes(data.contentType) to include js |
13 | 10 | // - that, in turn will require decompressing js.... |
| 11 | + // call() and apply() on `this || original_thing` |
| 12 | + // prevent a failure in one initializer from stopping subsequent initializers |
| 13 | + |
| 14 | + function fixUrl(urlStr, config, location) { |
| 15 | + var currentRemoteHref; |
| 16 | + if (location.pathname.substr(0, config.prefix.length) === config.prefix) { |
| 17 | + currentRemoteHref = |
| 18 | + location.pathname.substr(config.prefix.length) + |
| 19 | + location.search + |
| 20 | + location.hash; |
| 21 | + } else { |
| 22 | + // in case sites (such as youtube) manage to bypass our history wrapper |
| 23 | + currentRemoteHref = config.url; |
| 24 | + } |
14 | 25 |
|
15 | | - console.log("begin unblocker client scripts", unblocker); |
| 26 | + // check if it's already proxied (root-relative) |
| 27 | + if (urlStr.substr(0, config.prefix.length) === config.prefix) { |
| 28 | + return urlStr; |
| 29 | + } |
16 | 30 |
|
17 | | - function fixUrl(target, prefix, location) { |
18 | | - var currentRemoteHref = |
19 | | - location.pathname.substr(prefix.length) + location.search + location.hash; |
20 | | - var url = new URL(target, currentRemoteHref); |
| 31 | + var url = new URL(urlStr, currentRemoteHref); |
21 | 32 |
|
22 | | - //todo: handle already proxied urls (will be important for checking current dom) |
| 33 | + // check if it's already proxied (absolute) |
| 34 | + if ( |
| 35 | + url.origin === location.origin && |
| 36 | + url.pathname.substr(0, config.prefix.length) === config.prefix |
| 37 | + ) { |
| 38 | + return urlStr; |
| 39 | + } |
23 | 40 |
|
24 | | - // don't break data: urls |
25 | | - if (url.protocol === "data:") { |
26 | | - return target; |
| 41 | + // don't break data: urls, about:blank, etc |
| 42 | + // todo: do modify ws: and wss: protocols |
| 43 | + if (url.protocol !== "http:" && url.protocol !== "https:") { |
| 44 | + return urlStr; |
27 | 45 | } |
28 | 46 |
|
29 | | - // sometimes websites are tricky |
| 47 | + // sometimes websites are tricky and use the current host or hostname + a relative url |
30 | 48 | // check hostname (ignoring port) |
31 | 49 | if (url.hostname === location.hostname) { |
32 | 50 | var currentRemoteUrl = new URL(currentRemoteHref); |
|
36 | 54 | url.protocol = currentRemoteUrl.protocol; |
37 | 55 | // todo: handle websocket protocols |
38 | 56 | } |
39 | | - return prefix + url.href; |
| 57 | + return config.prefix + url.href; |
40 | 58 | } |
41 | 59 |
|
42 | | - function initXMLHttpRequest(config) { |
43 | | - var _XMLHttpRequest = XMLHttpRequest; |
| 60 | + function initXMLHttpRequest(config, window) { |
| 61 | + if (!window.XMLHttpRequest) return; |
| 62 | + var _XMLHttpRequest = window.XMLHttpRequest; |
44 | 63 |
|
45 | 64 | window.XMLHttpRequest = function (opts) { |
46 | 65 | var xhr = new _XMLHttpRequest(opts); |
47 | 66 | var _open = xhr.open; |
48 | 67 | xhr.open = function () { |
49 | 68 | var args = Array.prototype.slice.call(arguments); |
50 | | - args[1] = fixUrl(args[1], config.prefix, location); |
| 69 | + args[1] = fixUrl(args[1], config, location); |
51 | 70 | return _open.apply(xhr, args); |
52 | 71 | }; |
53 | 72 | return xhr; |
54 | 73 | }; |
55 | 74 | } |
56 | 75 |
|
57 | | - function initCreateElement(config) { |
58 | | - var prefix = config.prefix; |
59 | | - var _createElement = document.createElement; |
60 | | - |
61 | | - document.createElement = function (tagName, options) { |
62 | | - var element = _createElement.call(document, tagName, options); |
63 | | - // todo: whitelist elements with href or src attributes and only check those |
64 | | - setTimeout(function () { |
65 | | - if (element.src) { |
66 | | - element.src = fixUrl(element.src, prefix, location); |
67 | | - } |
68 | | - if (element.href) { |
69 | | - element.href = fixUrl(element.href, prefix, location); |
70 | | - } |
71 | | - // todo: support srcset and ..? |
72 | | - }, 0); |
73 | | - // todo: handle urls that aren't set immediately |
| 76 | + function initFetch(config, window) { |
| 77 | + if (!window.fetch) return; |
| 78 | + var _fetch = window.fetch; |
| 79 | + |
| 80 | + window.fetch = function (resource, init) { |
| 81 | + if (resource.url) { |
| 82 | + resource.url = fixUrl(resource.url, config, location); |
| 83 | + } else { |
| 84 | + resource = fixUrl(resource.toString(), config, location); |
| 85 | + } |
| 86 | + return _fetch(resource, init); |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + function initCreateElement(config, window) { |
| 91 | + if (!window.document || !window.document.createElement) return; |
| 92 | + var _createElement = window.document.createElement; |
| 93 | + |
| 94 | + window.document.createElement = function (tagName, options) { |
| 95 | + if (tagName.toLowerCase() === "iframe") { |
| 96 | + initAppendBodyIframe(config, window); |
| 97 | + } |
| 98 | + var element = _createElement.call(window.document, tagName, options); |
| 99 | + Object.defineProperty(element, "src", { |
| 100 | + set: function (src) { |
| 101 | + delete element.src; // remove this setter so we don't get stuck in an infinite loop |
| 102 | + element.src = fixUrl(src, config, location); |
| 103 | + }, |
| 104 | + configurable: true, |
| 105 | + }); |
| 106 | + Object.defineProperty(element, "href", { |
| 107 | + set: function (href) { |
| 108 | + delete element.href; // remove this setter so we don't get stuck in an infinite loop |
| 109 | + element.href = fixUrl(href, config, location); |
| 110 | + }, |
| 111 | + configurable: true, |
| 112 | + }); |
| 113 | + // todo: consider restoring the setter in case the client js changes the value later... |
| 114 | + // todo: support srcset |
74 | 115 | return element; |
75 | 116 | }; |
76 | 117 | } |
77 | 118 |
|
78 | | - function initWebsockets(config) { |
79 | | - var _WebSocket = WebSocket; |
| 119 | + // js on some sites, such as youtube, uses an iframe to grab native APIs such as history, so we need to fix those also. |
| 120 | + // document.body isn't available when this script is first executed, |
| 121 | + // so we'll also try when createElement is called, but set a flag to ensure it only installs once |
| 122 | + function initAppendBodyIframe(config, window) { |
| 123 | + if ( |
| 124 | + !window.document || |
| 125 | + !window.document.body || |
| 126 | + !window.document.body.appendChild || |
| 127 | + window.document.body.unblockerIframeAppendListenerInstalled |
| 128 | + ) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + var _appendChild = window.document.body.appendChild; |
| 133 | + |
| 134 | + window.document.body.appendChild = function (element) { |
| 135 | + var ret = _appendChild.call(window.document.body, element); |
| 136 | + if ( |
| 137 | + element.tagName && |
| 138 | + element.tagName.toLowerCase() === "iframe" && |
| 139 | + element.src === "about:blank" && |
| 140 | + element.contentWindow |
| 141 | + ) { |
| 142 | + initForWindow(config, element.contentWindow); |
| 143 | + } |
| 144 | + return ret; |
| 145 | + }; |
| 146 | + window.document.body.unblockerIframeAppendListenerInstalled = true; |
| 147 | + } |
| 148 | + |
| 149 | + function initWebSockets(config, window) { |
| 150 | + if (!window.WebSocket) return; |
| 151 | + var _WebSocket = window.WebSocket; |
80 | 152 | var prefix = config.prefix; |
81 | 153 | var proxyHost = location.host; |
82 | 154 | var isSecure = location.protocol === "https"; |
|
118 | 190 | }; |
119 | 191 | } |
120 | 192 |
|
121 | | - initXMLHttpRequest(unblocker); |
122 | | - initCreateElement(unblocker); |
123 | | - initWebsockets(unblocker); |
| 193 | + // todo: figure out how youtube bypasses this |
| 194 | + // notes: look at bindHistoryStateFunctions_ - it looks like it checks the contentWindow.history of an iframe *fitst*, then it's __proto__, then the global history api |
| 195 | + // - so, we need to inject this into iframes also |
| 196 | + function initPushState(config, window) { |
| 197 | + if (!window.history || !window.history.pushState) return; |
| 198 | + |
| 199 | + var _pushState = window.history.pushState; |
| 200 | + window.history.pushState = function (state, title, url) { |
| 201 | + if (url) { |
| 202 | + url = fixUrl(url, config, location); |
| 203 | + config.url = new URL(url, config.url); |
| 204 | + return _pushState.call(history, state, title, url); |
| 205 | + } |
| 206 | + }; |
| 207 | + |
| 208 | + if (!window.history.replaceState) return; |
| 209 | + var _replaceState = window.history.replaceState; |
| 210 | + window.history.replaceState = function (state, title, url) { |
| 211 | + if (url) { |
| 212 | + url = fixUrl(url, config, location); |
| 213 | + config.url = new URL(url, config.url); |
| 214 | + return _replaceState.call(history, state, title, url); |
| 215 | + } |
| 216 | + }; |
| 217 | + } |
124 | 218 |
|
125 | | - console.log("unblocker client scripts initialized"); |
| 219 | + function initForWindow(config, window) { |
| 220 | + console.log("begin unblocker client scripts", config, window); |
| 221 | + initXMLHttpRequest(config, window); |
| 222 | + initFetch(config, window); |
| 223 | + initCreateElement(config, window); |
| 224 | + initAppendBodyIframe(config, window); |
| 225 | + initWebSockets(config, window); |
| 226 | + initPushState(config, window); |
| 227 | + if (window === global) { |
| 228 | + // leave no trace |
| 229 | + delete global.unblockerInit; |
| 230 | + } |
| 231 | + console.log("unblocker client scripts initialized"); |
| 232 | + } |
126 | 233 |
|
127 | | - // export things for testing if loaded via commonjs |
128 | | - exports.fixUrl = fixUrl; |
| 234 | + // either export things for testing or put the init method into the global scope to be called |
| 235 | + // with config by the next script tag in a browser |
129 | 236 | /*globals module*/ |
130 | | -})((typeof module === "object" && module.exports) || {}); |
| 237 | + if (typeof module === "undefined") { |
| 238 | + global.unblockerInit = initForWindow; |
| 239 | + } else { |
| 240 | + module.exports = { |
| 241 | + initForWindow: initForWindow, |
| 242 | + fixUrl: fixUrl, |
| 243 | + }; |
| 244 | + } |
| 245 | +})(this); // window in a browser, global in node.js |
0 commit comments