我刚开始使用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
谢谢!