Skip to content

Instantly share code, notes, and snippets.

@redstoneleo
Created February 28, 2018 02:22
Show Gist options
  • Save redstoneleo/a21880e31efbbb2ef956186794ecaa0f to your computer and use it in GitHub Desktop.
Save redstoneleo/a21880e31efbbb2ef956186794ecaa0f to your computer and use it in GitHub Desktop.
MediaDownload
// console.log('tabTitle----onClicked----',tab.title,hostname);
fileExt = tabId2DownloadInfo[tab.id][1]; //{tabId:[targetUrl,fileExt]} ------->fileExt
np = nameParser[hostname];
fileName = (np ? np(tabTitle) : tabTitle) + fileExt; //必须在三元运算符外加括号才会成为一个值,不然直接return
// console.log('downloadInfo fileName ' ,fileName.replace(/[\\/:*?"<>|]/g, '_'));
tabId2DownloadInfo[tabId][1] = fileName.replace(/[\\/:*?"<>|~]/g, '_'); //{tabId:[targetUrl,fileName]} 替换网页标题里不能包含于文件名(以Windows的文件命名为准)中的非法字符\/:*?"<>|
function coursera(tabTitle) {
return tabTitle.split(' - ')[0];
}
function xiami(tabTitle) {
return tabTitle.match(/:(.+)-/)[1]; ////:一定会有,(.+)婪模式匹配(效率),-一定会有
}
function kuwo(tabTitle) {
return tabTitle.split('-')[0];
// return tabTitle.match(/【?(.+?)】?_/)[1]; //【可有可无,(.+?)非贪婪模式匹配,】可有可无 _一定会有
}
function music163(tabTitle) {
return tabTitle.split('▶ ')[1];
}
function _1ting(tabTitle) {
return tabTitle.split('_')[0];
}
function QQkg(tabTitle) {
return tabTitle.split('-')[0];
}
// function baidu(tabTitle) {
// return tabTitle.split('-')[0];
// }
var nameParser = {//QQ有音乐、fm、kg,所以key只能这么写了
'www.coursera.org': coursera,
'www.xiami.com': xiami,
'www.kuwo.cn': kuwo,
'music.163.com': music163,
'www.1ting.com': _1ting, //'baidu':baidu
'kg.qq.com':QQkg
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment