Skip to content

Instantly share code, notes, and snippets.

@ahmedre
Created November 16, 2009 07:07
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 ahmedre/235821 to your computer and use it in GitHub Desktop.
Save ahmedre/235821 to your computer and use it in GitHub Desktop.
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