Sanskrit Tools - Toolbar

Sanskrit Language Tools

目前为 2014-11-15 提交的版本。查看 最新版本

// ==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.2
// ==/UserScript==

(function() {
  const IGNORES = [
    'mail.yahoo.com',
    'groups.yahoo.com',
    'spokensanskrit.de',
  ];
  const ALLOW_ANCHORS = [
    'sanskrit.uohyd.ernet.in/cgi-bin/scl/SHMT/generate.cgi',
  ];
  const GROUPS = [
    {
      type:'link', entries:
        [
          [
            'news', 'वार्ता',
            'Doordarshan Sanskrit News', 'www.youtube.com/user/sanskritanews/videos'
          ],
          [
            'mag', 'पत्रिका',
            'Sambhaashana Sandesha magazine', 'www.sambhashanasandesha.in/'
          ],
          [
            'books', 'पुस्तकानि',
            'Books', 'www.sanskrit.nic.in/ebook.htm'
          ],
          [
             'wiki', 'विकिपीडिय&#2366',
             'Sanskrit Wikipedia', 'sa.wikipedia.org'
          ]
        ]
    },
    {
      type:'link', entries: [
        [
          'dict', 'शब्दकोशः',
          'Dictionary', 'spokensanskrit.de', special
        ],
        [
          'sandhi', 'सन्धिः',
          'Sandhi trainer', 'sanskrit.inria.fr/DICO/sandhi.en.html',
        ],
        [
          'grammar', 'तिङन्त-/सुबन्त-रूपाणि',
          'Grammar lookup', 'sanskrit.inria.fr/DICO/grammar.fr.html'
        ],
        [
          'taddita', 'तद्दितरूपाणि',
          'Noun forms', 'sanskrit.uohyd.ernet.in/scl/skt_gen/waxXiwa/waxXiwa_gen.html'
        ],
        [
          'sutra', 'माहेश्वरसूत्राणि',
          'Maaheshwara sutras', 'en.wikipedia.org/wiki/Siva_Sutra#Text'
        ],
      ]
    },
    {
      type:'option', entries: [
        [ 'o_auto', 'check', 'Auto-dictionary ', 'Double-clicking a word will automatically launch the dictionary', true],
        [ 'o_mini', 'check', 'Mini dictionary ', 'Launch the dictionary in a slim window', true],
      ]
    }
  ];

  var toolbarHTML;
  var iconHTML;
  var icon;
  var cb = {};
  var numClicks = 0;
  var vdiv = null;
  var allowAnchor = false;

  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="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="sbt"></a>'
    ;
    iconHTML =
      '<div id="icon" title="Click to show/hide Sanskrit Toolbar">\u0938' +
      '</div>'
    ;
  }

  function makeGroups() {
    var html = '';
    for (var i in GROUPS) {
      var g = GROUPS[i];
      if (g.type == 'link')
        html += makeLinks(g);
      else if (g.type == 'option')
        html += makeOptions(g);
      else {
        continue;
      }
      html += '<span class="st_space"></span>';
    }
    return html;
  }

  function makeLinks(g) {
    var html = '';
    for (var i in g.entries) {
      var e = g.entries[i];
      if (e[4]) {
        html += '<button id="'+e[0]+'" title="'+e[2]+'" class="st_cb st_button">'+
          e[1]+'</button>';
        cb[e[0]] = e[4];
      } else {
        html += '<a style="color:inherit" href="http://'+e[3]+'" target="sbt">' +
          '<button id="'+e[0]+'" title="'+e[2]+'" class="st_button">'+
          e[1]+'</button></a>';
      }
    }
    return html;
  }

  function makeOptions(g) {
    var html = '';
    for (var i in g.entries) {
      var e = g.entries[i];
      if (e[1] == 'label') {
        html += '<span id="'+e[0]+'" class="st_label">'+e[2]+'</span>';
      } else if (e[1] == 'check') {
        var checked = e[4] ? ' checked="checked"' : ''
        html += '<input type="checkbox" id="'+e[0]+'" class="st_input" title="'+e[3]+'"' + checked + '/>' +
        '<span class="st_label" title="'+ e[3] + '"> ' + e[2] + '</span>';
      } 
    }
    return html;
  }

  function display() {
    place('toolbar', toolbarHTML, {
      position: 'fixed',
      'top': 0,
      margin: 0,
      height: '45px',
      width: '100%',
      zIndex: 2999999999,
      paddingTop: '5px',
      backgroundColor: '#eeeeee',
      borderBottom: '1px solid orange',
      display:'none'
    });
    $('.st_space').css({
      marginLeft:'20px',
      verticalAlign:'middle',
    });
    $('.st_button').css({
      marginLeft:'10px',
      padding:'5px',
      verticalAlign:'middle',
      height:'35px',
      cursor: 'pointer',
    });
    $('.st_label').css({
      verticalAlign:'middle',
      padding:'0px',
      height:'30px',
      verticalAlign:'middle'
    });
    $('.st_input').css({
      marginLeft:'10px',
      verticalAlign:'middle',
      padding:'0px',
      height:'30px',
      verticalAlign:'middle'
    });
    place('icon', iconHTML, {
      cursor:'pointer',
      'float':'right',
      padding: '0px 15px 18px',
      fontWeight:'bold',
      backgroundColor: 'white',
      color:'red',
      position:'fixed',
      right:0,
      bottom: 0,
      height:'10px',
      width:'10px',
      zIndex:9999
    });
    icon = $('#icon').get(0);
    $('#o_mini').prop('checked', GM_getValue('mini', true));
    $('#o_auto').prop('checked', GM_getValue('auto', true));
    $('#icon').on('click', toggle);
    $('.st_cb').on('click', function(e) {
      e.preventDefault();
      e.stopPropagation();
      var x = $(this).attr('id');
      special(x);
    });
    $('.st_option').on('change', function(e) {
      var x = $(this).attr('id');
      options[x] = $(this).val();
    });
    $('#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 s = GM_getValue('status', 0);
    if (s == 0)
      show();
    else
      hide();
  }

  function show() {
    $('#toolbar').css({
      'display':'block',
    });
    $('body').css('marginTop', '50px');
    GM_setValue('status', 1);
  }

  function hide() {
    $('#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 == 'BUTTON')
        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() {
    var selectedText = getSelectedText(true);
    if (selectedText != null && selectedText.length > 0) {
      if ($('#o_auto').prop('checked')) {
        if (selectedText.indexOf(' ') != -1)
          return;
        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;
    if (vdiv) vdiv.close();
    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.width-vwidth) +
        ',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('');
  }

  main();
})();