|
| 1 | +/* handle custom redirects after fetch */ |
| 2 | + |
| 3 | +function interceptor() { |
| 4 | + const externalMatches = ['accounts.google.com']; |
| 5 | + const needIntercept = (url) => externalMatches.some((d) => new RegExp(d).test(url)); |
| 6 | + const { externalRedirects } = window.__LOCALHOSTIFY__; |
| 7 | + |
| 8 | + const onReadyStateChange = function () { |
| 9 | + if (this.readyState === 4 && needIntercept(this.responseURL)) { |
| 10 | + const { responseURL, responseType, response } = this; |
| 11 | + |
| 12 | + if (responseType === '' || responseType === 'text') { |
| 13 | + Object.defineProperty(this, 'responseText', { |
| 14 | + get: function () { |
| 15 | + let replacedResponse = response; |
| 16 | + |
| 17 | + externalRedirects.forEach((rule) => { |
| 18 | + if (new RegExp(rule.match).test(replacedResponse)) { |
| 19 | + replacedResponse = replacedResponse.replace(rule.replace[0], rule.replace[1]); |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + if (response !== replacedResponse) { |
| 24 | + console.log('Localhostify -> ', JSON.stringify({ responseURL, response, replacedResponse }, 0, 2)); |
| 25 | + } |
| 26 | + return replacedResponse; |
| 27 | + }, |
| 28 | + }); |
| 29 | + } |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + const XHR = XMLHttpRequest; |
| 34 | + XMLHttpRequest = function () { |
| 35 | + const xhr = new XHR(); |
| 36 | + xhr.requestURL; |
| 37 | + xhr.addEventListener('readystatechange', onReadyStateChange.bind(xhr), false); |
| 38 | + return xhr; |
| 39 | + }; |
| 40 | + |
| 41 | + XMLHttpRequest.prototype = XHR.prototype; |
| 42 | + Object.entries(XHR).map(([key, val]) => { |
| 43 | + XMLHttpRequest[key] = val; |
| 44 | + }); |
| 45 | + |
| 46 | + const open = XMLHttpRequest.prototype.open; |
| 47 | + XMLHttpRequest.prototype.open = function (method) { |
| 48 | + this.method = method; |
| 49 | + open.apply(this, arguments); |
| 50 | + }; |
| 51 | + |
| 52 | + const send = XMLHttpRequest.prototype.send; |
| 53 | + XMLHttpRequest.prototype.send = function (data) { |
| 54 | + this.requestData = data; |
| 55 | + send.apply(this, arguments); |
| 56 | + }; |
| 57 | + |
| 58 | + let setRequestHeader = XMLHttpRequest.prototype.setRequestHeader; |
| 59 | + XMLHttpRequest.prototype.setRequestHeader = function (header, value) { |
| 60 | + this.requestHeaders = this.requestHeaders || {}; |
| 61 | + this.requestHeaders[header] = value; |
| 62 | + setRequestHeader.apply(this, arguments); |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +const interceptorScript = interceptor.toString(); |
| 67 | +const config = 'window.__LOCALHOSTIFY__=' + JSON.stringify(window.__LOCALHOSTIFY__, 0, 2) + ';'; |
| 68 | + |
| 69 | +const script = document.createElement('script'); |
| 70 | +script.className = 'localhostify'; |
| 71 | +script.type = 'text/javascript'; |
| 72 | +script.appendChild(document.createTextNode(`${config} (${interceptorScript})()`)); |
| 73 | + |
| 74 | +const parent = document.head || document.documentElement; |
| 75 | +parent.appendChild(script); |
0 commit comments