|
| 1 | +/* |
| 2 | + * NoScript Commons Library |
| 3 | + * Reusable building blocks for cross-browser security/privacy WebExtensions. |
| 4 | + * Copyright (C) 2020-2023 Giorgio Maone <https://maone.net> |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify it under |
| 9 | + * the terms of the GNU General Public License as published by the Free Software |
| 10 | + * Foundation, either version 3 of the License, or (at your option) any later |
| 11 | + * version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 14 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 15 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License along with |
| 18 | + * this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +// depends on nscl/content/patchWindow.js |
| 22 | +"use strict"; |
| 23 | + |
| 24 | +{ |
| 25 | + debug("Prompt Hook installation", document.URL); // DEV_ONLY |
| 26 | + const patchPrompts = (scope, {port, xray}) => { |
| 27 | + for (let methodName of ["alert", "confirm", "prompt"]) { |
| 28 | + let target = methodName in scope.__proto__ ? scope.__proto__ : scope; |
| 29 | + const method = xray.getSafeMethod(target, methodName); |
| 30 | + const handler = cloneInto({ |
| 31 | + apply(targetObject, thisArg, argumentsList) { |
| 32 | + try { |
| 33 | + return method.call(thisArg, ...argumentsList); |
| 34 | + } finally { |
| 35 | + port.postMessage("prompt", target); |
| 36 | + } |
| 37 | + } |
| 38 | + }, scope, {cloneFunctions: true}); |
| 39 | + target[methodName] = new scope.Proxy(method, handler); |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + const port = patchWindow(patchPrompts); |
| 44 | + port.onMessage = (msg, {target}) => { |
| 45 | + if (msg !== "prompt") return; |
| 46 | + debug("Prompt Hook triggered by", target.location.href); // DEV_ONLY |
| 47 | + function efs(w) { |
| 48 | + try { |
| 49 | + if (w.document.fullscreenElement) { |
| 50 | + w.document.exitFullscreen(); |
| 51 | + } |
| 52 | + } catch (e) { |
| 53 | + debug(e); // DEV_ONLY |
| 54 | + } |
| 55 | + } |
| 56 | + efs(window); |
| 57 | + for (let j = 0; j in window; j++) { |
| 58 | + efs(window[j]); |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments