Tampermonkey js/jquery/ajax 怎么加个sleep?
Tampermonkey js/jquery/ajax how to add a sleep?
对于像这样的页面
https://www.converto.io/?v=EbuYMynCWV8,
我有一个 Tampermonkey 脚本可以自动执行:
- 选择mp4格式
- 点击转换按钮。
可以用,但有时可能太快了,最后我得到了mp3格式的下载链接。
所以现在我想在两个步骤之间加入一个睡眠时间。测试结果是代码只完成了第一步。有什么想法吗?
我的代码:
// ==UserScript==
// @name _ConverTo, Automatically select mp4
// @match https://www.converto.io/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.
waitForKeyElements (".format-select:has(option[value=mp4])", selectFinickyDropdown); //------ step 1,choose mp4 format------
function selectFinickyDropdown (jNode) {
var evt = new Event ("click");
jNode[0].dispatchEvent (evt);
jNode.val('mp4');
evt = new Event ("change");
jNode[0].dispatchEvent (evt);
setTimeout("secondStep()", 10000); //--- sleep 10s then step 2,click CONVERT button -------
}
function secondStep() {
waitForKeyElements (".convert-btn", clickbuttonconvert);
}
function clickbuttonconvert (jNode) {
var evt = new Event ("click");
jNode[0].dispatchEvent (evt);
}
从 setTimeout
调用中删除引号和括号,第一个参数应该是函数本身。
setTimeout(secondStep, 10000);
对于像这样的页面 https://www.converto.io/?v=EbuYMynCWV8, 我有一个 Tampermonkey 脚本可以自动执行:
- 选择mp4格式
- 点击转换按钮。
可以用,但有时可能太快了,最后我得到了mp3格式的下载链接。
所以现在我想在两个步骤之间加入一个睡眠时间。测试结果是代码只完成了第一步。有什么想法吗?
我的代码:
// ==UserScript==
// @name _ConverTo, Automatically select mp4
// @match https://www.converto.io/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.
waitForKeyElements (".format-select:has(option[value=mp4])", selectFinickyDropdown); //------ step 1,choose mp4 format------
function selectFinickyDropdown (jNode) {
var evt = new Event ("click");
jNode[0].dispatchEvent (evt);
jNode.val('mp4');
evt = new Event ("change");
jNode[0].dispatchEvent (evt);
setTimeout("secondStep()", 10000); //--- sleep 10s then step 2,click CONVERT button -------
}
function secondStep() {
waitForKeyElements (".convert-btn", clickbuttonconvert);
}
function clickbuttonconvert (jNode) {
var evt = new Event ("click");
jNode[0].dispatchEvent (evt);
}
从 setTimeout
调用中删除引号和括号,第一个参数应该是函数本身。
setTimeout(secondStep, 10000);