Tampermonkey / tampermonkey

Tampermonkey is the most popular userscript manager, with over 10 million users. It's available for Chrome, Microsoft Edge, Safari, Opera Next, and Firefox.
GNU General Public License v3.0
4.17k stars 416 forks source link

Script runs only on refresh / first time a page loads #1997

Closed yashaskm11 closed 6 months ago

yashaskm11 commented 6 months ago

I tried writing a simple script to copy content of a web page on click of a button. It does work when first page loads, but on clicking / navigating to other pages (that match the @match <url> ), the script doesn't run by default. It works only after refresh.

Expected Behavior

Script runs on every link that navigated in the website, not only on the first page (or) when the page is refreshed.

Actual Behavior

Scripts runs only when the page is loaded (or) refreshed.

Specifications

Script

// ==UserScript==
// @name         Jio Saavn Lyrics
// @namespace    http://tampermonkey.net/
// @version      2024-03-01
// @description  Copies lyrics from jio saavn
// @author       funny
// @match        https://www.jiosaavn.com/lyrics/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=jiosaavn.com
// ==/UserScript==

alert("hello")

Ex. url : https://www.jiosaavn.com/lyrics/something-about-us-lyrics/FFwBQz19XAU

jb222 commented 6 months ago

Your alert is only working on page load. While navigating on the website, I noticed the left Browse column does not reload, so I guess the content of the main column is changed by Javascript (i.e. XMLHttpRequest), while simultanously rewriting the link in the address bar (but not actually reloading the whole page). Maybe you can detect a rewrite of the address bar and when the word "lyrics" is present, do the alert.

7nik commented 6 months ago

See #1897. TM/GM/VM and any other userscript manager doesn't and won't support script injection on soft navigation.

jb222 commented 6 months ago

Sure, @match only works on refresh. But there must be a way of detecting a content change in the page, true?

7nik commented 6 months ago

window.onurlchange https://github.com/Tampermonkey/tampermonkey/issues/1897#issuecomment-1813387491 also, kinda related https://github.com/Tampermonkey/tampermonkey/issues/1305#issuecomment-903281912