Dyskusje » Tworzenie skryptów

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

§
Napisano: 05-03-2024
Edytowano: 05-03-2024

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

§
Napisano: 06-03-2024
Edytowano: 06-03-2024

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/

§
Napisano: 06-03-2024

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
§
Napisano: 06-03-2024

try adding a check

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

§
Napisano: 06-03-2024

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

§
Napisano: 06-03-2024

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)

§
Napisano: 07-03-2024

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

§
Napisano: 07-03-2024
Edytowano: 07-03-2024

@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.

§
Napisano: 07-03-2024
Edytowano: 07-03-2024

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.

§
Napisano: 07-03-2024
Edytowano: 07-03-2024

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?

§
Napisano: 07-03-2024
Edytowano: 07-03-2024

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.

§
Napisano: 07-03-2024

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

§
Napisano: 07-03-2024

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

§
Napisano: 07-03-2024
Edytowano: 07-03-2024

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/

§
Napisano: 08-03-2024

Tampermonkey is better

That is true

§
Napisano: 08-03-2024

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.

Odpowiedz

Zaloguj się, by odpowiedzieć.