Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检测当前选项卡是否为Chrome扩展中的Youtube时出现问题_Javascript_Google Chrome_Google Chrome Extension_Youtube - Fatal编程技术网

Javascript 检测当前选项卡是否为Chrome扩展中的Youtube时出现问题

Javascript 检测当前选项卡是否为Chrome扩展中的Youtube时出现问题,javascript,google-chrome,google-chrome-extension,youtube,Javascript,Google Chrome,Google Chrome Extension,Youtube,我刚开始使用Chrome扩展。我正在尝试构建一个简单的扩展,它允许我快速下载我当前正在播放的某个Youtube视频的mp3。 第一步是检测当前网站是否为Youtube,我遇到了问题。调试器什么也没说,所以我真的不知道是什么问题。 代码如下: // COPYRIGHT Gofilord May 2014 // Download mp3 songs from Youtube easily and quickly // javascript file var url = ''; // store

我刚开始使用Chrome扩展。我正在尝试构建一个简单的扩展,它允许我快速下载我当前正在播放的某个Youtube视频的mp3。 第一步是检测当前网站是否为Youtube,我遇到了问题。调试器什么也没说,所以我真的不知道是什么问题。 代码如下:

// 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

谢谢

是内容还是背景脚本?内容。然而,我明白了!我应该把if语句放在匿名函数中。可能是