Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kkmuffme opened this issue Oct 2, 2024 · 4 comments
Open

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

kkmuffme opened this issue Oct 2, 2024 · 4 comments

Comments

@kkmuffme
Copy link

kkmuffme commented Oct 2, 2024

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 #200 #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

  • Chromium: 129
  • TM: 5.1.1
@7nik
Copy link

7nik commented Oct 2, 2024

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

@kkmuffme
Copy link
Author

kkmuffme commented Oct 2, 2024

Where?

@7nik
Copy link

7nik commented Oct 2, 2024

#1305 (comment) - sorry, seems I forgot to insert the link

@kkmuffme
Copy link
Author

kkmuffme commented Oct 3, 2024

I'm not familiar with the inner workings of Chrome extensions, however assuming the same JS APIs are available to it as to the DOM and that comment being 3 years old:
now there's window.navigation available in Chrome which has a 'navigate' event.

For scripts that are not loaded on the current page (bc no @match) tampermonkey could do:

window.navigation.addEventListener( 'navigate', ( navigateEvent ) => {
	if ( !navigateEvent.canIntercept || navigateEvent.downloadRequest ) {
		return;
	}

	// different page will be loaded, regular handling of @match can be used
	if ( !navigateEvent.destination.sameDocument ) {
		return;
	}
	
	const destinationUrl = new URL( navigateEvent.destination.url );
	// @todo see if new URL would @match and intercept if it does except if it has injected already?

	navigateEvent.intercept({
		handler: @todo,
	});
}, { passive: true });

While obviously, this can just be done in each tampermonkey script itself, this would lead to the issues a)b)c) outlined in the OP.

This approach obviously still has the issue of having to check whether tampermonkey already injected the script or not.

Feel free to close if you don't think changing this behavior is a priority right now for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants