0

I'm just starting out with Chrome Extensions. I'm trying to build a simple extension that allows me to quickly download the mp3 of a certain Youtube video that I am currently on. The first step is detecting whether the current website is Youtube or not, and I have problems. Debugger said nothing so I really don't know what's the problem. Here's the code:

// COPYRIGHT Gofilord May 2014
// Download mp3 songs from Youtube easily and quickly
// javascript file


var url = ''; // store the url of the current youtube song
var btn_group = $('.download-btn-group'); // caching
var errors = $('.errors');


// both are dynamic elements
// they show up accordingly 
btn_group.hide();
errors.hide();

// get current tab's URL 
// for sending that URL to the API of the mp3 converter
chrome.tabs.getSelected(null, function(tab) {
    url = tab.url;
});

// function to check if the current website is indeed Youtube
// add-on works ONLY on youtube.
function isYoutube() {
  if (url.toLowerCase().indexOf('www.youtube.com') >= 0) // if Youtube's address is    existing in the url
    return true; // that means the user is in youtube.
  return false;
}


if (isYoutube()) // if it's youtube
  btn_group.show(); // show the download buttons
else 
  errors.show(); // show a msg saying the website isn't youtube

Thanks!

Rob W
  • 341,306
  • 83
  • 791
  • 678
Gofilord
  • 6,239
  • 4
  • 27
  • 43
  • is it content or background script? – desu May 16 '14 at 19:02
  • Content. However, I figured it out! I should have put the if statements inside the anonymous function. – Gofilord May 16 '14 at 20:00
  • possible duplicate of [Why is my variable undefined after I modify it inside of a function? (canonical asynchronicity topic)](http://stackoverflow.com/questions/23667086/why-is-my-variable-undefined-after-i-modify-it-inside-of-a-function-canonical) – rsanchez May 17 '14 at 16:51

0 Answers0