84864694 发表于 2022-7-7 23:15:29

针对多个网站的脚本怎么实现匹配执行

本帖最后由 84864694 于 2022-7-8 00:02 编辑

我是一个小白,想请问一下,如果一个油猴脚本是针对多个网站的,怎么让相应的代码在相应的网站执行,而另外一些代码不会被执行。比如:@match http://www.faxuanyun.com/bps/frame/t/frame.html?loginUserAccount=*                     @match http://www.faxuanyun.com/sps/courseware/t/courseware_1_t.html?c=*&loginUserAccount=*       是匹配这两个网站的,代码肯定是写在一个脚本里的,怎么让针对第一个网站的代码不会在后面的网站执行

steven026 发表于 2022-7-7 23:15:30

if(window.location.href.indexOf("http://www.faxuanyun.com/bps/frame/t/frame.html")>-1){
    函数1()
}

if(window.location.href.indexOf("http://www.faxuanyun.com/sps/courseware/t/courseware_1_t.html")>-1){
    函数2()
}

84864694 发表于 2022-7-8 00:03:41

steven026 发表于 2022-7-7 23:27
if(window.location.href.indexOf("http://www.faxuanyun.com/bps/frame/t/frame.html")>-1){
    函数1()
...

谢谢,ggnb

Ne-21 发表于 2022-7-8 09:56:32

同一主域名下可以通过if来匹配location.pathname

84864694 发表于 2022-7-8 14:17:03

Ne-21 发表于 2022-7-8 09:56
同一主域名下可以通过if来匹配location.pathname

谢谢大佬

青空玄想 发表于 2022-7-26 22:43:17

document.URL也行啊{:4_93:}
页: [1]
查看完整版本: 针对多个网站的脚本怎么实现匹配执行