[JavaScript] 纯文本查看 复制代码
// ==UserScript==
// [url=home.php?mod=space&uid=170990]@name[/url] aliyun_slide
// [url=home.php?mod=space&uid=467642]@namespace[/url] [url=https://www.yuban.ltd/]https://www.yuban.ltd/[/url]
// [url=home.php?mod=space&uid=1248337]@version[/url] 0.0.1
// @description 阿里云滑块自动滑动
// [url=home.php?mod=space&uid=686208]@AuThor[/url] RenJie
// [url=home.php?mod=space&uid=697773]@include[/url] /[a-zA-z]+://[^\s]*/
// [url=home.php?mod=space&uid=609072]@grant[/url] none
// ==/UserScript==
(function () {
'use strict';
/**
* 休眠
* [url=home.php?mod=space&uid=952169]@Param[/url] time 休眠时间,单位秒
* @param desc
* @returns {Promise<unknown>}
*/
function sleep(time, desc = 'sleep') {
return new Promise(resolve => {
//sleep
setTimeout(() => {
console.log(desc, time, 's')
resolve(time)
}, Math.floor(time * 1000))
})
}
/**
* 监测节点是否存在
* @param selector CSS选择器
* @param desc
* @returns {Promise<unknown>}
*/
function obsHas(selector, desc = 'has') {
return new Promise(resolve => {
//obs node
let timer = setInterval(() => {
let target = document.querySelector(selector)
if (!!target) {
clearInterval(timer)
console.log(desc, selector)
resolve(selector)
} else {
return
}
}, 100)
})
}
function slide(id) {
var slider = document.getElementById(id),
container = slider.parentNode;
var rect = slider.getBoundingClientRect(),
x0 = rect.x || rect.left,
y0 = rect.y || rect.top,
w = container.getBoundingClientRect().width,
x1 = x0 + w,
y1 = y0;
var mousedown = document.createEvent("MouseEvents");
mousedown.initMouseEvent("mousedown", true, true, window, 0,
x0, y0, x0, y0, false, false, false, false, 0, null);
slider.dispatchEvent(mousedown);
var mousemove = document.createEvent("MouseEvents");
mousemove.initMouseEvent("mousemove", true, true, window, 0,
x1, y1, x1, y1, false, false, false, false, 0, null);
slider.dispatchEvent(mousemove);
}
sleep(1)
.then(() => obsHas('.nc_wrapper'))
.then(() => slide('nc_1_n1z'))
})();