我想更改某些网站的背景,因为我讨厌当前网站的背景,但为此,我必须使用document.location.host。我有一个很大的js文件,其中包含另一个网站,所以我不能只为此创建另一个js。唯一的问题是该站点将example.com重定向到example1.com,example2.com,example3.com等。
所以我可以用
if (document.location.host === example1.com || example2.com || example3.com)
但是我想要更稳定的东西,这样就不需要并排编写1000个url,例如example1.com-example1255.com。一定像
if (document.location.host === example[*].com)
现在,我知道这是可能的,因为我以前做过。我想我是通过使用i ++方法做到的,但是我不记得这是我的问题了。
如何使用数组获取网站?或者更像是;如何选择带有和数组的网站?
附言我不想使用时尚的东西!
P.P.S.我不需要平等检查。整个想法不是写这些值而是让脚本执行这些值。
最佳答案
对于Tampermonkey,应使用the @include
, @match
, and @exclude
directives来控制脚本尽可能在哪些页面上运行。
这样可以提高性能并减少副作用的风险。
Tampermonkey还支持@include
中的有限正则表达式(与@match
中可用的模式匹配不同),因此您可以使用如下脚本:
// ==UserScript==
// @name _Run on a subset of pages
// @include *://example*.com/*
// @grant none
// ==/UserScript==
//-- Additional check to make sure server is of form example{some optional number}:
if ( ! /example\d*\.com/.test (location.hostname) )
return;
console.log ("Hello world!");
关于javascript - 如何设置用户脚本以在一系列网站上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50321608/