对于我的用户脚本中的大部分代码,我需要使用unsafeWindow
对于我的脚本执行的网站。我通过使用来做到这一点// @grant unsafeWindow
。但是,我的一些代码无法执行unsafeWindow
并且需要在 Tampermonkey 的隔离沙箱中运行。我怎样才能做到这一点?像这样的事情可以工作:
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()
默认情况下,使用带有特权用户脚本函数的隔离沙箱。然后,对于需要使用本机页面的代码,您可以将代码插入到<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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)