Code works in console but not in user script

In the following website, this code works in console but not in userscript.

Console code

if (window.location.href = "n12.co.il") {
    alert("Hi");
}

User script code

// ==UserScript==
// @name         New Userscript
// @match        https://*
// ==/UserScript==

window.addEventListener('load', () => {
    if (window.location.href = "n12.co.il") {
        alert("Hi");
    }
});

What may cause this situation?

Is this a *Monkey script?

Tampermonkey (Chrome).

So most likely this is because Tampermonkey scripts default to running at DOMContentLoad time, which means that the load event has already passed. If you need it to run at load time, tell the script to @run-at document-start ; otherwise, just call your function normally: (function() { //Your Code here })();

After reading your post I have tried both solutions but sadly they don’t work, the code still doesn’t run on that website.

So there’s two major problems with that line.

  1. You’ve confused assignment and comparison. = is not ==.
  2. window.location.href contains the protocol and any filenames attached to it; as such, it will never match “n12.co.il” exactly. You may be trying to do an includes test.
2 Likes

That is a beginner mistake that I think even experienced programmers make. I do not know if it works in JavaScript (I assume so) but some programmers do something as in the following.

if ("n12.co.il" == window.location.href)

And then if (and only if) = is used instead of == then that is a syntax error.

2 Likes

I thank you both @m_hutley and @SamuelCalifornia

Indeed I tend to confuse assignment (=) with comparison (== or ===).

I wish that there were unique characters to both actions as it may have been reducing the chance for people to confuse the two.

I gladly share the working code, tested twice on n12.co.il and successfully blocking this website:

// ==UserScript==
// @name         New Userscript
// @match        https://*
// @run-at      document-start
// ==/UserScript==

if (
  window.location.href.includes('n12.co.il')
) {
  window.open("https://google.com/", "_self");
}
1 Like

Can’t you use the @match to determine when to run the script instead of a manual if statement?

That way the script is run only for domains that match instead of every domain.

I shouldn’t in this case because the actual script is larger and does more actions on various websites (executed on any website), for example, for the sake of blocking any website that has the word “News” or “news” in its <title> or <meta> tags.

The N12 is just an example for an exception that doesn’t have such words in the HTML source code and should be clocked by a web domain.

Now the larger script covers N12 without an exception, after adding terms like חדשות and החדשות (“news” and “the news” in Hebrew) so there is no need in checking a web domain for this particular case, but such web domain based exceptions may still be needed.

1 Like

It is a problem with the C programming language and even FORTRAN I think. It has been half a century since I wrote a FORTRAN program but see Fortran - Operators. It is one of those things that developers just must be aware of. The good news is that once you are accustomed to it your experience is likely relevant to other languages you learn.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.