上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
12下一页
返回列表 发新帖

不想动脑子了,大佬们请进,求教油猴的include是如何实现的

[复制链接]
  • TA的每日心情
    无聊
    2022-8-21 01:21
  • 签到天数: 1 天

    [LV.1]初来乍到

    7

    主题

    58

    回帖

    63

    积分

    初级工程师

    积分
    63
    发表于 2022-9-10 16:46:53 | 显示全部楼层 | 阅读模式

    起因如下:
    目前已经有一个rollup-plugin实现注释提升到头部。但是如果是多网站的话是需要判断具体是哪一个网站的,对应的网站运行对应的代码。
    所以我想着,既然能注释提升到头部,那么只要在写代码的时候把注释写到函数内部,然后进行判断就可以省去手动判断网站了,达到一注释多用的目的。
    目前已经能拿到注释内容,接下来就是实现油猴的@include来做判断是否命中网站,但是由于我没怎么写过油猴脚本[之前只是写的类似via脚本不需要@include功能],所以需要大佬们提供一下@include实现源码或者具体思路。
    附当前代码:

    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()
    })
  • TA的每日心情
    开心
    2023-2-28 23:59
  • 签到天数: 191 天

    [LV.7]常住居民III

    633

    主题

    5173

    回帖

    6052

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    6052

    荣誉开发者管理员油中2周年生态建设者喜迎中秋

    发表于 2022-9-10 18:05:23 | 显示全部楼层
    @王一之 太硬了,我答不动,王总上
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.net/a/lihengdao666
    个人宣言:この世界で私に胜てる人とコードはまだ生まれていません。死ぬのが怖くなければ来てください。
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    20 小时前
  • 签到天数: 625 天

    [LV.9]以坛为家II

    30

    主题

    532

    回帖

    1400

    积分

    荣誉开发者

    积分
    1400

    荣誉开发者新人进步奖油中2周年生态建设者新人报道挑战者 lv2油中3周年喜迎中秋

    发表于 2022-9-10 18:54:01 | 显示全部楼层

    本帖最后由 steven026 于 2022-9-10 19:01 编辑

    // ==UserScript==
    // @name         New Userscript
    // @version      1.0
    // @description  try to take over the world!
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
    
    AtInclude(() => {
        // @include http*://bbs.tampermonkey.net.cn/*
        alert("AtInclude: true")
    })
    
    //////////////////////
    
    function AtInclude(value){
        let commentHref=value.toString().match(/\/\/ @(include|match) (.+)\n/)?.[2];
        let regHref=new RegExp(commentHref?.replace(/\?/g,".")?.replace(/\*/g,".*?"));
        if(!regHref.test(location.href)) return;
        value();
    }

    这样?
    一般油猴同时匹配多个网址又要针对不同网址运行不同代码时,可以通过对比location.href来判断网址,然后调用相应函数
    这样写太复杂了,一般都是这么写

    // ==UserScript==
    // @name         New Userscript
    // @version      1.0
    // @description  try to take over the world!
    // @match        *://*/*
    // @grant        none
    // ==/UserScript==
    
    if(location.href.includes("bbs.tampermonkey.net.cn")){
        油中()
    }
    
    function 油中(){
        alert("bbs.tampermonkey.net.cn")
        //code here
    }
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2022-8-21 01:21
  • 签到天数: 1 天

    [LV.1]初来乍到

    7

    主题

    58

    回帖

    63

    积分

    初级工程师

    积分
    63
    发表于 2022-9-10 20:05:33 | 显示全部楼层

    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的命中逻辑

    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    20 小时前
  • 签到天数: 625 天

    [LV.9]以坛为家II

    30

    主题

    532

    回帖

    1400

    积分

    荣誉开发者

    积分
    1400

    荣誉开发者新人进步奖油中2周年生态建设者新人报道挑战者 lv2油中3周年喜迎中秋

    发表于 2022-9-10 20:24:50 | 显示全部楼层

    笑尘天雨 发表于 2022-9-10 20:05

    [md]不是这样。
    比如说原来的是这样的写法:

    // ==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){
        let regHref=new RegExp(value?.replace(/\?/g,".")?.replace(/\*/g,".*?"));
        if(regHref.test(location.href)) 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';
    })
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2022-8-21 01:21
  • 签到天数: 1 天

    [LV.1]初来乍到

    7

    主题

    58

    回帖

    63

    积分

    初级工程师

    积分
    63
    发表于 2022-9-10 21:53:22 | 显示全部楼层
    steven026 发表于 2022-9-10 20:24
    [md]```
    // ==UserScript==
    // @name         New Userscript

    @match和@include有什么不同吗?还有@exclude
    是之前考虑不周了,这三个得同时实现才行,不然和tampermonkey的命中规则不一样。
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    20 小时前
  • 签到天数: 625 天

    [LV.9]以坛为家II

    30

    主题

    532

    回帖

    1400

    积分

    荣誉开发者

    积分
    1400

    荣誉开发者新人进步奖油中2周年生态建设者新人报道挑战者 lv2油中3周年喜迎中秋

    发表于 2022-9-10 22:30:00 | 显示全部楼层
    笑尘天雨 发表于 2022-9-10 21:53
    @match和@include有什么不同吗?还有@exclude
    是之前考虑不周了,这三个得同时实现才行,不然和tampermon ...

    大概就是@include的*匹配不安全可能会被钓鱼,@match的*匹配限制更严格
    油猴的eslint说 明年年初@include可能会被弃用建议用@match
    @exclude是匹配黑名单,从@match或者@include匹配结果中剔除
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2022-8-21 01:21
  • 签到天数: 1 天

    [LV.1]初来乍到

    7

    主题

    58

    回帖

    63

    积分

    初级工程师

    积分
    63
    发表于 2022-9-10 22:47:10 | 显示全部楼层
    steven026 发表于 2022-9-10 22:30
    大概就是@include的*匹配不安全可能会被钓鱼,@match的*匹配限制更严格
    油猴的eslint说 明年年初@include ...

    为啥要把[?]替换成[.]?
    @match咋写
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    290

    主题

    3884

    回帖

    3800

    积分

    管理员

    积分
    3800

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2022-9-11 00:30:55 | 显示全部楼层
    推荐使用@match

    https://docs.scriptcat.org/dev/meta.html#match

    实现的话,我是分为四段使用正则实现的

    [http]://[domain]/[path]?[query]
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    20 小时前
  • 签到天数: 625 天

    [LV.9]以坛为家II

    30

    主题

    532

    回帖

    1400

    积分

    荣誉开发者

    积分
    1400

    荣誉开发者新人进步奖油中2周年生态建设者新人报道挑战者 lv2油中3周年喜迎中秋

    发表于 2022-9-11 11:39:40 | 显示全部楼层

    本帖最后由 steven026 于 2022-9-11 12:00 编辑

    本帖最后由 steven026 于 2022-9-11 11:55 编辑

    本帖最后由 steven026 于 2022-9-11 11:54 编辑

    因为用的是正则去判断网址,通配符的?在正则里面就是.

    @match和@include主要有2个区别
    一是通配符*的区别,写成正则形式应该是一样的
    不建议用@include,比如@include *bbs.tampermonkey.net.cn*可能会被https://钓鱼网站.com/bbs.tampermonkey.net.cn钓鱼
    如果写成@include *://bbs.tampermonkey.net.cn/*就不会被钓鱼,但写法就和@match一样了

    (还有一个是油猴include可以用正则、match不能用正则;脚本猫include、match都不能用正则)

    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表