1

I want to change some website's background since I hate the current one but to do so I got to use document.location.host. I got a big js file includes another sites so I can't just create another js for this one. Only problem is that site redirects example.com to example1.com, example2.com, example3.com and so on.

So I can use

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

but I want something more steady so I wouldn't need to write 1000 urls side by side, like example1.com - example1255.com. It must be like

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

Now, I know for a fact that this is possible because I did it before. I guess I did it by using i++ method but I don't remember how so this is my question.

How can I use an array to get website? Or is it more like; How can I choose a website with and array?

P.S. I don't want to use stylish or something! P.P.S. I don't need an equality check. The whole idea is not writing those values but making the script do it.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
munzuradam
  • 13
  • 5
  • 2
    Use a regular expression. If you post the actual site names, it would be easier to come up with one. – CertainPerformance May 13 '18 at 23:47
  • 4
    `["example1.com", "example2.com","stackoverflow.com"].includes(document.location.host)` – Keith May 13 '18 at 23:49
  • @RandyCasburn do we really need another answer when there are probably dozens like this already? https://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values – VLAZ May 13 '18 at 23:56
  • Possible duplicate of [Check variable equality against a list of values](https://stackoverflow.com/questions/4728144/check-variable-equality-against-a-list-of-values) – VLAZ May 13 '18 at 23:56
  • @vlaz - concur. – Randy Casburn May 13 '18 at 23:58
  • @RandyCasburn I don't need it to check already writen values. The whole idea was me not writing all those values. Like I said I could use if (document.location.host === example1.com || example2.com || example3.com .... || example1255.com) – munzuradam May 14 '18 at 00:04
  • `/^example\d+\.com$/.test(document.location.host)` ~ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test – Phil May 14 '18 at 00:34

1 Answers1

1

For Tampermonkey, you should use the @include, @match, and @exclude directives to control what pages the script runs on as much as possible.
This increases performance and reduces the risk of side effects.

Tampermonkey also supports limited regex in @include (different from pattern matching available in @match), so you can use a script like:

// ==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!");
Brock Adams
  • 90,639
  • 22
  • 233
  • 295