Javascript 检测当前选项卡是否为Chrome扩展中的Youtube时出现问题
我刚开始使用Chrome扩展。我正在尝试构建一个简单的扩展,它允许我快速下载我当前正在播放的某个Youtube视频的mp3。 第一步是检测当前网站是否为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
// 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语句放在匿名函数中。可能是