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 = "") {

User script code

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

window.addEventListener('load', () => {
    if (window.location.href = "") {

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 “” exactly. You may be trying to do an includes test.
1 Like

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 ("" == window.location.href)

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

1 Like