Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Hash sign as a path component in a user script's @match command prevents the script from running

Post

Hash sign as a path component in a user script's @match command prevents the script from running

+3
−0

I try to exactly match a login page in a website, for a user script manager (USM) script, which I run with Tampermonkey.

This pattern didn't work:

// @match        https://example.com/#/login

This pattern worked:

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

Why would the Hash sign (#) prevent the script from running?

By "prevent the script from running" I mean that Tampermonkey (version 4.13) just won't start the script on that webpage if the first pattern is used.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

In an URL, the `#` is called fragment identifier, followed by the actual fragment. (4 comments)
In an URL, the `#` is called fragment identifier, followed by the actual fragment.
elgonzo‭ wrote over 2 years ago · edited over 2 years ago

In an URL, the # is called fragment identifier, followed by the actual fragment. It is important to understand that fragments are NOT part of the path in an URL. For example, all the following url's have the same empty path: https://example.com/, https://example.com/#, https://example.com/#whatever, https://example.com/#/login. But each of those URLs have different fragments.

I have no experience with browser extension development or user scripts, though, and the documentation about match patterns is a bit ambiguous in that regard, but i would suspect this being related. The pattern https://example.com/* matches successfully, because it can match against any given path, including an empty path.

However, the pattern https://example.com/#/login would not match against an empty path. I suspect (but i am not sure; again, i have no experience with content script match patterns), that that pattern would match against URLs like https://example.com/%23/login.

elgonzo‭ wrote over 2 years ago · edited over 2 years ago

Important side note: The URL https://example.com/%23/login is quite different from the URL https://example.com/#/login. I am talking about actual URLs here, i am not talking about URL patterns. Do not make the mistake of treating them as equivalent forms of the same URLs. They are not the same URL.

https://example.com/%23/login is a URL with the path (URL-percent-encoding decoded) "#/login" and no fragment.

On the other hand, https://example.com/#/login is a URL with an empty path and the fragment "/login".

elgonzo‭ wrote over 2 years ago · edited over 2 years ago

The question then boils down to how could you possibly match against a given fragment in a URL. Frankly and unfortunately, i don't know. Content script match patterns basically can match schemes, hosts, and paths (https://developer.chrome.com/docs/extensions/mv3/match_patterns/). Note that it does not include fragments.

Whether you can reach your goal while still employing content script match patterns by choosing a different angle or different approach that would not need to rely on matching against URL fragments -- again, i don't know...

elgonzo‭ wrote over 2 years ago · edited over 2 years ago

Whether TamperMonkey offers alternative mechanisms for fragment matching, i don't know. But even if it would have such a mechanism, I would have a hard time understanding how this could possibly work satisfactorily. Navigation based on URL fragment where the scheme/host/path of the URLs stays the same would not result in page unloading and loading. It would just result in the page repositioning in the view port. How this could possibly work with page-based user scripts (whose life-time and scope are normally both bounded by the page loading and unloading), i have frankly not the faintest clue...