对于我的用户脚本中的大多数代码,我需要在我的脚本执行的网站上使用unsafeWindow。我通过使用// @grant unsafeWindow来实现这一点。然而,我的一些代码不能在unsafeWindow中执行,需要在坦帕猴子的隔离沙箱中运行。我怎么才能做到这一点呢?像这样的东西可能会起作用:
function disableUnsafeWindow() {
// Disable UnsafeWindow
}
function enableUnsafeWindow() {
// Enable unsafeWindow
}
function withUnsafeWindow() {
enableUnsafeWindow()
// Run the code using unsafeWindow
}
function withoutUnsafeWindow() {
disableUnsafeWindow();
// Remove unsafeWindow access and execute the code without unsafeWindow
}
withUnsafeWindow()
withoutUnsafeWindow()发布于 2021-01-03 08:06:43
默认情况下,使用具有特权用户脚本函数的隔离沙箱。然后,对于需要使用本机页面的代码,可以将代码插入到<script>标记中,例如:
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();
https://stackoverflow.com/questions/65545401
复制相似问题