Last active
December 24, 2015 15:28
-
-
Save 958/6820032 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
// Info | |
let PLUGIN_INFO = | |
<KeySnailPlugin> | |
<name>Quick Google</name> | |
<description>Search from Google with KeySnail</description> | |
<description lang="ja">KeySnail 内で Google 検索</description> | |
<updateURL>https://gist.github.com/958/6820032/raw/quickggl.ks.js</updateURL> | |
<author>958</author> | |
<version>0.0.1</version> | |
<license>MIT</license> | |
<minVersion>1.8.0</minVersion> | |
<include>main</include> | |
<detail lang="ja"><![CDATA[ | |
=== 使い方 === | |
KeySnail の prompt 内で Google 検索を実行し、結果を表示します | |
キーマップを変更したい人は、次のような設定を .keysnail.js の PRESERVE エリアへ | |
>|javascript| | |
plugins.options["quick_google.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( | |
['google'], 'Quick google search', | |
function(args) ext.exec('quick-google', args.join(' ')), | |
{ | |
argCount: '*', | |
completer: function(args, ex) completer.fetch.google(ex.left || '', ex.whole || '') | |
}, | |
true | |
); | |
||< | |
=== 謝辞 === | |
以下のスクリプトを参考にしました | |
//https://gist.github.com/445519 | |
]]></detail> | |
</KeySnailPlugin>; | |
// Option | |
let pOptions = plugins.setupOptions("quick_google", { | |
"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 quickGoogle(q){ | |
let query = "", 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 parseResponse(text) { | |
let results; | |
try { | |
results = JSON.parse(text).responseData.results; | |
} catch(e) { } | |
if (!results) return; | |
results.forEach(function(result) { | |
collection.push([util.getFaviconPath(result.unescapedUrl), result.titleNoFormatting, result.unescapedUrl]); | |
}) | |
} | |
function searchWord(cb) { | |
let params = { v: '1.0', rsz: 'large', start: collection.length, q: encodeURIComponent(query)}; | |
util.requestGet("http://ajax.googleapis.com/ajax/services/search/web", { | |
params: params, | |
header: { "Content-type": "application/json" }, | |
callback: function(xhr) { | |
if (xhr.status != 200) return; | |
parseResponse(xhr.responseText); | |
cb(); | |
} | |
}); | |
} | |
let fetchingPreviousNow = false; | |
function doFetchPrevious() { | |
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); | |
}); | |
} | |
function showSelector() { | |
prompt.finish(true); | |
prompt.selector({ | |
message : "Search result:", | |
acyclic : true, | |
initialIndex: beforeIndex, | |
collection : collection, | |
flags : [ICON | IGNORE, 0, 0], | |
style : [0, style.prompt.url], | |
header : ["Title", "URL"], | |
keymap : pOptions["keymap"], | |
beforeSelection : function (arg) { | |
if (!arg.row || fetchingPreviousNow) | |
return; | |
if (collection.length > 1 && arg.i === collection.length - 1 && arg.i === beforeIndex) { | |
doFetchPrevious(); | |
return; | |
} | |
beforeIndex = arg.i; | |
}, | |
actions : [ | |
[ | |
function (aIndex, items) openUILinkIn(items[aIndex][2], 'current'), | |
M({ja: '選択中アイテムを開く', en: "Open in new tab"}), | |
"open-current-tab" | |
], | |
[ | |
function (aIndex, items) openUILinkIn(items[aIndex][2], 'tab'), | |
M({ja: '選択中アイテムを新しいタブで開く', en: "Open in new tab"}), | |
"open-new-tab" | |
], | |
[ | |
function (aIndex, items) openUILinkIn(items[aIndex][2], 'tabshifted'), | |
M({ja: '選択中アイテムを新しいバックグラウンドタブで開く', en: "Open in background new tab"}), | |
"open-background-tab" | |
] | |
] | |
}); | |
} | |
} | |
// Add ext | |
plugins.withProvides(function (provide) { | |
provide("quick-google", function (ev, arg) quickGoogle(arg), 'Quick Google Search'); | |
}, PLUGIN_INFO); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment