Greasy Fork is available in English.

讨论 » 创建请求

[youtube.com] create anchor for channelid next to title or wherever. channel's UID

§
发表于:2023-05-15

hello

can someone create a script that creates an <a> next to

<div id="container" class="style-scope ytd-channel-name">
<div id="text-container" class="style-scope ytd-channel-name">
    <yt-formatted-string id="text" link-inherit-color="" title="" class="style-scope ytd-channel-name">MewZe</yt-formatted-string>
</div>
<tp-yt-paper-tooltip fit-to-visible-bounds="" class="style-scope ytd-channel-name" role="tooltip" tabindex="-1" style="inset: -15.6167px auto auto 209.8px;"><!--css-build:shady--><div id="tooltip" class="style-scope tp-yt-paper-tooltip hidden" style-target="tooltip">

    MewZe

</div>
</tp-yt-paper-tooltip>
</div>

taking the value from

<link rel="canonical" href="https://www.youtube.com/channel/UC0-p-Hmc92YCtI8s3PEHC3A">
§
发表于:2023-05-15

took too long to edit:

so basically

<a href="https://www.youtube.com/channel/UC0-p-Hmc92YCtI8s3PEHC3A">greasescript</a>

or

<div id="container" class="style-scope ytd-channel-name">
<div id="text-container" class="style-scope ytd-channel-name">
    <yt-formatted-string id="text" link-inherit-color="" title="" class="style-scope ytd-channel-name">MewZe</yt-formatted-string>
<a href="https://www.youtube.com/channel/UC0-p-Hmc92YCtI8s3PEHC3A">greasescript</a>
</div>
§
发表于:2023-05-15
document.querySelector('#container.ytd-channel-name').insertAdjacentHTML('afterend',
`<a href="https://www.youtube.com/channel/${document.querySelector('link[rel="canonical"][href]').href.split('/').pop()}">greasescript</a>`);

But your way of getting the channel-id is complete bullshit. Use better way to get it

document.body.querySelector('ytd-watch-flexy')?.playerData?.videoDetails.channelId
§
发表于:2023-05-15

oh wow, thank you very much

But your way of getting the channel-id is complete bullshit. Use better way to get it

heh, I'm sorry. I just wanted to show what I meant. I don't even know what type of call that is. DOM, JS, etc. ?

I think you had too much faith in me. I pasted the code in tamper and grease and nothing shows up. Tinkered a bit with the selector but nothing.

§
发表于:2023-05-15
编辑于:2023-05-15

Yes, I thought that if you inserted the code, then it was you who wrote it, heh

Read about ways to add html

// universal

https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML

// around

https://developer.mozilla.org/en-US/docs/Web/API/Element/after

https://developer.mozilla.org/en-US/docs/Web/API/Element/before

// inside

https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

https://developer.mozilla.org/en-US/docs/Web/API/Element/append

There is such a thing as. It specifies when to run the code. Before page load or after.

// @run-at document-start

OR

// @run-at document-end

obviously what you need after. Since you need to wait for the ytd-watch-flexy element to appear p.s. it is possible and up to but more complex designs are needed. If you want to dive, then here

In general, the code can be directly tested (without monkeys) if you paste it into the browser console Try pasting this code into the console. Should add a link.

document.querySelector('#container.ytd-channel-name')
  .insertAdjacentHTML('afterend',
`<a href="https://www.youtube.com/channel/${document.querySelector('link[rel="canonical"][href]').href.split('/').pop()}">greasescript</a>`);

The complete code looks like this

// ==UserScript==
// @name        111
// @namespace   Violentmonkey Scripts
// @match       https://www.youtube.com/watch?v=*
// @version     1.0
// @run-at document-end
// ==/UserScript==

 waitForElement('#upload-info #channel-name a[href]')
   .then(element => {
      element.insertAdjacentHTML('afterend',
      `<a href="https://www.youtube.com/channel/${document.querySelector('link[rel="canonical"][href]').href.split('/').pop()}">greasescript2</a>`
    )
   });


function waitForElement(selector = required()) {
   if (typeof selector !== 'string') return console.error('wait > selector:', typeof selector);

   return new Promise((resolve, reject) => {
      // try {
      let nodeInterval
      const checkIfExists = () => {
         if (el = document.querySelector(selector)) {
            if (typeof nodeInterval === 'number') clearInterval(nodeInterval);
            resolve(el);

         } else return;
      }
      checkIfExists();
      nodeInterval = setInterval(checkIfExists, 150); // ms
      // } catch (err) { // does not output the reason/line to the stack
      //    reject(new Error('Error waitElement', err));
      // }
   });
}

The problem is that youtube is loaded in parts. And not entirely as a static page. Therefore, you need to find the time or trigger to understand when the desired element appeared and only then run our code

§
发表于:2023-05-16

omfg I tried to modify the element selectors but no idea what I'm doing wrong.
btw my example was for the profile page. the canonical is different on video pages.

but, if you can make it work with the document.body.querySelector('ytd-watch-flexy')?.playerData?.videoDetails.channelId
on video pages, I think that would be even better.

§
发表于:2023-05-16
编辑于:2023-05-16
// ==UserScript==
// @name        lerma-yt
// @namespace   https://greasyfork.org/en/discussions/requests/183547-youtube-com-create-anchor-for-channelid-next-to-title-or-wherever-channel-s-uid
// @match       https://www.youtube.com/*
// @version     1.0
// @run-at document-end
// ==/UserScript==

 waitForElement('#channel-header ytd-channel-name, #upload-info #channel-name a[href]')
   .then(element => {
      element.insertAdjacentHTML('afterend',
      `<a href="https://www.youtube.com/channel/${document.querySelector('document.querySelector('link[itemprop="url"][href]')?.href.split('/')[4] || document.body.querySelector('ytd-watch-flexy')?.playerData?.videoDetails.channelId}">channel link</a>`
    )
   });


function waitForElement(selector = required()) {
   if (typeof selector !== 'string') return console.error('wait > selector:', typeof selector);

   return new Promise((resolve, reject) => {
      // try {
      let nodeInterval
      const checkIfExists = () => {
         if (el = document.querySelector(selector)) {
            if (typeof nodeInterval === 'number') clearInterval(nodeInterval);
            resolve(el);

         } else return;
      }
      checkIfExists();
      nodeInterval = setInterval(checkIfExists, 150); // ms
      // } catch (err) { // does not output the reason/line to the stack
      //    reject(new Error('Error waitElement', err));
      // }
   });
}
§
发表于:2023-05-17

:S I'm getting Syntax error? with violentmonkey. tamper gives error on line 13

<3

§
发表于:2023-05-17

remove ('document.querySelector

§
发表于:2023-05-17
编辑于:2023-05-17

perfection. amazing.
very happy.

thank you very much <3

super blind. should've figured out this myself. the line 13. AD&D etc

§
发表于:2023-06-06
编辑于:2023-06-06

seems like youtube is or already was doing this. I just never bothered to look over these links whenever I opened the description box. thanks asmongold for being too lazy to have an advertisement description box.

https://i.postimg.cc/wMDtYL54/image.png

but i prefer the script either way. thank you

§
发表于:2023-06-06

if you need to open a "video" tab instead of "featured"

that is, implementation is much simpler - redirection

There are alternative scripts above

§
发表于:2023-06-06

no I wanted the channel's UID page in case the person gets hacked or renames or whatever.

holy cow, I don't think I've heard of Nova Youtube. I'll give it a try.

发表回复

登录以发表回复。