|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +function patchWindow(patchingCallback, targetWindow = window) { |
| 4 | + let nativeExport = this && this.exportFunction || typeof exportFunction == "function"; |
| 5 | + if (!nativeExport) { |
| 6 | + // Chromium |
| 7 | + let exportFunction = (func, targetObject, {defineAs}) => { |
| 8 | + let original = targetObject[defineAs]; |
| 9 | + if (original && original.toString) { |
| 10 | + func.toString = original.toString.bind(original); |
| 11 | + } |
| 12 | + console.log(`Setting ${targetObject}.${defineAs}`, func); |
| 13 | + targetObject[defineAs] = func; |
| 14 | + }; |
| 15 | + let cloneInto = (obj, targetObject) => { |
| 16 | + return obj; // dummy for assignment |
| 17 | + }; |
| 18 | + let script = document.createElement("script"); |
| 19 | + script.text = ` |
| 20 | + (() => { |
| 21 | + console.log("Chromium patchWindow"); |
| 22 | + let patchWindow = ${patchWindow}; |
| 23 | + let cloneInto = ${cloneInto}; |
| 24 | + let exportFunction = ${exportFunction}; |
| 25 | + ({ |
| 26 | + patchWindow, |
| 27 | + exportFunction, |
| 28 | + cloneInto, |
| 29 | + }).patchWindow(${patchingCallback}); |
| 30 | + })(); |
| 31 | + `; |
| 32 | + document.documentElement.insertBefore(script, document.documentElement.firstChild); |
| 33 | + script.remove(); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // win: window object to modify. |
| 38 | + // modifyTarget: callback to function that modifies the desired properties |
| 39 | + // or methods. Callback must take target window as argument. |
| 40 | + function modifyWindow(win, modifyTarget) { |
| 41 | + try { |
| 42 | + modifyTarget(win.wrappedJSObject || win); |
| 43 | + modifyWindowOpenMethod(win, modifyTarget); |
| 44 | + modifyFramingElements(win, modifyTarget); |
| 45 | + } catch (e) { |
| 46 | + if (e instanceof DOMException && e.name === "SecurityError") { |
| 47 | + // In case someone tries to access SOP restricted window. |
| 48 | + // We can just ignore this. |
| 49 | + } else throw e; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + function modifyWindowOpenMethod(win, modifyTarget) { |
| 54 | + let windowOpen = win.wrappedJSObject ? win.wrappedJSObject.open : win.open; |
| 55 | + exportFunction(function(...args) { |
| 56 | + let newWin = windowOpen.call(this, ...args); |
| 57 | + if (newWin) modifyWindow(newWin, modifyTarget); |
| 58 | + return newWin; |
| 59 | + }, win, {defineAs: "open"}); |
| 60 | + } |
| 61 | + |
| 62 | + function modifyFramingElements(win, modifyTarget) { |
| 63 | + for (let property of ["contentWindow", "contentDocument"]) { |
| 64 | + for (let iface of ["Frame", "IFrame", "Object"]) { |
| 65 | + let proto = win[`HTML${iface}Element`].prototype; |
| 66 | + modifyContentProperties(proto, property, modifyTarget) |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + function modifyContentProperties(proto, property, modifyTarget) { |
| 72 | + let descriptor = Object.getOwnPropertyDescriptor(proto, property); |
| 73 | + let origGetter = descriptor.get; |
| 74 | + let replacementFn; |
| 75 | + |
| 76 | + if (property === "contentWindow") { replacementFn = function() { |
| 77 | + let win = origGetter.call(this); |
| 78 | + if (win) modifyWindow(win, modifyTarget); |
| 79 | + return win; |
| 80 | + }} |
| 81 | + if (property === "contentDocument") { replacementFn = function() { |
| 82 | + let document = origGetter.call(this); |
| 83 | + if (document && document.defaultView) modifyWindow(document.defaultView, modifyTarget); |
| 84 | + return document; |
| 85 | + }} |
| 86 | + |
| 87 | + descriptor.get = exportFunction(replacementFn, proto, {defineAs: `get $property`}); |
| 88 | + let wrappedProto = proto.wrappedJSObject || proto; |
| 89 | + Object.defineProperty(wrappedProto, property, descriptor); |
| 90 | + } |
| 91 | + |
| 92 | + return modifyWindow(targetWindow, patchingCallback); |
| 93 | +} |
0 commit comments