0

I tried to write a Greasemonkey userscript that checks to see if the user is in one of a list of websites.

If the user is indeed in one of them, the script will alert:

Enough with this domain already!

The purpose of the script is to remind the user that he stop visiting this site (addiction-like behavior).

The output should include only the domain, without the TLD.

I have tried the following code which failed (the code runs on a collection of tlds and uses the collection to strip these away):

let sites = ['walla.com', 'mako.co.il'];
let tlds = new RegExp('\.+(com|co.il)');

for (let i = 0; i < sites.length; i++) {
    if (window.location.href.indexOf(sites[i]) != -1 ) {
        sites.forEach((e)=>{
            e.replace(tlds, '').split('.').pop(),
        });

     alert(` Enough with this ${sites[i]} already! `);
    }
}

No console errors.

To reproduce, install the script in Greasemoneky/Tampermonkey and try it in the listed sites.

OsiOs
  • 39
  • 10

1 Answers1

1

You should iterate the sites, and if the href contains the site (sites[i]), replace everything after the domain (the don onward), alert, and break the loop:

const sites = ['walla.com', 'mako.co.il'];
const regex = /\..+/;
const href = 'http://www.mako.co.il/news?partner=NavBar'; // this replace window.location.href for demo purposes

for (let i = 0; i < sites.length; i++) {
  if (href.includes(sites[i])) { // if the href includes the sites[i]
    const domain = sites[i].replace(regex, ''); // remove the 1st dot and everything after it to get the domain name

    alert(` Enough with this ${domain} already! `);
    
    break; // or return if in a function
  }
}
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • Shalom Ori. In great plea, please elaborate more on the difference between what you did here and what I did. You say you "replaced all from the dot onward" but I miss what you exactly mean. I see you moved the regex variable to inside the if and added a new variable with a specific domain (why, I humbly want this to be flexible as possible). – OsiOs Oct 10 '17 at 22:17
  • I've updated the code, and added comments. Is it clearer now? Is the end result what you were looking for? – Ori Drori Oct 10 '17 at 22:22
  • It is much clearer now, thank you dearly! I also assume that in `href` variable, instead mako.co.il we can put a regex `[a-zA-Z]\..+` to give a more "general purpose". – OsiOs Oct 10 '17 at 22:32
  • About the purpose of the script --- It was just important for me to learn how to print only the domain of the site, because it seems neater. I tried several things that failed. The script just helps me to handle an addiction-like behavior I personally have with some sites. – OsiOs Oct 10 '17 at 22:33
  • The `href` variable replaces the `window.location.href` for the demo. In your code you should use `window.location.href`. – Ori Drori Oct 10 '17 at 22:34
  • But there is one problem: The script runs on other sites as well, maybe because you removed the domain checking of domain+tld in the `sites` variable. I will try to bring it back now. – OsiOs Oct 10 '17 at 22:35
  • Another thing - whenever you can use `const`. Use `let` only if you really need to mutate the value. – Ori Drori Oct 10 '17 at 22:35
  • Oh, just put there `window.location.href` so to check `sites` against current location href. Okay. – OsiOs Oct 10 '17 at 22:36
  • The tlds wasn't doing anything. String#replace returns a new string, and I've assumed that you wanted to remove everything but the domain. – Ori Drori Oct 10 '17 at 22:36
  • Indeed. It works in my test fine! I will read it and analyze it! I suggest a edit: Just replace `const href = 'http://www.mako.co.il/news?partner=NavBar';` to `window.location.href` which makes the script valid only for the sites in the array. – OsiOs Oct 10 '17 at 22:46
  • Ignore `const href = 'http://www.mako.co.il/news?partner=NavBar';`. It's just to make the demo work. Use `window.location.href` in your code. – Ori Drori Oct 10 '17 at 22:48
  • Yes, exactly, I didn't understand it has to do with the StackExchange demo widget (which I never used before). Sorry... – OsiOs Oct 10 '17 at 23:10