简体   繁体   English

脚本运行后如何在控制台中提供Tampermonkey添加的功能?

[英]How do I make functions added by Tampermonkey be available in console after the script has been ran?

I made a script that among other things has a function in it: 我创建了一个脚本,其中包含一个函数:

function updateGUI(){
    document.getElementById("cursoft").value = getSoftware();
    document.getElementById("curver").value = getCurrentVersion();
    document.getElementById("rcycles").value = getResearchCycles();
    document.getElementById("rcycle").value = getCurrentCycle();
    document.getElementById("curproc").value = getCurrentProcess();
    document.getElementById("curact").value = getCurrentAction();
}

The script runs on page load just fine, but when I try to run this function after the script finishes execution it's "undefined". 该脚本在页面加载时运行很好,但是当我尝试在脚本完成执行后运行此函数时,它是“未定义”。
How can I make it "stay" in the current scope? 如何让它“保持”在当前范围内?

Tampermonkey scripts are run in a separate scope. Tampermonkey脚本在单独的范围内运行。 This means that to make a function available globally you'll want to do something along the following: 这意味着要使全局可用的功能,您需要执行以下操作:

window.updateGUI = function () {...}

If you want to add a number of functions to the global scope, it's better to store them under a single object and address your functions through there. 如果要向全局范围添加许多函数,最好将它们存储在单个对象下并通过那里解决您的函数。 That helps avoid any possible collisions you might otherwise have. 这有助于避免您可能遇到的任何可能的冲突。

var myFunctions = window.myFunctions = {};
myFunctions.updateGUI = function () {...};

After that you can simply call myFunctions.updateGUI(); 之后你可以简单地调用myFunctions.updateGUI(); .

A better way do to that is to grant unsafeWindow . 更好的方法是授予unsafeWindow

// @grant       unsafeWindow

Then create your function like this: 然后像这样创建你的函数:

function test()
{
    console.log("test");
}

But also make it available in console through unsafeWindow : 但也可以通过unsafeWindow在控制台中使用它:

if(!unsafeWindow.test)
{
    unsafeWindow.test = test;
}

Then you can access test in the console. 然后,您可以在控制台中访问test

I found an answer to my question, but Nit's answer is superior. 我找到了我的问题的答案,但尼特的答案更胜一筹。 I will still post mine in case someone needs it. 如果有人需要,我仍然会发布我的。

You can add functions to global scope by adding them to a script element and appending it to body 您可以通过将函数添加到脚本元素并将其附加到正文来向全局范围添加函数

var scriptElem = document.createElement('script');
scriptElem.innerHTML = 'function updateGui() { /* stuff */ }';
document.body.appendChild(scriptElem);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在tampermonkey中制作脚本以自动单击 - How do i make a script in tampermonkey to auto click 添加字母后如何进行字母替换? - How to make letter replace after it has been added? 我如何使 Tampermonkey 脚本在网站上“单击”按钮? 我试过以前在这里问过的问题 - How do i make tampermonkey script will 'click' button on website? I tried question asked here previously 如何检查特定模块是否已添加到 Worklet? - How do I check if a specific module has been added to a Worklet? DOM 元素添加到 DOM 树后是否立即可用? - Is a DOM element available immediately after it has been added to the DOM tree? 我的一组函数执行完后该怎么办? - How to do something after my set of functions has been executed? 设置PHP查询字符串后,如何使jQuery脚本运行? - How do I make a jQuery script run once a PHP query string has been set? 如何在 webpack 开发服务器启动后自动运行 NPM 脚本? - How do I automatically run an NPM script after webpack dev server has been started? 如何使水平条形图的高度灵活,以便在添加新数据时调整大小? - How do I make the height of a horizontal bar chart flexible in order to be resized when new data has been added in? 即使脚本之前已经执行过,我们如何在控制台上访问变量和函数? - How can we access variables and functions on console even if the script has been executed way before?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM