Discussions » Demandes de création de scripts

[Youtube] parse channel name into <title> tag

§
Posté le: 2021-05-05

hello

Up until today I ran the following script but it seems Youtube must've changed something bc the script goes into an endless loop now.
Could someone revise the code and help me out?






// ==UserScript==
// @name YouTube Channel in Title
// @include https://*youtube.com/*
// @include https://www.youtube.com/c/*/live
// @include https://www.youtube.com/c/*/live*
// @exclude https://*youtube.com/channel/*
// @exclude https://www.youtube.com/user/*
// @exclude https://www.youtube.com/results?*
// @exclude https://www.youtube.com/playlist*
// @exclude https://www.youtube.com/feed/*
// @exclude https://www.youtube.com/post
// @exclude https://www.youtube.com/c/*
// ==/UserScript==
 
(function() {
 'use strict';
 
 var loadScript = function (content) {
 var tag = document.createElement('script');
 tag.type = 'text/javascript';
 tag.innerHTML = content;
 document.head.appendChild(tag);
 };
 
 loadScript(`
 var lastName = '';
 document.body.addEventListener('transitionend', function(event) {
 if (document.title != lastName) {
 const videoTitle = document.title.replace(' - YouTube', '');
 const owner = document.querySelector('ytd-channel-name').innerText;
 const date2 = document.querySelector('#date > yt-formatted-string.ytd-video-primary-info-renderer').innerText;
 lastName = "- " + videoTitle + " -- " + owner + "- -- " + date2;
 document.title = lastName;
 }
 });
 `);
})();
woxxomMod
§
Posté le: 2021-05-05

Here's a different approach:

// ==UserScript==
// @name  YouTube Channel in Title
// @match https://*.youtube.com/*
// @grant none
// ==/UserScript==

'use strict';

let title = '';
const mo = new MutationObserver(() => {
  mo.disconnect();
  pinTitle();
});
pinTitle();

function pinTitle() {
  title = document.title.replace(' - YouTube', '');
  if (location.pathname.startsWith('/watch')) {
    const owner = document.querySelector('ytd-channel-name').innerText;
    const date2 = document.querySelector('#date > yt-formatted-string.ytd-video-primary-info-renderer').innerText;
    title = `- ${title} -- ${owner}- -- ${date2}`;
  }
  document.title = title;
  mo.observe(document.querySelector('title'), {
    characterData: true,
    childList: true,
  });
}
§
Posté le: 2021-05-05

hi. thank you
unfortunately nothing is happening.
I'm on firefox Version 88.0.1, Build ID 20210504152106

woxxomMod
§
Posté le: 2021-05-06

It works for me in Firefox + Greasemonkey/Violentmonkey/Tampermonkey.

§
Posté le: 2021-05-06

Just created a fresh profile. Out of 50 opened tabs it maybe worked three times :S
unfortunately it didn't work once during the video

here's a video https://vimeo.com/545810573
password is: aaaaaaaaaaaa

woxxomMod
§
Posté le: 2021-05-06

I was testing in one tab... If no one else posts their solution, I'll make another try later.

§
Posté le: 2021-05-06

alright thanks man

§
Posté le: 2021-05-06

Why have you posted a video from Vimeo?
This script is only supposed to work on YT, maybe that's why you're saying it doesn't work

§
Posté le: 2021-05-06

this was just to show how it works.

woxxomMod
§
Posté le: 2021-05-06
Édité le: 2021-05-06

This seems to work correctly:

// ==UserScript==
// @name  YouTube Channel in Title
// @match https://*.youtube.com/*
// @grant none
// ==/UserScript==

'use strict';

const mo = new MutationObserver(pinTitle);
const $ = (sel, node = document) => node.querySelector(sel);

pinTitle();
document.addEventListener('yt-navigate-finish', pinTitle);

function pinTitle() {
  let title = document.title.replace(' - YouTube', '');
  if (location.pathname.startsWith('/watch')) {
    const name = $('.title')?.innerText || title;
    const owner = $('ytd-channel-name')?.innerText;
    const date2 = $('#date > yt-formatted-string.ytd-video-primary-info-renderer')?.innerText;
    title = `- ${name || ''} -- ${owner || ''}- -- ${date2 || ''}`;
  }
  if (document.title !== title) {
    mo.disconnect();
    document.title = title;
    mo.observe($('title'), {characterData: true, childList: true});
  }
}
§
Posté le: 2021-05-06

legend. thank you

is there any way to remove that space before the hyphen? that's what I need to search channels

§
Posté le: 2021-05-06
Édité le: 2021-05-06

nvm. solved it. thank you very much

const owner = $('a.yt-simple-endpoint.style-scope.yt-formatted-string')?.innerText;

also it seems like your script doesn't make the title vanish. because that's the problem I had with the other one.
after about ten minutes the title would refresh and I had to trigger the script by either pressing the bell, right-clicking the video or hovering around the video timeline. (that's why the weird mouse movements in the video above.)

where can I send dono?

§
Posté le: 2021-06-17

hey. just wanted to drop by and give you another thanks. the script works wonderfully.
now not even the title changes when the bell has notifications.
It used to prefix the title with the amount of notifications pending in the inbox like: (6) -title-

thanks man

Poster une réponse

Connectez-vous pour poster une réponse.