本帖最后由 iamqz 于 2022-12-22 13:57 编辑
异步延迟 测试例子:
多次间隔地点击baidu.com首页右侧百度热搜的换一换按钮
// ==UserScript==
// @name 测试油猴4
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match *://*/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=baidu.com
// @grant none
// ==/UserScript==
function log(msg){
console.log(msg)
}
function sleep1(time) {
time*=1000
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, time);
});
}
// 油猴主函数,加上async 改成异步函数; 在内部使用await sleep()来调用异步延迟
(async function() {
'use strict';
// Your code here...
log(`异步测试油猴1`)
await sleep1(3)
document.querySelector("a.c-font-normal.c-color-gray2.hot-refresh").click()
log(`异步测试油猴2`)
await sleep1(3)
document.querySelector("a.c-font-normal.c-color-gray2.hot-refresh").click()
})();