// ==UserScript== // @name Sanskrit Tools - Toolbar // @namespace stgeorge // @description Sanskrit Language Tools - Quick access to Sanskrit dictionary, thesarus, news and other tools, on Firefox and Chrome browsers. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js // @match *://*/* // @version 3.4.10 // @downloadURL none // ==/UserScript== (function() { let $j = jQuery.noConflict(); // ==================================================================== // Options. Modify as needed. // TODO: Expose these options via GM. Some day ... let OPTION_DEBUG = false; // When on spokensanskrit.org site, Set to true to change word completion list // to show up below the input field instead of on the side. let OPTION_IN_PLACE_MATCH = true; // Remove extraneous elements. let OPTION_TRIM = true; // End options. Don't change anything below this. // ==================================================================== // Grammar stuff. let SANSKRIT_SITE_OLD = 'spokensanskrit.org'; let SANSKRIT_SITE_NEW = 'learnsanskrit.cc'; let SANSKRIT_SITES = [SANSKRIT_SITE_OLD,SANSKRIT_SITE_NEW]; let verbMatch = /(verb)\s*(.*)/; let verbRootMatch = /{\s*(.*)\s*}/; let verbClassMatch = /\s*([0-9]+)\s*/g; let nounMatch = /\b([fmn](?=\.))/g; let nounRootMatch = /^\s*(.*)\s*$/; let vurl = 'http://sanskrit.inria.fr/cgi-bin/SKT/sktconjug?lex=SH&q=%Q%&t=KH&c=%C%&font=deva'; let nurl = 'http://sanskrit.inria.fr/cgi-bin/SKT/sktdeclin?lex=SH&q=%Q%&t=KH&g=%G%&font=deva'; let surl = 'http://sanskrit.uohyd.ernet.in/cgi-bin/scl/SHMT/generate.cgi?dic=amara&word=%Q%'; let genders = { f: 'Fem', n: 'Neu', m: 'Mas' }; let gender_names = { f: 'feminine', n: 'neuter', m: 'masculine' }; let dictTable = null; let dictTableRows = null; let dictTableRowsSorted = null; let tableSorted = false; function isDictionarySite() { for (let i = 0; i < SANSKRIT_SITES.length; ++i) { let s = SANSKRIT_SITES[i]; if (document.URL.indexOf(s) != -1) return true; } } function buildGrammarUI() { if (OPTION_TRIM) trimFat(); fixSuggestBox(); addGrammarLinks(); } function trimFat() { $j('.table0').not('.bgcolor2').not('.bgcolor0').remove(); } function fixSuggestBox() { let ans = $j('#answer2'); if (OPTION_IN_PLACE_MATCH) { let target = $j('input[name="tran_input"]').closest('form'); ans.detach(); target.after(ans); ans.attr('align', 'center'); ans.css({'position':'relative', 'top':'-12em'}); } observe(ans); } function observe(ele) { let observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { let newNodes = mutation.addedNodes; if(newNodes !== null) { $j(newNodes).each(function() { if (this.nodeName == 'BR') $j(this).remove(); }); } }); }); observer.observe(ele[0], { attributes: true, childList: true, characterData: true }); } function initSorted() { if (dictTable === null) { dictTable = $j('tr.bgcolor2').first().closest('table'); dictTableRows = dictTable.find('tbody >tr:has(td)').get(); dictTableRowsSorted = $j.extend([], dictTableRows); dictTableRowsSorted.sort(function(a, b) { let val1 = $j(a).children('td').first().text(); let val2 = $j(b).children('td').first().text(); return val1.localeCompare(val2); }); } } function sortTable() { let rows = tableSorted ? dictTableRows : dictTableRowsSorted; let tb = dictTable.children('tbody').first(); tb.empty(); $j.each(rows, function(index, row) { tb.append(row); }); tableSorted = !tableSorted; } function addGrammarLinks() { let line = 1; $j('tr.bgcolor2,tr.bgcolor0,tr.highlight').each(function() { let row = $j(this); // Each row is of the form: // sans_text grammar_info translit_text meaning let col = row.children().first(); let sansText = col.text().trim(); col = col.next(); let grammarInfo = col.text().trim(); col = col.next(); let transText = col.text().trim(); _debug("line " + (line++) + "='" + sansText + "' '" + grammarInfo + "' '" + transText + "'"); let links = []; if (matchVerb(sansText, grammarInfo, transText, links) || matchNoun(sansText, grammarInfo, transText, links)) { makeURLs(row, links); } _debug('-----'); }); } function matchVerb(sansText, grammarInfo, transText, links) { // Grammar is of the form: verb N let a = grammarInfo.match(verbMatch); if (a && a[1] == 'verb') { // transText is of the form xlit_word (xlit_root). // We want the root. let b = transText.match(verbRootMatch); if (!b || !b[1]) return false; b[1] = b[1].trim().replace(/[\s-]/g, "") _debug('verb: matching ' + transText + ' with verbroot'); if (b[1].match(/[^A-Za-z]/)) return false; let n; // For verbs, see if grammar_info has the gaNA info. if (a[2]) n = a[2].trim().match(verbClassMatch); if (!(n && n[0])) { return false; } // At this point, b[1] is the transliterated verb root, // sansText is the devangari verb root, and n the gaNa. _debug('verb=' + b[1]); _debug('ganas=' + n); for (let i = 0; i < n.length; ++i) { links.push({ tooltip: 'Inflections for ' + a[1] + '(' + n[i].trim() + ') ' + sansText, url: vurl.replace('%Q%', b[1]).replace('%C%', n[i].trim()), sym: '▸', target: 'l_grammar', }); } return true; } return false; } function matchNoun(sansText, grammarInfo, transText, links) { // grammar, in turn, is of the forms: m./f./n./adj. etc (for nouns) _debug('noun: matching ' + grammarInfo + ' with noun'); let a = grammarInfo.match(nounMatch); if (!(a && a[0])) return false; _debug('noun: matching ' + transText + ' with nounroot'); let b = transText.match(nounRootMatch); if (!b || !b[1]) return false; b[1] = b[1].trim(); if (b[1].match(/[^A-Za-z]/)) return false; // At this point, b[1] is the xlit noun, sansText is the // devanagari noun, and a is one or more lingas. _debug('noun=' + b[1]); _debug('lingams=' + a); if (a.length > 0) { for (let i = 0; i < a.length; ++i) { links.push({ url: nurl.replace('%Q%', b[1]).replace('%G%', genders[a[i]]), tooltip: 'Inflections for ' + gender_names[a[i]] + ' noun ' + sansText, sym: '▸', target: 'l_grammar', }); } return true; } return false; } function makeURLs(row, links) { let ltd = row.children().first(); let html; html = ''; for (let i in links) { l = links[i]; ltd.attr('valign','top'); html += ''+l.sym+''; } _debug("link: " + l.url + " --> " + l.tooltip); ltd.html(html + ' ' + ltd.html()); ltd.attr('align', 'left'); return true; } // =============================================================== // Toolbar stuff. // =============================================================== let IGNORES = [ 'mail.yahoo.com', 'groups.yahoo.com', SANSKRIT_SITE_OLD, SANSKRIT_SITE_NEW, ]; let ALLOW_ANCHORS = [ 'sanskrit.uohyd.ernet.in/cgi-bin/scl/SHMT/generate.cgi', ]; let TOOLBAR_LINKS = [ [ "vArtAvaliH", "https://www.youtube.com/playlist?list=PLxx0m3vtiqMZGmsUEVeTAuWIXqc9fTMHy", "l_news", "वार्तावलिः" ], [ "samprati vArtAH", "http://samprativartah.in/", "l_mag2", "सम्प्रतिवार्ताः" ], [ "sudharmA", "http://epaper.sudharmasanskritdaily.in/", "l_mag3", "सुधर्मा" ], [ "sambhASaNa sandezaH", "http://www.sambhashanasandesha.in/", "l_mag1", "सम्भाषण सन्देशः" ], [ "mAhezvarasUtrANi", "http://en.wikipedia.org/wiki/Siva_Sutra#Text", "l_msutra", "माहेश्वरसूत्राणि" ], [ "sandhiH", "http://sanskrit.jnu.ac.in/sandhi/viccheda.jsp", "l_sandhi", "सन्धिः" ], [ "Noun/Verb", "http://sanskrit.inria.fr/DICO/grammar.fr.html", "l_inria", "शब्द-/धातु-रूपावली" ], [ "Books", "http://www.sanskrit.nic.in/ebooks.php", "l_books", "पुस्तकानि" ], [ "Wikipedia", "http://sa.wikipedia.org", "l_wiki", "विकिपीडिया" ], ]; let TOOLBAR_HTML = '\
| \
\
\
\
\
| \