How to run JavaScript in a redirected page?

In example.com/page/1, I run a user script JavaScript via a user script manager like Tampermonkey but for some reason this webpage is always redirected to example.com/page/2.

How could I re-run the JavaScript right after the redirection was completed?

  • Using a global // @match *://*/* doesn’t help for some reason, i.e. it just won’t cover the redirect.

I think LocalStorage is a topic I should learn to know how to do that. Am I wrong?

How do you know? What have you tried?

Also, you could (should?) see the loading of page 2 as its own event, not as a result of redirecting away from page 1. That’s pretty much irrelevant.

I tried this:

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

////////////////////////////////////////////////////////////////////////////
// Block news websites as much as possible
////////////////////////////////////////////////////////////////////////////

if (
  /* document.querySelector('title').textContent.includes('Breaking News') || */
  document.querySelector('meta[name="title"]')?.content.includes('News') ||
  document.querySelector('meta[name="title"]')?.content.includes('news') ||
  document.querySelector('meta[name="description"]')?.content.includes('News') ||
  document.querySelector('meta[name="description"]')?.content.includes('news') ||
  document.querySelector('meta[name="tags"]')?.content.includes('News') ||
  document.querySelector('meta[name="tags"]')?.content.includes('news') ||
  document.querySelector('title')?.textContent.includes('חדשות') ||
  document.querySelector('title')?.textContent.includes('החדשות') ||
  document.querySelector('meta[name="title"]')?.content.includes('חדשות') ||
  document.querySelector('meta[name="title"]')?.content.includes('החדשות') ||
  document.querySelector('meta[name="description"]')?.content.includes('חדשות') ||
  document.querySelector('meta[name="description"]')?.content.includes('החדשות') ||
  document.querySelector('meta[name="tags"]')?.content.includes('חדשות') ||
  document.querySelector('meta[name="tags"]')?.content.includes('החדשות')
) {
  window.open("https://google.com/", "_self");
}

////////////////////////////////////////////////////////////////////////////
// Block videos autoplay
////////////////////////////////////////////////////////////////////////////

window.setInterval( () => {
  document.querySelectorAll('video').forEach(video => {
    video.pause();
  });
}, 1);

////////////////////////////////////////////////////////////////////////////
// Block specific website/s
////////////////////////////////////////////////////////////////////////////

window.setInterval( () => {
    const urlPatternToBlock = [
        'match'
    ];

    for (const element of urlPatternToBlock) {
        if (window.location.href.includes(urlPatternToBlock)) {
            window.open("https://google.com/", "_self");
        }
    }
}, 1);

In the third part of the code, were 'match' is available, I have tried to block two websites and didn’t succeed, unless a specific // @match *://DOMAIN_TLD_1/*/* was used alongside the more general // @match *://*/*.

But, for some reason, If I run that third part of the code in a separate, standalone script, it does work only with the general // @match *://*/*, so I was probably wrong about redirects, it’s not redirects, but running all three code blocks together causing some clashing maybe.

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