原文网址:油猴--实例_IT利刃出鞘的博客-CSDN博客
输出hello world
代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16// ==UserScript== // @name hello world // @namespace http://tampermonkey.net/ // @version 0.1 // @description try to take over the world! // @author You // @match *://*/* // @include * // @run-at document-end // ==/UserScript== (function() { 'use strict'; alert('hello world'); })();
等待指定元素出现
其他网址
jQuery下实现等待指定元素加载完毕_win_turn的博客-CSDN博客_jquery等待页面加载完成
jQuery下实现等待指定元素加载完毕 - 楼教主 - 博客园
简介
页面中的一些元素是ajax请求的,不是一开始就在页面里的。所以需要通过判断元素是否加载成功。
实例
需求:id为boxQuit的元素是动态生成的,等它出现后,点击它。
js写法
思路
使用promise
代码
函数
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22function waitElement(selector, times, interval, flag = true) { var _times = times || 50, // 默认50次 _interval = interval || 100, // 默认每次间隔100毫秒 _selector = selector, //选择器 _iIntervalID, _flag = flag; //定时器id return new Promise(function (resolve, reject) { _iIntervalID = setInterval(function () { if (!_times) { //是0就退出 clearInterval(_iIntervalID); reject(); } _times <= 0 || _times--; //如果是正数就 -- var _self = document.querySelector(_selector); //再次选择 if ((_flag && _self) || (!_flag && !_self)) { //判断是否取到 clearInterval(_iIntervalID); resolve(_self); } }, _interval); }); }
调用方法
复制代码
1
2
3
4waitElement('#boxQuit') .then(result => { result.click(); })
jQuery写法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28jQuery.fn.wait = function (func, times, interval) { var _times = times || 100, //100次 _interval = interval || 20, //20毫秒每次 _self = this, _selector = this.selector, //选择器 _iIntervalID; //定时器id if( this.length ){ //如果已经获取到了,就直接执行函数 func && func.call(this); } else { _iIntervalID = setInterval(function() { if(!_times) { //是0就退出 clearInterval(_iIntervalID); } _times <= 0 || _times--; //如果是正数就 -- _self = $(_selector); //再次选择 if( _self.length ) { //判断是否取到 func && func.call(_self); clearInterval(_iIntervalID); } }, _interval); } return this; } $("#boxQuit").wait(function(){ $('#boxQuit')[0].click(); //这里不能这么写:$('#boxQuit').click(); });
其他网址
如何开发一个油猴脚本- 从零开始编写一个油猴脚本_kesyuepng的博客-CSDN博客
油猴脚本编写规则_Senreme-CSDN博客
编写你的第一个油猴脚本_XavierJ的博客-CSDN博客
最后
以上就是俏皮自行车最近收集整理的关于油猴--实例输出hello world等待指定元素出现其他网址的全部内容,更多相关油猴--实例输出hello内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复