Created
October 5, 2008 20:02
-
-
Save isidorosp/14919 to your computer and use it in GitHub Desktop.
dictionary.js - a revised version of the "define" command
This file contains 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
function dictionaryWord(word, callback) { | |
var url = "http://services.aonaware.com/DictService/DictService.asmx/DefineInDict"; | |
var params = Utils.paramsToString({ | |
dictId: "wn", //wn: WordNet, gcide: Collaborative Dictionary | |
word: word | |
}); | |
Utils.ajaxGet(url + params, function(xml) { | |
CmdUtils.loadJQuery( function(jQuery) { | |
var text = jQuery(xml).find("WordDefinition").text(); | |
callback(text); | |
}); | |
}); | |
} | |
CmdUtils.CreateCommand({ | |
name: "dictionary", | |
description: "Gives the meaning of a word.", | |
author: { name: "Isidoros Passadis", email: "isidoros.passadis@gmail.com"}, | |
help: "Try issuing "dictionary ubiquity"", | |
license: "MPL", | |
icon: "http://dictionary.reference.com/favicon.ico", | |
takes: {"word": noun_arb_text}, | |
execute: function( directObj ) { | |
var word = directObj.text; | |
Utils.openUrlInBrowser( "http://dictionary.reference.com/search?q=" + escape(word) ); | |
}, | |
preview: function( pblock, directObj ) { | |
var word = directObj.text; | |
if (word.length < 2) | |
pblock.innerHTML = "Gives the definition of a word."; | |
else { | |
pblock.innerHTML = "Gives the definition of the word " + word + "."; | |
dictionaryWord( word, function(text){ | |
var content; | |
var header; | |
var styles = '<style type="text/css">.content{ height: 100%; min-height: 140px; overflow: auto; position: relative; color: #000; background-color: #fff; border: 2px solid #ddd; -moz-border-radius: 15px 0 15px 0; z-index: 5;} .body {position: relative; padding-top: 40px; padding-left: 50px; padding-right: 5px; max-height: 500px; overflow:auto;} .part_of_speech span {font-style: italic;} .extra { position: absolute; left: -10px; top: -50px; color: #DDD; font-size: 10em; z-index: -1;} .header { position: absolute; right: 20px; color: #666; background-color: white; z-index: 10; } .word {color: darkorange; font-weight: bold;} .definitions {padding-left: 15px;}</style>'; | |
header = "Definition for <span class='word'>"+word+"</span>."; | |
text = text.replace(word, ""); | |
text = text.replace(/(\d+:)/g, "<br /><b>$&</b>"); | |
text = text.replace(/<br \/><b>1:<\/b>/g, "<b>1:</b>"); | |
text = text.replace(/(\s([nv]|adj|adv)\s)/g, "</div><div class='part_of_speech'><span>$&</span><div class='definitions'>"); | |
text = text.replace(/<\/div>/, ""); | |
text = text.replace(/\[[^\]]*\]/g, ""); | |
body = text; | |
extra = '<div class="extra">dict</div>'; | |
if (text == "") { | |
body = "No definitions were found for the word <span class='word'>"+word+"</span>. Please check the spelling and try again."; | |
} | |
content = '<div class="content">'+extra+'<div class="header">'+header+'</div>'+'<div class="body">'+body+'</p></div>'; | |
pblock.innerHTML = styles+"\n"+content; | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment