[英]How do I make my userscript execute code in isolated sandbox and unsafeWindow too?
對於我的用戶腳本中的大部分代碼,我需要在我的腳本執行的網站上使用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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.