Greasy Fork is available in English.

Sanskrit Tools - Toolbar

Sanskrit Language Tools

Ekde 2014/12/08. Vidu La ĝisdata versio.

// ==UserScript==
// @name           Sanskrit Tools - Toolbar
// @namespace      stgeorge
// @description    Sanskrit Language Tools
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @grant          GM_setValue
// @grant          GM_getValue
// @version        2.4.6
// ==/UserScript==

(function() {
  var IGNORES = [
    'mail.yahoo.com',
    'groups.yahoo.com',
    'm.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_mag', 'पत्रिका',
            'Sambhaashana Sandesha magazine', 'www.sambhashanasandesha.in/'
          ],
          [
            'l_books', 'पुस्तकानि',
            'Books', 'www.sanskrit.nic.in/ebook.htm'
          ],
          [
            'l_search', 'गवेषिका',
            'Text Search Engine', 'sanskrit.uohyd.ernet.in:8080/searchengine/index1.jsp',
          ],
          [
             'l_wiki', 'विकिपीडिय&#2366',
             'Wikipedia', 'sa.wikipedia.org'
          ],
          [
            'l_msutra', 'माहेश्वरसूत्राणि',
            'Maheshwara Sutras', 'en.wikipedia.org/wiki/Siva_Sutra#Text'
          ]
        ]
    },
    {
      type:'link', entries: [
        [
          'l_dict', 'शब्दकोशः',
          'Dictionary', 'spokensanskrit.de', special
        ],
        /*
        [
          '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',
        ]
      ]
    },
    {
      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],
        [ 'o_win', 'check', 'Reuse window ', 'Show all tools in one 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;
      console.log('Adding group type ' + gt);
      if (gt == 'link')
        html += makeLinks(g);
      else if (gt == 'option')
        html += makeOptions(g);
      else {
        continue;
      }
      console.log('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;
      console.log('  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="l_dict">' +
          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="l_dict">' +
          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;
      console.log('  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 + '/>' +
        '<span 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',
    });
    $('.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);
    $('.st_cb').on('click', function(e) {
      e.preventDefault();
      e.stopPropagation();
      var x = $(this).attr('id');
      special(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'));
    });
    $('#o_win').on('change', function(e) {
      var v = $(this).prop('checked');
      GM_setValue('win', v);
      $('a.st_link').each(function(k,val) {
        if (v) {
          $(this).attr('target', 'l_dict');
        } else {
          $(this).attr('target', $(this).attr('id'));
        }
      });
    });
    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-65) +
        ',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 special(id) {
    showDict(selectedText ? selectedText : '');
  }

  main();
})();