aboutsummaryrefslogtreecommitdiffstats
path: root/lookupDictionary.js
diff options
context:
space:
mode:
authordrry2008-04-07 04:33:24 +0000
committerdrry2008-04-07 04:33:24 +0000
commit8606eb07a4fd2cf38bbe333bfe971e62f06c3f02 (patch)
tree33ae7d7a835da4d58066a6af491b494ec96cc6fd /lookupDictionary.js
parentf7ac591b97db112cdff08c8b7a152aaccfcb4294 (diff)
downloadvimperator-plugins-8606eb07a4fd2cf38bbe333bfe971e62f06c3f02.tar.bz2
lang/javascript/vimperator-plugins/trunk/lookupDictionary.js:
* typo を修正しました。 * インデントを修整しました。 * ほか。 git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@9066 d0d07461-0603-4401-acd4-de1884942a52
Diffstat (limited to 'lookupDictionary.js')
-rw-r--r--lookupDictionary.js160
1 files changed, 78 insertions, 82 deletions
diff --git a/lookupDictionary.js b/lookupDictionary.js
index fd27d85..4d7c822 100644
--- a/lookupDictionary.js
+++ b/lookupDictionary.js
@@ -1,88 +1,84 @@
/**
- * lookup dictionary (vimperator plugin)
- * For vimperator 0.5.3
+ * lookup dictionary (Vimperator plugin)
+ * For Vimperator 0.6pre
* @author teramako teramako@gmail.com
* @version 0.1
*
- * Lookup web dictionary
- * And show the result in the bottom of the window
+ * Lookup words from Web dictionaries
+ * And show the results in the bottom of the window
*/
(function(){
-const dictionalyList = [
- {
- names: ['eiji[ro]'],
- url: 'http://eow.alc.co.jp/%s/UTF-8/',
- shortHelp: '英辞郎',
- xpath: '//*[@id="resultList"]'
- },{
- names: ['goo'],
- url: 'http://dictionary.goo.ne.jp/search.php?MT=%s&kind=all&mode=0',
- shortHelp: 'goo辞書',
- encode: 'EUC-JP',
- xpath: '//div[@id="incontents"]/*[@class="ch04" or @class="fs14" or contains(@class,"diclst")]',
- multi: true
- }
-];
-for (var i=0; i<dictionalyList.length; i++){
- let j = i;
- liberator.commands.addUserCommand(
- dictionalyList[j].names,
- dictionalyList[j].shortHelp,
- function(arg,special){
- var sel = window.content.document.getSelection();
- if (special && sel) arg = sel;
- if (!arg) return;
- var url;
- if (dictionalyList[j].encode){
- var ttbu = Components.classes['@mozilla.org/intl/texttosuburi;1']
- .getService( Components.interfaces.nsITextToSubURI);
- url = dictionalyList[j].url.replace(/%s/g, ttbu.ConvertAndEscape(dictionalyList[j].encode, arg));
- } else {
- url = dictionalyList[j].url.replace(/%s/g,encodeURI(arg));
- }
- //liberator.log('URL: ' +url);
- var result;
- getHTML(url, function(str){
- var doc = createHTMLDocument(str);
- var result = getNodeFromXPath(dictionalyList[j].xpath, doc, dictionalyList[j].multi);
- if (!result){
- liberator.echoerr('Nothing to show...');
- }
- var xs = new XMLSerializer();
- liberator.echo('<base href="' + url + '"/>' + xs.serializeToString( result ), true);
- });
- },{}
- );
-}
+[{
+ names: ['eiji[ro]'],
+ url: 'http://eow.alc.co.jp/%s/UTF-8/',
+ shortHelp: 'SPACE ALC (英辞郎 on the Web)',
+ xpath: '//*[@id="resultList"]'
+},{
+ names: ['goo'],
+ url: 'http://dictionary.goo.ne.jp/search.php?MT=%s&kind=all&mode=0',
+ shortHelp: 'goo 辞書',
+ encode: 'EUC-JP',
+ xpath: '//div[@id="incontents"]/*[@class="ch04" or @class="fs14" or contains(@class,"diclst")]',
+ multi: true
+}].forEach(function(dictionary){
+ liberator.commands.addUserCommand(
+ dictionary.names,
+ dictionary.shortHelp,
+ function(arg,special){
+ var sel = window.content.document.getSelection();
+ if (special && sel) arg = sel;
+ if (!arg) return;
+ var url;
+ if (dictionary.encode){
+ var ttbu = Components.classes['@mozilla.org/intl/texttosuburi;1']
+ .getService( Components.interfaces.nsITextToSubURI);
+ url = dictionary.url.replace(/%s/g, ttbu.ConvertAndEscape(dictionary.encode, arg));
+ } else {
+ url = dictionary.url.replace(/%s/g,encodeURI(arg));
+ }
+ //liberator.log('URL: ' +url);
+ var result;
+ getHTML(url, function(str){
+ var doc = createHTMLDocument(str);
+ var result = getNodeFromXPath(dictionary.xpath, doc, dictionary.multi);
+ if (!result){
+ liberator.echoerr('Nothing to show...');
+ }
+ var xs = new XMLSerializer();
+ liberator.echo('<base href="' + url + '"/>' + xs.serializeToString( result ), true);
+ });
+ },{}
+ );
+});
/**
* @param {String} url
* @param {Function} callback
*/
function getHTML(url, callback){
- var xhr= new XMLHttpRequest();
- xhr.onreadystatechange = function(){
- if (xhr.readyState == 4){
- if (xhr.status == 200){
- callback.call(this,xhr.responseText);
- } else {
- throw new Error(xhr.statusText);
- }
- }
- };
- xhr.open('GET',url,true);
- xhr.send(null);
+ var xhr= new XMLHttpRequest();
+ xhr.onreadystatechange = function(){
+ if (xhr.readyState == 4){
+ if (xhr.status == 200){
+ callback.call(this,xhr.responseText);
+ } else {
+ throw new Error(xhr.statusText);
+ }
+ }
+ };
+ xhr.open('GET',url,true);
+ xhr.send(null);
}
/**
* @param {String} str
* @return {DOMDocument}
*/
function createHTMLDocument(str){
- str = str.replace(/^[\s\S]*?<html[^>]*>|[\r\n]+|<\/html\s*>[\s\S]*$/ig, '');
- var htmlFragment = document.implementation.createDocument(null,'html',null);
- var range = document.createRange();
- range.setStartAfter(window.content.document.body);
- htmlFragment.documentElement.appendChild(htmlFragment.importNode(range.createContextualFragment(str),true));
- return htmlFragment;
+ str = str.replace(/^[\s\S]*?<html(?:\s[^>]+?)?>|[\r\n]+|<\/html\s*>[\S\s]*$/ig, '');
+ var htmlFragment = document.implementation.createDocument(null,'html',null);
+ var range = document.createRange();
+ range.setStartAfter(window.content.document.body);
+ htmlFragment.documentElement.appendChild(htmlFragment.importNode(range.createContextualFragment(str),true));
+ return htmlFragment;
}
/**
* @param {String} xpath XPath Expression
@@ -91,20 +87,20 @@ function createHTMLDocument(str){
* @return {Element}
*/
function getNodeFromXPath(xpath,doc,isMulti){
- if (!xpath || !doc) return;
- var result;
- if (isMulti){
- var nodesSnapshot = doc.evaluate(xpath,doc.documentElement,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
- if (nodesSnapshot.snapshotLength == 0) return;
- result = document.createElementNS(null,'div');
- for (var i=0; i<nodesSnapshot.snapshotLength; result.appendChild(nodesSnapshot.snapshotItem(i++)));
- } else {
- var node = doc.evaluate(xpath,doc.documentElement,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
- if (!node.singleNodeValue) return;
- result = node.singleNodeValue;
- }
- return result;
+ if (!xpath || !doc) return;
+ var result;
+ if (isMulti){
+ var nodesSnapshot = doc.evaluate(xpath,doc.documentElement,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
+ if (nodesSnapshot.snapshotLength == 0) return;
+ result = document.createElementNS(null,'div');
+ for (var i=0; i<nodesSnapshot.snapshotLength; result.appendChild(nodesSnapshot.snapshotItem(i++)));
+ } else {
+ var node = doc.evaluate(xpath,doc.documentElement,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
+ if (!node.singleNodeValue) return;
+ result = node.singleNodeValue;
+ }
+ return result;
}
})();
-// vim: set fdm=marker sw=4 ts=4 et:
+// vim: fdm=marker sw=4 ts=4 et: