安装
科学上网后直接在谷歌商店安装
官方文档地址
文档地址
编写脚本
- 油猴自带的编译器非常不好用,不想麻烦配置编译器建议直接把内容拷贝到VSCode中进行编辑。
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 2024-01-13
// @description try to take over the world!
// @author You
// @match https://github.com/Tampermonkey/tampermonkey
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Your code here...
})();
@match
字段定义了 这个脚本要在那些网址上运行,支持 *
号通配符
2. 当网址发生变化,如果这个脚本的@match
字段还是支持该网址,那么脚本将从头开始运行。
一个简单脚本
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 2024-01-10
// @description try to take over the world!
// @author You
// @match http://192.168.10.1/cgi-bin/luci*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function waitElement(selector, callback, all = false){
let ele;
if(all){
ele = document.querySelectorAll(selector);
if(ele.length > 0){
callback(ele);
}else{
console.log("not find");
setTimeout(()=>{
waitElement(selector, callback, all);
});
}
}else{
ele = document.querySelector(selector);
if(ele){
callback(ele);
}else{
setTimeout(()=>{
waitElement(selector, callback, all);
});
}
}
}
if(window.location.href === "http://192.168.10.1/cgi-bin/luci"){
waitElement("#password", function(element){
element.value = "ihr6pe3f";
});
waitElement("#submitID", function(element){
element.click();
});
}else if(window.location.href.match(/^(http:\/\/192\.168\.10\.1\/cgi-bin\/luci\/;stok=\w+)$/g)){
waitElement(".client.block_m.mouse", function(element){
element.click();
});
}else if(window.location.href.match(/^(http:\/\/192.168.10.1\/cgi-bin\/luci\/;stok=\w+\/CMCC\/advanceSetup\/onlineDevices)$/g)){
waitElement("#list tr", function(element){
for(let i = 0; i < element.length; ++i){
if(element[i].id !== "20:F4:78:0F:21:9F" && element[i].id !== "F2:55:C4:F4:70:C4" && element[i].id !== "38:BA:F8:85:4D:37"){
let btn = element[i].querySelector("#btn_id");
if(btn && btn.classList.length >= 2 && btn.classList[1] === "btn-on"){
btn.click();
}
}
}
}, true);
}
})();