Greasy Fork is available in English.

Discussions » Développement

How to only apply userscript to YouTube channel homepage https://www.youtube.com/@xxx ?

§
Posté le: 2024-03-05
Édité le: 2024-03-05

Hi, How to ONLY apply a userscript on YouTube channel homepage https://www.youtube.com/@xxx (for example: https://www.youtube.com/@LCI) and not for other YouTube page ? I have tried

// @match       https://www.youtube.com/@*

and

// @match       https://www.youtube.com/\@*

but in both case the userscript is running for all YouTube webpages. Regards

§
Posté le: 2024-03-06
Édité le: 2024-03-06

I have also tested with this:

// @include /^https?:\/\/www\.youtube\.com\/@[^\/]+$

but it still does not work 😓 though the regular express is ok, I have checked it with https://regex101.com/

§
Posté le: 2024-03-06

I have ended with this:

// @include     https://www.youtube.com/@*
// @include     https://www.youtube.com/@*/featured
// @exclude     https://www.youtube.com/@*/videos
// @exclude     https://www.youtube.com/@*/shorts
// @exclude     https://www.youtube.com/@*/streams
// @exclude     https://www.youtube.com/@*/playlists
// @exclude     https://www.youtube.com/@*/community

but I don't like it really much it as I want to exclude it from All url like https://www.youtube.com/@/ except https://www.youtube.com/@*/featured and this does not work:

// @include     https://www.youtube.com/@*
// @include     https://www.youtube.com/@*/featured
// @exclude     https://www.youtube.com/@*/*[?featured]
Deleted user 821489
§
Posté le: 2024-03-06

try adding a check

if (location.pathname.split('/') > 2) return;

§
Posté le: 2024-03-06

If you can't achieve what you want using // @match then just match everything and move filter logic to js as raingart have mentioned

§
Posté le: 2024-03-06

Thank you, I have done it.
I do like when the userscript manager does not state there is a userscript running when not need though (so when something bad happen it is easier to spot the cause)

§
Posté le: 2024-03-07

I do like when the userscript manager does not state there is a userscript running when not need

Relatable, but @include is subject to be removed, and the @match possibilities are poor, so you don't really have a choice

§
Posté le: 2024-03-07
Édité le: 2024-03-07

@Konf Thanks for your message, I find it strange that a powerful system (@include) is being replaced with a less powerful system (@match).
Having a match that is only targeting the proper webpage you need is very useful to ease debug when you have several userscript. When this match is done in the code, it can become difficult to be certain of the cause. I don't understand the coding philosophy behind this removal of @include with a less functional @match.

§
Posté le: 2024-03-07
Édité le: 2024-03-07

I find it strange that a powerful system (@include) is being replaced with a less powerful system (@match)

I don't remember exactly, but I've ran into some problem using @include and then I've come to conclusion that the best way to filter out a pages where you want your script to be running is a javascript, by working with the window.location etc. Though @include directive is good for some simple scripts, there is a chance that it will not be working when the Manifest V3 will arrive, or will be reworked somehow. So the most stable and reliable way for now is @match + js.

§
Posté le: 2024-03-07
Édité le: 2024-03-07

Good to know, I will use @match from now. Which software tool do you use for viewing/editing your userscript that can give this useful info?

§
Posté le: 2024-03-07
Édité le: 2024-03-07

Please also be aware that, desktop www.youtube.com is Single Page Application.

You should just use https://www.youtube.com/*

Otherwise your script would not work in the way that the user enter the homepage or video page first that jump to the channel page.

You can use

document.addEventListener('yt-navigate-finish', function(){
  console.log(location.href);
  const match = /^https:\/\/www\.youtube\.com\/@([-_a-zA-Z0-9.]{3,30})$/.exec(location.href);
  if(match){
     const handleName = match[1];
     console.log(`This is a channel page of ${handleName}`);
  }
});

to detect the page change.

§
Posté le: 2024-03-07

Which software tool do you use for viewing/editing your userscript that can give this useful info?

It is a default Tampermonkey editor. Maybe I have some non-default settings, but idk

§
Posté le: 2024-03-07

I am using Violentmonkey on Chrome. It seems like Tampermonkey is better as Violentmonkey does not show this.

§
Posté le: 2024-03-07
Édité le: 2024-03-07

I am using Violentmonkey on Chrome. It seems like Tampermonkey is better as Violentmonkey does not show this.

Tampermonkey is closed-source. Violentmonkey is open source. Tampermonkey is too bulky.

Violentmonkey's author uses the local editor mostly. So the built-in editor is just a simple editor.

See https://violentmonkey.github.io/posts/how-to-edit-scripts-with-your-favorite-editor/

§
Posté le: 2024-03-08

Tampermonkey is better

That is true

§
Posté le: 2024-03-08

Violentmonkey's author uses the local editor mostly. So the built-in editor is just a simple editor.

See https://violentmonkey.github.io/posts/how-to-edit-scripts-with-your-favorite-editor/

It is quite hard to implement as needs to implement a local server. I think I will host my userscript in GitHub and edit them from GitHub website instead.

Poster une réponse

Connectez-vous pour poster une réponse.