admin管理员组文章数量:1326081
I wrote an Greasemonkey/Tampermonkey script with this code:
var on = true;
function doSomething(){
if(on){
//do stuff
}
}
setInterval(doSomething,1000);
Normally the function is active but for some few cases, I want to disable it from the javascript console in the browser.
I do not want to add an extra toggle button to the webpage.
How can I achieve this? Entering on = true
, in the console, does not work because on
is "not defined".
I wrote an Greasemonkey/Tampermonkey script with this code:
var on = true;
function doSomething(){
if(on){
//do stuff
}
}
setInterval(doSomething,1000);
Normally the function is active but for some few cases, I want to disable it from the javascript console in the browser.
I do not want to add an extra toggle button to the webpage.
How can I achieve this? Entering on = true
, in the console, does not work because on
is "not defined".
- Not sure what you want to do here, but you can add a line in your script: "debugger;". When you are in google chrome devtools(F12), then you can refresh the webpage and the script will stop at the point where you have that line. Then you can open the JS console and script "live" on the page. – patchie Commented Oct 9, 2013 at 14:07
1 Answer
Reset to default 6Greasemonkey, and Tampermonkey, operate in separate scopes from the target page, and may use a sandbox as well.
The target page and JS console can not see variables and functions defined in the script scope, but your script can inject stuff into the target page's scope.
So, place your on
variable in the target page scope, and you can then control that function from the console. In this case, use unsafeWindow
Doc to do that.
A plete Greasemonkey and Tampermonkey script that does that is:
// ==UserScript==
// @name _Allow console control of Tampermonkey function
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant unsafeWindow
// ==/UserScript==
unsafeWindow.on = true;
function doSomething () {
if (unsafeWindow.on){
//do stuff
}
}
setInterval (doSomething, 1000);
From the console, omit the unsafeWindow
. That is, you would use:
on = false;
// OR
on = true;
to stop and start that script's action.
Note that, in Chrome, the script must be running in Tampermonkey, not Chrome's native userscript emulation.
版权声明:本文标题:javascript - How to control a Tampermonkey script's function from the browser's JS console? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742195553a2431013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论