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.2k stars 418 forks source link

Inject on pushState if URL wasn't matched before #2187

Open kkmuffme opened 18 hours ago

kkmuffme commented 18 hours ago

when using @match and the site redirects using e.g. history.pushState tampermonkey will not inject the script - which normally is fine and is wontfix as discussed in https://github.com/Tampermonkey/tampermonkey/issues/200 https://github.com/Tampermonkey/tampermonkey/issues/1803

However, if the previous URL didn't have the script injected at all, tampermonkey will never inject the script and it's obviously also not possible to do from the script itself (since it's not loaded)

e.g.

// @match https://mywebsite.com/internal/*

Browser URL is https://mywebsite.com/external/ and I click on a button, which redirects me to https://mywebsite.com/internal/foo with history.pushState()

This means my tampermonkey script never gets injected.

I could obviously change my @match to

// @match https://mywebsite.com/*

however this means it will load my script on tons of pages I don't need it a) making it less secure, since suddenly my script runs on lots of pages it doesn't need to b) making it impossible to use if there are subdirectories my script must not run (e.g. https://mywebsite.com/secure/ runs validations/other browser extensions that will make the page fail if it encounters any unexpected modifications to the DOM) c) making the browser slower/consume more RAM

Expected Behavior

Inject the script on pushState,... if the script hasn't been injected yet because the previous URL didn't "@match"

Actual Behavior

Script will never run if URL is changed to an URL with history.pushState that @match if the previous URL didn't @match

Specifications

7nik commented 17 hours ago

Here is my comment on the adjacent feature requests, and I believe most of the arguments also apply to your one.

kkmuffme commented 17 hours ago

Where?

7nik commented 17 hours ago

https://github.com/Tampermonkey/tampermonkey/issues/1305#issuecomment-903281912 - sorry, seems I forgot to insert the link