【发布时间】:2023-03-02 23:40:02
【问题描述】:
unsafeWindow API 是为了让用户脚本可以与脚本执行的页面的变量和函数进行交互。但是,强烈建议不要这样做,因为网站可能会通过unsafeWindow 劫持用户脚本并使其执行恶意代码。但是,为什么unsafeWindow 甚至是必要的,为什么仍然使用它?在 Firefox 39 之前,用户可以使用位置黑客来替代unsafeWindow。由于 Firefox 39 中的更新修复了这个问题,这个黑客最终停止了工作。尽管如此,用户仍然可以在隔离的沙箱中使用 GM API,并通过这样的脚本标签插入代码:
const fnToRunOnNativePage = () => {
console.log('fnToRunOnNativePage');
};
const script = document.body.appendChild(document.createElement('script'));
script.textContent = '(' + fnToRunOnNativePage.toString() + ')();';
// to use information inside the function that was retrieved elsewhere in the script,
// pass arguments above
script.remove();
我从这个 stackoverflow 帖子中得到了这段代码:How do I make my userscript execute code in isolated sandbox and unsafeWindow too?
那么为什么unsafeWindow 仍然可用?上面的代码几乎是unsafeWindow 的完美替代品。另外作为旁注,unsafeWindow 在 Greasemonkey 和 Tampermonkey 中的运行方式有什么不同吗?谢谢。
外部资源:
【问题讨论】:
标签: javascript browser sandbox execution userscripts