-4

Question : How to reference id of a link? See the attached image Click here

function insertButton(refresh = false) {
var to_match = 'a[class="';
var PAGE_TYPE = 0;

if (document.location.href == "https://www.youtube.com") {
    

How to reference id = "video-title-link"

var anchor_tag = document.getElementById("video-title-link");

There are 2 anchor tags* with the same class name. I would like to reference the id ="video-title-link"

  if(anchor_tag.hasAttribute('href="\/watch\?v=(\S+)"')){

    to_match = to_match.concat("yt-simple-endpoint style-scope ytd-rich-grid-video-renderer", '"]').getattribute('id ="video-title-link"');
    PAGE_TYPE = 1;

  } 

}

UPDATE : POLLING

const poller = setInterval(function(){  
    
  const anchor_tags = document.querySelectorAll('a[id="video-title-link"]'); 
  
    if(anchor_tags){
  
        for (index = 0; index < anchor_tags.length ; index++){
            
            console.log(anchor_tags[index].getAttribute('href'));
            
        }   
        
        clearInterval(poller);
        
    }   
    
}, 1000);   

OUTPUT : Click here : href of each 'id'

prv.2k20
  • 1
  • 2

1 Answers1

2

You can use querySelector() and .getAttribute() to do this:

const element = document.querySelector(".yt-simple-endpoint")
const href = element.getAttribute("href");
console.log(href)
<a class="yt-simple-endpoint" href="https://example.com">Link</a>
Luke Storry
  • 6,032
  • 1
  • 9
  • 22