Shayd3 Shayd3 - 1 year ago 63
Javascript Question

Using YouTube API to like a video after action done on a different website

I'm trying to code my first google chrome extension and I'm kind of stuck on something. So, I'm trying to make an extension that when a user 'Upvotes' a link on, and if it's a link, I want to grab that link and 'like' the video. So it's basically turning 4-5 clicks into just one.

My main question is how do I go about listening for that 'Upvote' button to be hit? Do I need to use Reddit's api to listen for this? Or is there some kind of method/event listener that can tell me if the user clicked the upvote button?

I did notice when inspecting the element on the upvote button, when it hasn't been clicked it, one of the classes on the arrows is 'Up' and when you click it, it changes to 'Upmod'

I just need to figure this out and I feel like I can do the rest with ease. I'm teaching myself javascript right now, and I find the best way to do so is to just have a project and learn as you make it.

Any help would be appreciated! Thank you

Answer Source

Since the upvote is triggered solely via clicking, simply attach a click event listener in a content script that is declared to run on reddit video urls (*, for example):

document.addEventListener('click', function(e) {
    if ('.arrow.upmod')) {
        // like it on youtube

To upvote on youtube, you'll need to do it in a background/event page that can connect to any urls you declared in manifest.json's permissions.

So send a message with the video url to your event page. In the event page's message listener you'll send a XMLHttpRequest as per youtube API, for example. This will require adding the API url in the abovementioned permissions.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download