Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
quran jetpack script
/* quran jetpack script
* --------------------
* a jetpack script which introduces a context menu that allows replacing
* instances of sura:ayah in textareas with the actual ayah text from the
* quran. tested under jetpack 0.6.2.
*
* http://whatstheplot.com/blog/2009/11/16/quran-jetpack-script/
*/
jetpack.future.import("menu");
jetpack.future.import("selection");
function getAyah(textarea, lang){
var sel = textarea.value.substring(textarea.selectionStart,
textarea.selectionEnd);
var res = /^(\d+):(\d+)$/.exec(sel);
if (res != null){
var sura = res[1];
var ayah = res[2];
}
else return;
var resultText = '';
var url = "http://alpha.api.searchquran.net/v1/sura/" + sura + "/" + ayah;
var params = {langs: lang};
jQuery.getJSON(url, params, function(data){
var start = textarea.value.substring(0, textarea.selectionStart);
var end = textarea.value.substring(textarea.selectionEnd);
// just in case, validate that what is selected is what we just got
var mid = textarea.value.substring(textarea.selectionStart,
textarea.selectionEnd);
var origUrl = url.substring(url.indexOf('sura/') + 5);
if (origUrl == (mid.replace(":", '/'))){
jQuery.each(data.verses, function(i, item){
resultText += item[lang];
});
textarea.value = start + resultText +
" (" + sura + ":" + ayah + ")" + end;
}
else jetpack.notifications.show("please try again...");
});
return;
}
jetpack.menu.context.page.on("textarea").beforeShow =
function (menu, context) {
var textarea = context.node;
var sel = textarea.value.substring(textarea.selectionStart,
textarea.selectionEnd);
if (sel){
var res = /^(\d+):(\d+)$/g.exec(sel);
if (res != null){
menu.reset();
menu.add({
label: "Get this Ayah",
icon: "http://whatstheplot.com/misc/quran.jetpack/favicon.ico",
menu: new jetpack.Menu([
{ label: "Arabic [no Tashkeel]", data: 4 },
{ label: "Arabic", data: 2 },
{ label: "Transliteration", data: 128 },
{ label: "Sahih International", data: 256 },
{ label: "Muhsin Khan", data: 8 }
]),
command: function (menuitem){
getAyah(textarea, menuitem.data);
}
});
}
}
else menu.reset();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment