Skip to content

Instantly share code, notes, and snippets.

@958
Last active September 25, 2015 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 958/921233 to your computer and use it in GitHub Desktop.
Save 958/921233 to your computer and use it in GitHub Desktop.
[keysnail]Quick Bing
// Info
let PLUGIN_INFO =
<KeySnailPlugin>
<name>Quick Bing</name>
<description>Search from Bing with KeySnail</description>
<description lang="ja">KeySnail 内で Bing 検索</description>
<author>958</author>
<updateURL>https://gist.github.com/958/921233/raw/quickbing.ks.js</updateURL>
<version>0.0.1</version>
<license>MIT</license>
<minVersion>1.8.0</minVersion>
<include>main</include>
<detail lang="ja"><![CDATA[
=== 使い方 ===
KeySnail の prompt 内で Bing 検索を実行し、結果を表示します
キーマップを変更したい人は、次のような設定を .keysnail.js の PRESERVE エリアへ
>|javascript|
plugins.options["quick_bing.keymap"] = {
"C-z" : "prompt-toggle-edit-mode",
"j" : "prompt-next-completion",
"k" : "prompt-previous-completion",
"g" : "prompt-beginning-of-candidates",
"G" : "prompt-end-of-candidates",
"D" : "prompt-cancel",
// specific actions
"o" : "open-current-tab,c",
"O" : "open-new-tab,c",
"C-o" : "open-background-tab,c"
};
||<
コマンドを追加したい場合は、こんな感じで
>|javascript|
shell.add(
['bing'], 'Quick Bing search',
function(args) ext.exec('quick-bing', args.join(' ')),
{
argCount: '*',
completer: function(args, ex) completer.fetch.google(ex.left || '', ex.whole || '')
},
true
);
||<
===なんで Bing===
XML で出力してくれるから。それ以上の理由はありません
]]></detail>
</KeySnailPlugin>;
// Option
let pOptions = plugins.setupOptions("quick_bing", {
"keymap" : {
preset: {
"C-z" : "prompt-toggle-edit-mode",
"j" : "prompt-next-completion",
"k" : "prompt-previous-completion",
"g" : "prompt-beginning-of-candidates",
"G" : "prompt-end-of-candidates",
"D" : "prompt-cancel",
// specific actions
"o" : "open-current-tab,c",
"O" : "open-new-tab,c",
"C-o" : "open-background-tab,c"
},
description: M({
ja: "メイン画面の操作用キーマップ",
en: "Local keymap for manipulation"
})
}
}, PLUGIN_INFO);
// Main
function quickBing(q){
let query = "", initialIndex = 0, beforeIndex = 0, collection = [];
(typeof q === 'string' && q.length > 0) ? search(q) : start();
function start() {
prompt.reader({
message : 'search:',
escapeWhiteSpace : false,
completer : completer.fetch.google,
callback : search
});
}
function search(q) {
query = q || query;
searchWord(function() (collection.length > 0) ? showSelector() : display.echoStatusBar("No results", 3000));
}
function parseRss(xml) {
let items = xml.getElementsByTagName('item');
if (!items) return;
for (let i = 0; i < items.length; i++) {
let title = items[i].getElementsByTagName('title')[0].textContent;
let url = items[i].getElementsByTagName('link')[0].textContent;
let desc = items[i].getElementsByTagName('description')[0].textContent;
let iconURL = util.getFaviconPath(url);
collection.push([iconURL, title, desc, url]);
}
}
function searchWord(cb, startIndex) {
let params = { format: 'rss', q: encodeURIComponent(query)};
if (startIndex)
params['first'] = startIndex;
util.requestGet('http://www.bing.com/search', {
params: params,
callback: function(xhr) {
if (xhr.status != 200) return;
if (xhr.responseXML) {
parseRss(xhr.responseXML);
cb();
}
}
});
}
let fetchingPreviousNow = false;
function doFetchPrevious(nextIndex) {
fetchingPreviousNow = true;
document.getElementById("keysnail-prompt-textbox").blur();
display.echoStatusBar("Fetching next entries ...", 3000);
searchWord(function(){
fetchingPreviousNow = false;
display.echoStatusBar("Fetching next entries Done", 3000);
(collection.length > 0) ? showSelector() : display.echoStatusBar("No results", 3000);
},
nextIndex);
}
function showSelector() {
prompt.finish(true);
prompt.selector({
message : "Search result:",
acyclic : true,
initialIndex: beforeIndex,
collection : collection,
flags : [ICON | IGNORE, 0, 0, 0],
style : [style.prompt.description, style.prompt.description, style.prompt.url],
header : ["Title", "Description", "URL"],
width : [25, 50, 25],
keymap : pOptions["keymap"],
beforeSelection : function (arg) {
if (!arg.row || fetchingPreviousNow)
return;
if (collection.length > 1 && arg.i === collection.length - 1 && arg.i === beforeIndex) {
doFetchPrevious(collection.length + 1);
return;
}
beforeIndex = arg.i;
},
actions : [
[
function (aIndex, items) openUILinkIn(items[aIndex][3], 'current'),
M({ja: '選択中アイテムを開く', en: "Open in new tab"}),
"open-current-tab"
],
[
function (aIndex, items) openUILinkIn(items[aIndex][3], 'tab'),
M({ja: '選択中アイテムを新しいタブで開く', en: "Open in new tab"}),
"open-new-tab"
],
[
function (aIndex, items) openUILinkIn(items[aIndex][3], 'tabshifted'),
M({ja: '選択中アイテムを新しいバックグラウンドタブで開く', en: "Open in background new tab"}),
"open-background-tab"
]
]
});
}
}
// Add ext
plugins.withProvides(function (provide) {
provide("quick-bing", function (ev, arg) quickBing(arg), 'Quick Bing Search');
}, PLUGIN_INFO);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment