我正在编写一个个人 Chrome 扩展程序,可以修改各个网站上的各种内容(即样式)。其中包括 Stack Exchange。我的扩展程序已在所有网站上启用,因此我必须根据
window.location
来区分它们。
目前我正在这样做:
// code executed before load
const host = window.location.hostname;
function hostIncludesAny(...strList) {
for (const str of strList) {
if (host.includes(str)) {
return true;
}
}
return false;
}
// ...
// more ifs here
// ...
if (hostIncludesAny("stackoverflow.com", "stackexchange.com", "serverfault.com", "superuser.com", "askubuntu.com")) {
// apply styles for Stack Exchange network
}
我这样做是因为
stackexchange.com
是许多子域的后缀,它可以获取所有子域。但有些社区有自己的域名,例如 superuser.com
或 askubuntu.com
,我必须手动包含它们。
是否有一些信息源(也许是响应标头?)连接所有这些网站?
我已经设置了一个后台脚本,因此我可以向它发送消息以使用可用的 Chrome API 执行某些操作(如果有帮助的话)。
我认为没有什么简单的方法可以自动检测这些域。只有少数域,直接列出它们会更容易。
这是我安装的 Tampermonkey 脚本的列表。
// @match *://*.stackexchange.com/*
// @match *://*.stackoverflow.com/*
// @match *://*.superuser.com/*
// @match *://*.serverfault.com/*
// @match *://*.askubuntu.com/*
// @match *://*.stackapps.com/*
// @match *://*.mathoverflow.net/*
// @exclude *://api.stackexchange.com/*
// @exclude *://blog.*.com/*
// @exclude *://chat.*.com/*
// @exclude *://data.stackexchange.com/*
// @exclude *://elections.stackexchange.com/*
// @exclude *://openid.stackexchange.com/*
// @exclude *://stackexchange.com/*