Created
December 29, 2008 10:09
-
-
Save azuwis/41234 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CmdUtils.CreateCommand({ | |
| name: "dictcn", | |
| author: { | |
| name: "Hu Ziming", | |
| email: "[email protected]" | |
| }, | |
| contributors: ["Hu Ziming"], | |
| description: "Consulting the word from <a>http://dict.cn</a>.", | |
| icon: "http://dict.cn/favicon.ico", | |
| help: "Consulting the word from <a>http://dict.cn</a>.", | |
| takes: { | |
| "eng_word": noun_arb_text | |
| }, | |
| license: "MPL", | |
| preview: function(pblock, direct_obj) { | |
| var eng_word = direct_obj.text; | |
| var url = "http://dict.cn/ws.php?q="; | |
| url += eng_word; | |
| var pre_template = "<style> #container {background-color: #73bFE6;} #word {font-family: arial black; color: maroon;font-size: x-large;} #explanation {color: midnightblue; font-size: medium; font-family: tahoma;padding:3px;} #sent {color: maroon;} .orig {background-color: #B6C5F2; margin: 1px 0; color: firebrick;} .trans {background-color: #2D4488; color: white; margin: 1px 0;} </style> <div id=\"container\"> <div id=\"word\">${w.word} (${w.prons})</div> <div id=\"explanation\">${w.def}</div>{for foo in w.sents}<div id=\"sent\"> <div class=\"orig\">${foo.orig}</div> <div class=\"trans\">${foo.trans}</div> </div> </div>{/for}"; | |
| if ((eng_word.match("[a-zA-Z]+") && eng_word.length < 3) || eng_word.length == 0) { | |
| pblock.innerHTML = "Need at lest 3 english chars."; | |
| } else { | |
| jQuery.get(url, | |
| function(data) { | |
| var rslt = jQuery(data).find("dict"); | |
| var sents = rslt.find("sent").map(function() { | |
| return { | |
| orig: jQuery("orig", this).text(), | |
| trans: jQuery("trans", this).text() | |
| }; | |
| }); | |
| var foobar = { | |
| word: rslt.find("key").text(), | |
| prons: rslt.find("pron").text(), | |
| def: rslt.find("def").text(), | |
| sents: sents | |
| }; | |
| pblock.innerHTML = CmdUtils.renderTemplate(pre_template, { | |
| w: foobar | |
| }); | |
| }, | |
| "xml"); | |
| } | |
| }, | |
| execute: function(direct_obj) { | |
| var eng_word = direct_obj.text; | |
| var url = "http://dict.cn/"; | |
| url += eng_word; | |
| Utils.openUrlInBrowser(url); | |
| } | |
| }); | |
| CmdUtils.__globalObject["cmd_dcn"] = CmdUtils.__globalObject["cmd_dictcn"]; | |
| noun_type_downloadlist = new CmdUtils.NounType("DownloadList", ["aria2", "html"], "aria2"); | |
| CmdUtils.CreateCommand({ | |
| name: "getmedia", | |
| icon: "http://vi.appspot.com/favicon.ico", | |
| homepage: "http://vi.appspot.com/", | |
| author: { | |
| name: "azuwis", | |
| email: "[email protected]" | |
| }, | |
| license: "GPL", | |
| description: "Get media urls", | |
| help: "Just preview the command, media urls will be copied to clipboard when success.", | |
| takes: { | |
| "download list type": noun_type_downloadlist | |
| }, | |
| modifiers: { | |
| filter: noun_arb_text | |
| }, | |
| _data: {}, | |
| _getTodouFLVFromIID: function(iid, hd) { | |
| var url; | |
| if (hd) { | |
| url = 'http://v2.tudou.com/v2/kili?id='; | |
| } else { | |
| url = 'http://v2.tudou.com/v2/cdn?id='; | |
| } | |
| var list; | |
| jQuery.ajax({ | |
| url: url + iid, | |
| async: false, | |
| success: function(data) { | |
| var rslt = jQuery(data).find("v"); | |
| list = { | |
| url: rslt.find("f").map(function() { | |
| return jQuery(this).text(); | |
| }).get(), | |
| title: rslt.attr("title") | |
| }; | |
| } | |
| }); | |
| return list; | |
| }, | |
| _handleTudouPlaylist: function(pblock, doc) { | |
| pblock.innerHTML = "Working..."; | |
| var iidDoc = jQuery(doc).find("div#slidePlaylist li"); | |
| var iidMax = iidDoc.length; | |
| var list = []; | |
| var upthis = this; | |
| iidDoc.each(function(i) { | |
| var iid = this.id.substring(8); | |
| list[i] = upthis._getTodouFLVFromIID(iid); | |
| pblock.innerHTML = "Working..." + (i + 1) + "/" + iidMax; | |
| }); | |
| return list; | |
| }, | |
| _handleTudouHDPlaylist: function(pblock, doc) { | |
| pblock.innerHTML = "Working..."; | |
| var uri = Utils.url(doc.documentURI); | |
| var videoId = uri.spec.match(/program\/(\d+)\//)[1]; | |
| var list = []; | |
| var upthis = this; | |
| var pageDoc = jQuery(doc).find("div#edList div.pagebox li"); | |
| var pageMax = pageDoc.length; | |
| if (pageMax === 0) { | |
| var iidDoc = jQuery(doc).find("script[type=text/javascript]:not([src])").text(); | |
| var iid = iidDoc.match(/iid:\s"(\d+)"/)[1]; | |
| var list = []; | |
| list[0] = this._getTodouFLVFromIID(iid, true); | |
| } else { | |
| pageDoc.each(function(i) { | |
| jQuery.ajax({ | |
| url: "http://hd.tudou.com/ajax/albumVideos.html?videoId=" + videoId + "&pageNumer=" + (i + 1), | |
| async: false, | |
| success: function(data) { | |
| var urlDoc = jQuery(data).find("a.mylist_hook"); | |
| var urlMax = urlDoc.length; | |
| urlDoc.each(function(j) { | |
| var url = "http://hd.tudou.com" + jQuery(this).attr("href"); | |
| jQuery.ajax({ | |
| url: url, | |
| async: false, | |
| success: function(data) { | |
| var index = i * 10 + j; | |
| var iid = data.match(/\siid:\s*"(\d+)",/)[1]; | |
| list[index] = upthis._getTodouFLVFromIID(iid, true); | |
| pblock.innerHTML = "Working..." + (index + 1) + "/" + (pageMax * 10 - 10 + urlMax); | |
| } | |
| }); | |
| }); | |
| } | |
| }); | |
| }); | |
| } | |
| return list; | |
| }, | |
| _handleTudouSingleVideo: function(pblock, doc) { | |
| var iid = jQuery(doc).find("div.shareButton a").attr("href").match(/iid=(\d+)/)[1]; | |
| pblock.innerHTML = "Working..."; | |
| var list = this._getTodouFLVFromIID(iid); | |
| var tmpl = "<p>Title: ${list.title}<br/>Urls:<br/>{for i in list.url}${i}<br/>{/for}<p/>"; | |
| pblock.innerHTML += CmdUtils.renderTemplate(tmpl, { | |
| list: list | |
| }); | |
| return [list]; | |
| }, | |
| _handle17173SingleVideo: function(pblock, doc) { | |
| var videoID = jQuery(doc).find("a[href^=/shareall.php]").attr("href").match(/id=(\d+)/)[1]; | |
| pblock.innerHTML = "Working..."; | |
| var list = []; | |
| jQuery.ajax({ | |
| url: "http://vlog.17173.com/port/pconfig_r.php?id=" + videoID, | |
| async: false, | |
| success: function(data) { | |
| var rslt = data.match(/MD5=(.*)/)[1]; | |
| var urls = rslt.split("||"); | |
| list = jQuery.map(urls, | |
| function(item) { | |
| return {url: [item]}; | |
| }); | |
| } | |
| }); | |
| return list; | |
| }, | |
| _genAriaList: function(list, filter) { | |
| return jQuery.map(list, | |
| function(item) { | |
| var url = item.url; | |
| if (filter.length > 0) { | |
| url = jQuery.grep(url, function(a) { | |
| return a.match(filter); | |
| }); | |
| } | |
| var out = item.title ? "\n out=" + item.title + ".flv" : ""; | |
| return url.join("\t") + out; | |
| }).join("\n"); | |
| }, | |
| _genHTMLList: function(list) { | |
| var tmpl = "{for i in list}<a href='${i.url[0]}'>${i.title}.flv</a><br/>\n{/for}\n"; | |
| return CmdUtils.renderTemplate(tmpl, { | |
| list: list | |
| }); | |
| }, | |
| preview: function(pblock, input, mods) { | |
| var downloadListType = input.text; | |
| var filter = mods.filter.text; | |
| var doc = CmdUtils.getDocument(); | |
| var uri = Utils.url(doc.documentURI); | |
| this._data.handler = { | |
| "http://vlog.17173.com/v/": this._handle17173SingleVideo, | |
| "http://www.tudou.com/playlist/playindex.do": this._handleTudouPlaylist, | |
| "http://www.tudou.com/programs/view/": this._handleTudouSingleVideo, | |
| "http://hd.tudou.com/program/": this._handleTudouHDPlaylist | |
| }; | |
| var handler = null; | |
| for (var p in this._data.handler) { | |
| if (uri.spec.match(p)) { | |
| handler = this._data.handler[p]; | |
| break; | |
| } | |
| } | |
| if (handler) { | |
| this._data.list = handler.call(this, pblock, doc); | |
| } else { | |
| var support = []; | |
| for (var i in this._data.handler) { | |
| support.push("<li>" + i + "</li>"); | |
| } | |
| pblock.innerHTML = "Preview this command in media web page. Support:<ul>" + support.join("") + "</ul>"; | |
| return; | |
| } | |
| if (downloadListType === "aria2") { | |
| CmdUtils.copyToClipboard(this._genAriaList(this._data.list, filter)); | |
| var tmpl = "<p>Done! ${list.length} items copied to clipboard. Url filter: {if filter}${filter}{else}None{/if}<p/><p>Press <b>Enter</b> will:<ul><li>Open media url in new tab, if there is only one media.</li><li>Modify this page with media links, if there're multiple media (for download manager such as DownloadThemAll, Flashgot).</li></ul><p/>"; | |
| pblock.innerHTML += CmdUtils.renderTemplate(tmpl, { | |
| list: this._data.list, | |
| filter: filter | |
| }); | |
| } | |
| }, | |
| execute: function(input) { | |
| var doc = CmdUtils.getDocument(); | |
| if (this._data.list.length === 1) { | |
| Utils.openUrlInBrowser(this._data.list[0].url[0]); | |
| } else { | |
| jQuery(doc).find("html").html(this._genHTMLList(this._data.list)); | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment