I'm writing a personal Chrome extension that modifies various things (i.e. styles) on various websites. That includes Stack Exchange. My extension is enabled on all websites so I have to differentiate between them based on window.location
.
Currently I'm doing it like this:
// 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
}
I do it like this because stackexchange.com
is a suffix for many subdomains and it gets them all. But some communities have their own domain like superuser.com
or askubuntu.com
and I have to include them manually.
Is there some information source (maybe response headers?) that connects all those websites?
I have already set up a background script, so I can message it to do something using available Chrome APIs, if that can help.
I don't think there's anything simple you can do to automatically detect these domains. There are only a handful of domains, it's easier to just list them explicitly.
Here's the list from a Tampermonkey script I've installed.
// @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/*