0

i have an extension which blocks ads and i want to have a youtuber whitelist which dosent block ads on specific youtube channels so i need to obtain the current watched video's channel name using js from within a content script how could i approuch this i tought about maybee geting the html element which holds the channel name but it dosent seem logical to access the DOM for data is there a object i can access from within the content script to get it? i also tought about getting the video id from the url and using a youtube api to somehow get the channel name but that seems sloppy

  • 1
    DOM is most likely your best option for this, since the video links don't include the channel. Another way would be to interact with the site's js, but you would have to inject your own js into the page and then pass back the info (most likely through the DOM too), so that is way messier than reading it from the DOM that you already have access to. I generally approach these kinds of problems by viewing the page source to find elements that are always present and have the info. One thing to keep in mind is that not everything loads when the html loads. – Gergely Szabo Mar 16 '20 at 16:04
  • but since im already using a content script thats being inject wouldnt it be easier to interact with the site's js isnt that how chrome extensions content scripts work they are injected to the site the handeling will be done within the content script anyway so i dont need to send it anywhere – Barak Gzlee Mar 16 '20 at 16:07
  • 1
    Interacting with site's JS is actually impossible from a content script because it runs in an isolated environment. You would have to [insert a DOM script](https://stackoverflow.com/a/9517879) with the code that can access JS variables and extract the channel name using something like window['ytd-player'].__dataHost.data.contents.twoColumnWatchNextResults.results.results.contents[1].videoSecondaryInfoRenderer.owner.videoOwnerRenderer.title.runs[0].text – wOxxOm Mar 16 '20 at 18:58

2 Answers2

4

Your best bet is indeed the DOM. I would use the selector #meta-contents #channel-name a

// channel name
document.querySelector("#meta-contents #channel-name a").text
// channel url
document.querySelector("#meta-contents #channel-name a").href
ariel
  • 15,620
  • 12
  • 61
  • 73
0

To get Youtube video title, use :

document.title.replace(/^\u25B6\s/, "").replace(/\s-\sYouTube$/, "")
Rak
  • 53
  • 1
  • 5