steven026 发表于 2022-9-10 18:54
[md]```
// ==UserScript==
// @name New Userscript
不是这样。
比如说原来的是这样的写法:
// ==UserScript==
// @name New Userscript
// @version 1.0
// @description try to take over the world!
// @include http*://bbs.tampermonkey.net.cn/*
// @include https://m.hafuktxt.com/chapter/*/*
// ==/UserScript==
if(location.href.includes("bbs.tampermonkey.net.cn")){
document.querySelectorAll("#hd #comiis_nv a")?.[6]?.click();
}
if (location.href.includes("https://m.hafuktxt.com/chapter/*/*")) {
document
.querySelectorAll("#__dfdsdefsdb")
?.forEach((el) => (el.style.display = "none"));
document.querySelector(
"#chaptercontent"
).previousElementSibling.style.display = "none";
document.querySelector("#chaptercontent").nextElementSibling.style.display =
"none";
}
比如说现在是这样
utils.js
function parseComment(comment) {
comment = comment.trim().substring(2).trim();
if (comment.startsWith("@")) {
const index = comment.indexOf(" ");
if (index === -1) return [comment.substring(1), ""];
return [comment.substring(1, index), comment.substring(index).trim()];
}
return ["", ""];
}
function AtInclude(value){
// 需要在此实现@include命中功能
return true;
}
function isInclude(comment){
const [key, value] = parseComment(comment);
if(key !== 'include') return false;
return AtInclude(value);
}
function include(callback){
const comments = callback.toString().match(/^[^"'`\n]+\/\/(.*)/gm);
if (!comments || !comments.some(isInclude)) return;
callback();
}
main.js
// @version 1.2
import { include } from './utils'
include(() => {
// @include http*://bbs.tampermonkey.net.cn/*
document.querySelectorAll('#hd #comiis_nv a')?.[6]?.click()
})
include(() => {
// @include https://m.hafuktxt.com/chapter/*/*
document.querySelectorAll('#__dfdsdefsdb')?.forEach( el => el.style.display = 'none');
document.querySelector('#chaptercontent').previousElementSibling.style.display = 'none';
document.querySelector('#chaptercontent').nextElementSibling.style.display = 'none';
})
使用rollup插件打包之后会自动把// @include这一行提到头部,大概就是
// ==UserScript==
// @name New Userscript
// @version 1.2
// @description try to take over the world!
// @include http*://bbs.tampermonkey.net.cn/*
// @include https://m.hafuktxt.com/chapter/*/*
// ==/UserScript==
function parseComment(comment) {
comment = comment.trim().substring(2).trim();
if (comment.startsWith("@")) {
const index = comment.indexOf(" ");
if (index === -1) return [comment.substring(1), ""];
return [comment.substring(1, index), comment.substring(index).trim()];
}
return ["", ""];
}
function AtInclude(value){
// 需要在此实现@include命中功能
return true;
}
function isInclude(comment){
const [key, value] = parseComment(comment);
if(key !== 'include') return false;
return AtInclude(value);
}
function include(callback){
const comments = callback.toString().match(/^[^"'`\n]+\/\/(.*)/gm);
if (!comments || !comments.some(isInclude)) return;
callback();
}
include(() => {
// @include http*://bbs.tampermonkey.net.cn/*
document.querySelectorAll('#hd #comiis_nv a')?.[6]?.click()
})
include(() => {
// @include https://m.hafuktxt.com/chapter/*/*
document.querySelectorAll('#__dfdsdefsdb')?.forEach( el => el.style.display = 'none');
document.querySelector('#chaptercontent').previousElementSibling.style.display = 'none';
document.querySelector('#chaptercontent').nextElementSibling.style.display = 'none';
})
所以我需要完成@include和@match的命中逻辑