A script works from console but will work from userscript manager only with a specific @match command

I have the problem that a script works from console but will work from userscript manager only with a specific enough @match command. A general @match command won’t be enough.

Works from console but not from user script manager

// ==UserScript==
// @name       example
// @run-at      document-start
// @match       *://*/*

// ==/UserScript==

if ( window.location.href.includes('https://A_VERY_SPECIFIC_PATH') ) {
    window.open("https://google.com/", "_self");
}

Works both form console and from a user script manager

// ==UserScript==
// @name       example
// @run-at      document-start
// @match       *://*/*
// @match       https://A_VERY_SPECIFIC_PATH/*
// ==/UserScript==

if ( window.location.href.includes('https://A_VERY_SPECIFIC_PATH') ) {
    window.open("https://google.com/", "_self");
}

Note the new // @match https://A_VERY_SPECIFIC_PATH/* which makes the code to work.

What may cause this

I assume that this situation is caused by a redirect.

  • In one website in which it happened I have found a redirect in the devtool “Network” section.
  • In another website in which it happened I didn’t find such a redirect there so there might have been a “frontend redirect” of some sort.

Notes

  • The alleged redirected webpages keep look the same, as if no redirect really ever happened
  • This problem doesn’t happen in 99% of the websites. It’s only two websites so far which have some kind of “user-side-javascript” prevention mechanism that causes window.location.href.includes() tests to be ineffective as long as there isn’t a specific enough @match command.

My question

What my cause this situation and how to run the code without a specific @match command?

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