[英]How do I make my userscript execute code in isolated sandbox and unsafeWindow too?
For most of my code in my userscript, I'm need to use unsafeWindow
for the websites my script executes on.对于我的用户脚本中的大部分代码,我需要在我的脚本执行的网站上使用
unsafeWindow
。 I do this by using // @grant unsafeWindow
.我通过使用
// @grant unsafeWindow
来做到这一点。 However, some of my code cannot be executed with unsafeWindow
and needs to run in Tampermonkey's isolated sandbox.但是,我的一些代码不能用
unsafeWindow
执行,需要在 Tampermonkey 的隔离沙箱中运行。 How would I be able to do this?我怎么能做到这一点? Something like this could work:
像这样的东西可以工作:
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()
Use the isolated sandbox with the privileged userscript functions by default.默认情况下,使用具有特权用户脚本功能的隔离沙箱。 Then, for the code that requires use of the native page, you could insert the code into a
<script>
tag, eg:然后,对于需要使用本机页面的代码,您可以将代码插入到
<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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.