Skip to content

Instantly share code, notes, and snippets.

@rostok
Created October 21, 2021 17:33
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 rostok/d4bbbeed3a5cea382bbe9253fa1f7926 to your computer and use it in GitHub Desktop.
Save rostok/d4bbbeed3a5cea382bbe9253fa1f7926 to your computer and use it in GitHub Desktop.
plwikipedia command for UbiChr
// UbiChr 'plwikipedia' command
CmdUtils.CreateCommand({
name:"plwikipedia",
description:"Search Polish Wikipedia for the given words",
author:{},
icon:"http://pl.wikipedia.org/favicon.ico",
homepage:"",
license:"",
preview:function wikipedia_preview(previewBlock, args) {
var args_format_html = "Polish";
var searchText = args.text.trim();
if (!searchText) {
previewBlock.innerHTML = "Searches Polish Wikipedia in " + args_format_html + ".";
return;
}
previewBlock.innerHTML = "Searching Polish Wikipedia for <b>" + args.text + "</b> ...";
function onerror() {
previewBlock.innerHTML = "<p class='error'>" + "Error searching Wikipedia" + "</p>";
}
var apiUrl = "http://pl.wikipedia.org/w/api.php";
CmdUtils.ajaxGetJSON("https://pl.wikipedia.org/w/api.php?action=query&list=search&srsearch="+searchText+"&srlimit=5&format=json", function (resp) {
function generateWikipediaLink(title) {
return "http://pl.wikipedia.org/wiki/" +title.replace(/ /g, "_");
}
function wikiAnchor(title) {
return "<a target=_blank href='"+generateWikipediaLink(title)+"'>"+title+"</a>";
}
previewBlock.innerHTML = "";
for (var i = 0; i < resp.query.search.length; i++) {
previewBlock.innerHTML += "<p>"+wikiAnchor(resp.query.search[i].title) + "<br>"+resp.query.search[i].snippet+"</p>";
}
jQuery(previewBlock).find("p").each((i,e)=>{
jQuery(e).attr("data-option","");
jQuery(e).attr("data-option-value", jQuery(e).find("a").first().attr("href"));
});
});
},
execute:function execute(args) {
var opt = args._opt_val || "";
if(opt.includes("://"))
CmdUtils.addTab(opt);
else {
var old = CmdUtils.SimpleUrlBasedCommand("http://pl.wikipedia.org/wiki/Special:Search?search={text}");
old(args);
}
},
old_execute:function(directObj) {
if (!directObj) return;
var opt = directObj._opt_val || "";
if(opt.includes("://")) {
CmdUtils.addTab(opt);
}
else {
var text = directObj.text;
text = encodeURIComponent(text);
var finalurl = url;
finalurl = finalurl.replace('{text}', text);
finalurl = finalurl.replace('{location}', CmdUtils.getLocation());
CmdUtils.addTab(finalurl);
}
},
names:["plwikipedia"]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment