Skip to content

Instantly share code, notes, and snippets.

@myuhe
Created September 3, 2008 06:06
Show Gist options
  • Save myuhe/8547 to your computer and use it in GitHub Desktop.
Save myuhe/8547 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
name: "wikipedia-ja",
takes: {search: noun_arb_text},
locale: "en-US",
homepage: "http://theunfocused.net/moz/ubiquity/verbs/",
author: {name: "Blair McBride", email: "blair@theunfocused.net"},
license: "MPL",
icon: "http://ja.wikipedia.org/favicon.ico",
description: "Searches Wikipedia for your words.",
preview: function(previewBlock, directObject) {
var apiUrl = "http://ja.wikipedia.org/w/api.php";
var searchText = jQuery.trim(directObject.text);
if(searchText.length < 1) {
previewBlock.innerHTML = "Searches Wikipedia";
return;
}
var previewTemplate = "Searching Wikipedia for <b>${query}</b> ...";
var previewData = {query: searchText};
previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
var apiParams = {
format: "json",
action: "query",
list: "search",
srlimit: 5, // is this a good limit?
srwhat: "text",
srsearch: searchText
};
jQuery.ajax({
type: "GET",
url: apiUrl,
data: apiParams,
datatype: "string",
error: function() {
previewBlock.innerHTML = "Error searching Wikipedia";
},
success: function(searchReponse) {
searchReponse = Utils.decodeJson(searchReponse);
if(!("query" in searchReponse && "search" in searchReponse.query)) {
previewBlock.innerHTML = "Error searching Wikipedia";
return;
}
function generateWikipediaLink(title) {
var wikipediaUrl = "http://en.wikipedia.org/wiki/";
return wikipediaUrl + title.replace(/ /g, "_");
}
previewData = {
query: searchText,
results: searchReponse.query.search,
_MODIFIERS: {wikilink: generateWikipediaLink}
};
previewBlock.innerHTML = CmdUtils.renderTemplate({file:"wikipedia.html"}, previewData);
jQuery(previewBlock).find("div[wikiarticle]").each(function() {
var article = jQuery(this).attr("wikiarticle");
fetchWikipediaArticle(this, article);
});
}
});
},
execute: function(directObject) {
var searchUrl = "http://ja.wikipedia.org/wiki/Special:Search";
var searchParams = {search: directObject.text};
Utils.openUrlInBrowser(searchUrl + Utils.paramsToString(searchParams));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment