Shir Gans
Shir Gans

Reputation: 2027

Change the popup element when matching a URL

I have an "activate" button on my popup, which should be disabled by default, and should become clickable when viewing YouTube.com video page (i.e url matches "/watch/?v=")

How this should be done? With background.js or with popup.js? I'm confused.

Upvotes: 0

Views: 258

Answers (2)

Shir Gans
Shir Gans

Reputation: 2027

This is my final solution (in popup.js as suggested here)

chrome.tabs.query({
    active: true,
    lastFocusedWindow: true
}, function(tabs) {
    var tab = tabs[0];
    var tabURL = tab.url;
    if (tabURL.includes('youtube.com/watch?v')) {
        jQuery('#activation-button').removeClass('unavailable');
    }
});

Upvotes: 1

Jason Owens
Jason Owens

Reputation: 543

Shouldn't you match urls in the manifest, therefore it would only work on youtube pages? I don't see why you couldn't do this using Match Patterns. In any event, if you must choose between the background and popup, I would say popup.js because you would want it to check initially of clicking popup.html.

So you have popup.html, and then it has a button called "Activate". Just make another file called activate.js. In activate.js

function activate(){
    var test =  window.location.href
    if (test.includes('watch?v')){
    console.log('Yes')}
    }
activate()

Include activate.js in your popup.html.

Upvotes: 0

Related Questions