如何在一系列网站上将用户脚本设置为 运行?

How to set a userscript to run on an array of websites?

我想更改一些网站的背景,因为我讨厌当前的背景,但为了这样做我不得不使用 document.location.host。我有一个很大的 js 文件,其中包含另一个站点,所以我不能只为这个创建另一个 js。唯一的问题是站点将 example.com 重定向到 example1.com、example2.com、example3.com 等等。

所以我可以使用

if (document.location.host === example1.com || example2.com ||  example3.com)

但我想要更稳定的东西,这样我就不需要并排写 1000 个网址,例如 example1.com - example1255.com。一定是

if (document.location.host === example[*].com)

现在,我知道这是可能的,因为我以前做过。我想我是用 i++ 方法做的,但我不记得这是我的问题。

如何使用数组获取网站?或者更像是;如何选择带有数组的网站?

P.S。我不想用时髦什么的! 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!");