简体   繁体   English

如何让我的用户脚本在隔离沙箱和 unsafeWindow 中执行代码?

[英]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.

相关问题 为什么用户脚本管理器仍然支持使用 unsafeWindow? - Why do userscript managers still suport the use of unsafeWindow? 如果我的文件是正在运行的文件,我如何使node.js执行一些代码? - How do I make node.js execute some code only if my file is the running file? 我的代码太复杂了,我应该如何让它变得简单? - My code is too complicated how should I make it simple? 如何使我的代码更快? - How do I make my code faster? jQuery我如何执行此代码,并使其显示在div中 - jquery how do i execute this code and make it display in a div 将Firefox 30之前的Greasemonkey脚本迁移到GM4 +时,如何替换unsafeWindow? - How do I replace unsafeWindow when migrating a pre-Firefox 30 Greasemonkey script to GM4+? 如何在Moz扩展页面上执行用户脚本? - How can I execute a userscript on a moz-extension page? 如何使用远程页面的构造函数在我的Greasemonkey UserScript中创建一个Object? - How do I use constructor of a remote Page to create an Object in my Greasemonkey UserScript? 我有一个用户脚本来更改嵌入的聊天颜色。 如何将脚本的功能合并到页面中? - I have a userscript to change embedded chat colors. How do I incorporate the script's functionality into my page instead? 我怎样才能说服WebStorm我的用户脚本加载jQuery? - How can I convince WebStorm that my userscript loads jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM