// ==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
// @grant GM_setValue
// @grant GM_getValue
// @version 2.4.11
// ==/UserScript==
(function() {
var DEBUG = false;
var IGNORES = [
'mail.yahoo.com',
'groups.yahoo.com',
'spokensanskrit.de',
];
var ALLOW_ANCHORS = [
'sanskrit.uohyd.ernet.in/cgi-bin/scl/SHMT/generate.cgi',
];
var GROUPS = [
{
// type is one of link, option.
type:'link', entries:
[
// Each link entry is of the form:
// id (in case we want to refer to the element), label
// tooltip, url, (optional) impl function. If an impl function is
// not provided, we simply go to the url.
[
'l_news', 'वार्ताः',
'Doordarshan Sanskrit News', 'www.youtube.com/user/sanskritanews/videos'
],
[
'l_mag1', 'पत्रिका १',
'Sambhaashana Sandesha', 'www.sambhashanasandesha.in/'
],
[
'l_mag2', 'पत्रिका २',
'Vishva Vani', 'www.speaksanskrit.org/vishvavani.shtml',
],
[
'l_books', 'पुस्तकानि',
'Books', 'www.sanskrit.nic.in/ebook.htm'
],
[
'l_wiki', 'विकिपीडिया',
'Wikipedia', 'sa.wikipedia.org'
],
[
'l_msutra', 'माहेश्वरसूत्राणि',
'Maheshwara Sutras', 'en.wikipedia.org/wiki/Siva_Sutra#Text'
]
]
},
{
type:'link', entries: [
[
'l_dict', 'शब्दकोशः',
'Dictionary', 'spokensanskrit.de', show_dict
],
/*
[
'l_grammar', 'तिङन्त-/सुबन्त-रूपाणि',
'Inflections', 'sanskrit.inria.fr/DICO/grammar.fr.html'
],
*/
[
'l_thes', ' अमरकोशः',
'Thesaurus', 'sanskrit.uohyd.ernet.in/scl/amarakosha/frame.html',
],
[
'l_sandhi', 'सन्धिः',
'Split words', 'tdil-dc.in/san/sandhi_splitter/index_dit.html',
],
[
'l_search', 'गवेषिका',
'Text Search Engine', 'sanskrit.uohyd.ernet.in:8080/searchengine/index1.jsp',
],
]
},
{
type:'option', entries: [
// Each option entry is of the form:
// id (in case we want to refer to the element),
// type (currently: label or check), label, tooltip, and (if
// type = check) whether checked or not.
[ 'o_auto', 'check', 'Auto-dictionary ', 'Double-clicking a word will automatically launch the dictionary', true],
[ 'o_mini', 'check', 'Mini dictionary ', 'Show the dictionary in a slim window', true],
]
}
];
var toolbarHTML;
var iconHTML;
var icon;
var cb = {};
var numClicks = 0;
var vdiv = null;
var allowAnchor = false;
var selectedText = null;
function main() {
for (var i in IGNORES) {
if (document.URL.indexOf(IGNORES[i]) != -1) {
return;
}
}
for (var i in ALLOW_ANCHORS) {
if (document.URL.indexOf(ALLOW_ANCHORS[i]) != -1) {
allowAnchor = true;
break;
}
}
init();
if (window.top != window.self) return;
make();
display();
}
function make() {
var elements = makeGroups();
toolbarHTML =
'<div id="s_toolbar" style="overflow:hidden; float:left">' +
'<div style="float:left; display:inline-block">' +
elements +
'</div>' +
'</div>' +
'<a id="a_dict" style="display:none" href="" target="l_dict"></a>'
;
iconHTML =
'<div id="icon" title="Click to show/hide Sanskrit Toolbar">\u0938' +
'</div>'
;
}
function makeGroups() {
var html = '<ul style="list-style:none;margin:0;padding:0">';
for (var i in GROUPS) {
var g = GROUPS[i];
var gt = g.type;
if (!gt) continue;
_debug('Adding group type ' + gt);
if (gt == 'link')
html += makeLinks(g);
else if (gt == 'option')
html += makeOptions(g);
else {
continue;
}
_debug('Adding seprator after group');
html += '<li class="st_li st_space"></li>';
}
return html + '</ul>';
}
function makeLinks(g) {
var html = '';
for (var i in g.entries) {
var e = g.entries[i];
if (!e[0] || e[0] == '_') continue;
_debug(' Making link ' + e[0] + ', title ' + e[2]);
html += '<li class="st_li">';
if (e[4]) {
html += '<a id="'+e[0]+'" title="'+e[2]+'" class="st_cb st_common st_link" target="'+e[0]+'">' +
e[1]+'</a>';
cb[e[0]] = e[4];
} else {
html += '<a id="'+e[0]+'" title="'+e[2]+'" class="st_common st_link" href="http://'+e[3]+'" target="'+e[0]+'">' +
e[1]+'</a>';
}
html += '</li>';
}
return html;
}
function makeOptions(g) {
var html = '<li class="st_li">';
for (var i in g.entries) {
var e = g.entries[i];
if (!e[0] || e[0] == '_') continue;
_debug(' Making option type ' + e[1] + 'id ' + e[0] + ', title ' + e[2]);
if (e[1] == 'label') {
html += '<span id="'+e[0]+'" class="st_common st_label">'+e[2]+'</span>';
} else if (e[1] == 'check') {
var checked = GM_getValue(e[0], e[4]) ? ' checked="checked"' : '';
html += '<div title="'+ e[3] + '" class="st_common st_option"><input type="checkbox" id="'+e[0]+'" class="st_common st_checkbox" title="'+e[3]+'"' + checked + '/>' +
'<label for="' + e[0] + '" class="st_label">' + e[2] + '</span></div>';
}
}
return html + '</li>';
}
function display() {
place('s_toolbar', toolbarHTML, {
position: 'fixed',
'top': 0,
margin: 0,
minHeight: '30px',
width: '100%',
zIndex: 2999999999,
paddingTop: '3px',
paddingBottom: '3px',
backgroundColor: 'white',
borderBottom: '1px solid orange',
display:'none'
});
$('.st_li').css({
background: 'none',
border: 0,
display:'inline',
padding: 0,
});
$('.st_space').css({
marginLeft:'20px',
});
$('.st_common').css({
border: 0,
margin: 0,
padding: 0,
height: '30px',
fontSize: '15px',
verticalAlign:'middle',
});
$('.st_link').css({
textDecoration: 'none',
fontWeight: 'bolder',
marginLeft:'5px',
padding:'5px',
cursor: 'pointer',
backgroundColor: '#eeeeee',
color: 'black',
});
$('.st_label').css({
marginLeft: '5px',
display: 'inline',
});
$('.st_option').css({
display: 'inline-block'
});
$('.st_link').hover(function() {
$(this).css({color:'orange'});
}, function() {
$(this).css({color:'black'});
});
$('.st_checkbox').css({
marginLeft:'10px',
});
place('icon', iconHTML, {
cursor:'pointer',
'float':'right',
padding: '0px 15px 18px',
fontWeight:'bold',
backgroundColor: 'transparent',
color:'red',
position:'fixed',
right:0,
bottom: 0,
height:'10px',
width:'10px',
zIndex:9999
});
icon = $('#icon').get(0);
$('#icon').on('click', toggle);
$('#l_dict').on('click', function(e) {
e.preventDefault();
e.stopPropagation();
var x = $(this).attr('id');
show_dict(x);
});
$('#o_mini').on('change', function(e) {
GM_setValue('mini', $(this).prop('checked'));
});
$('#o_auto').on('change', function(e) {
GM_setValue('auto', $(this).prop('checked'));
});
if (GM_getValue('status', 0))
show();
}
function place(id, html, css) {
$('body').prepend(html);
$('#'+id).css(css);
}
function toggle() {
var v = $('#s_toolbar').css('display');
if (v == 'none')
show();
else
hide();
}
function show() {
$('#s_toolbar').css({
'display':'block',
});
$('body').css('marginTop', '50px');
GM_setValue('status', 1);
}
function hide() {
$('#s_toolbar').css({
'display':'none',
});
$('body').css('marginTop', 0);
GM_setValue('status', 0);
}
function init() {
document.addEventListener('mouseup', function(e) {
var node = (e.target || e.srcElement);
if (e.button != 0 || (node.nodeName == 'A' && !allowAnchor)
|| node.nodeName == 'INPUT') {
return;
}
var n = node;
while (n) {
if (n == icon) {
return;
}
if (n.getAttribute) {
var ce = n.getAttribute('contenteditable');
if (ce) {
return;
}
}
n = n.parentNode;
}
if (++numClicks == 1) {
window.setTimeout(function() {
dictionaryLookup();
numClicks = 0;
}, 300);
}
}, false);
}
function dictionaryLookup() {
selectedText = getSelectedText(true);
if (selectedText != null && selectedText.length > 0) {
if (selectedText.indexOf(' ') != -1) {
selectedText = null;
return;
}
if ($('#o_auto').prop('checked')) {
showDict(selectedText);
}
} else {
hideDict();
}
}
function getSelectedText(trim) {
var text =
(window.getSelection) ? window.getSelection().toString() :
(document.getSelection) ? document.getSelection().toString() :
(document.selection) ? document.selection.createRange().text : null;
if (trim && text != null)
text = text.trim();
return text;
}
function showDict(text) {
var vwidth;
hideDict();
if ($('#o_mini').prop('checked')) {
vwidth = 250;
var url = 'http://m.spokensanskrit.de/index.php?tinput=';
vdiv = window.open(url + text + '&trans=Translate', 'stdict',
',left=' + (screen.availWidth-vwidth-5) +
',width=' + vwidth +
',top=' + 0 +
',height=' + screen.height +
',location=0,menubar=0,status=0,scrollbars=1,toolbar=0,dependent=1'
);
} else {
var a = $('#a_dict');
a.on('click', function(e) {
a.attr('href',
'http://spokensanskrit.de/index.php?trans=Translate&tinput='+text);
});
a.get(0).click();
}
}
function hideDict() {
if (vdiv) {
vdiv.close();
vdiv = null;
}
}
function show_dict(id) {
showDict(selectedText ? selectedText : '');
}
function _debug(s) {
if (DEBUG)
console.log(s);
}
main();
})();