0

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.

Trevor Reid
  • 3,310
  • 4
  • 27
  • 46
Shir Gans
  • 1,976
  • 3
  • 23
  • 40

2 Answers2

1

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');
    }
});
Shir Gans
  • 1,976
  • 3
  • 23
  • 40
  • 1
    I could find this useful. I was running it from the console, just testing but in any event, I'm glad it worked out. – Jason Owens Nov 05 '19 at 16:21
0

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.

Jason Owens
  • 525
  • 2
  • 6
  • 21
  • I want the pop-up to be available on all pages, just part of it, the button to be available on the video pages – Shir Gans Nov 04 '19 at 18:21
  • Thank, helped me to find the final solution, when I tried that. window.location.href has return the URL of the extension and not the tab URL. I used @wOxxOm suggestion to find the tab URL. – Shir Gans Nov 05 '19 at 05:43