继续操作前请注册或者登录。

Sanskrit Tools - Toolbar

Sanskrit Language Tools

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

  1. // ==UserScript==
  2. // @name Sanskrit Tools - Toolbar
  3. // @namespace stgeorge
  4. // @description Sanskrit Language Tools
  5. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  6. // @grant GM_setValue
  7. // @grant GM_getValue
  8. // @version 2.4.2
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. const IGNORES = [
  13. 'mail.yahoo.com',
  14. 'groups.yahoo.com',
  15. 'spokensanskrit.de',
  16. ];
  17. const ALLOW_ANCHORS = [
  18. 'sanskrit.uohyd.ernet.in/cgi-bin/scl/SHMT/generate.cgi',
  19. ];
  20. const GROUPS = [
  21. {
  22. type:'link', entries:
  23. [
  24. [
  25. 'news', 'वार्ता',
  26. 'Doordarshan Sanskrit News', 'www.youtube.com/user/sanskritanews/videos'
  27. ],
  28. [
  29. 'mag', 'पत्रिका',
  30. 'Sambhaashana Sandesha magazine', 'www.sambhashanasandesha.in/'
  31. ],
  32. [
  33. 'books', 'पुस्तकानि',
  34. 'Books', 'www.sanskrit.nic.in/ebook.htm'
  35. ],
  36. [
  37. 'wiki', 'विकिपीडिय&#2366',
  38. 'Sanskrit Wikipedia', 'sa.wikipedia.org'
  39. ]
  40. ]
  41. },
  42. {
  43. type:'link', entries: [
  44. [
  45. 'dict', 'शब्दकोशः',
  46. 'Dictionary', 'spokensanskrit.de', special
  47. ],
  48. [
  49. 'sandhi', 'सन्धिः',
  50. 'Sandhi trainer', 'sanskrit.inria.fr/DICO/sandhi.en.html',
  51. ],
  52. [
  53. 'grammar', 'तिङन्त-/सुबन्त-रूपाणि',
  54. 'Grammar lookup', 'sanskrit.inria.fr/DICO/grammar.fr.html'
  55. ],
  56. [
  57. 'taddita', 'तद्दितरूपाणि',
  58. 'Noun forms', 'sanskrit.uohyd.ernet.in/scl/skt_gen/waxXiwa/waxXiwa_gen.html'
  59. ],
  60. [
  61. 'sutra', 'माहेश्वरसूत्राणि',
  62. 'Maaheshwara sutras', 'en.wikipedia.org/wiki/Siva_Sutra#Text'
  63. ],
  64. ]
  65. },
  66. {
  67. type:'option', entries: [
  68. [ 'o_auto', 'check', 'Auto-dictionary ', 'Double-clicking a word will automatically launch the dictionary', true],
  69. [ 'o_mini', 'check', 'Mini dictionary ', 'Launch the dictionary in a slim window', true],
  70. ]
  71. }
  72. ];
  73.  
  74. var toolbarHTML;
  75. var iconHTML;
  76. var icon;
  77. var cb = {};
  78. var numClicks = 0;
  79. var vdiv = null;
  80. var allowAnchor = false;
  81.  
  82. function main() {
  83. for (var i in IGNORES) {
  84. if (document.URL.indexOf(IGNORES[i]) != -1) {
  85. return;
  86. }
  87. }
  88. for (var i in ALLOW_ANCHORS) {
  89. if (document.URL.indexOf(ALLOW_ANCHORS[i]) != -1) {
  90. allowAnchor = true;
  91. break;
  92. }
  93. }
  94. init();
  95. if (window.top != window.self) return;
  96. make();
  97. display();
  98. }
  99.  
  100. function make() {
  101. var elements = makeGroups();
  102. toolbarHTML =
  103. '<div id="toolbar" style="overflow:hidden; float:left">' +
  104. '<div style="float:left; display:inline-block">' +
  105. elements +
  106. '</div>' +
  107. '</div>' +
  108. '<a id="a_dict" style="display:none" href="" target="sbt"></a>'
  109. ;
  110. iconHTML =
  111. '<div id="icon" title="Click to show/hide Sanskrit Toolbar">\u0938' +
  112. '</div>'
  113. ;
  114. }
  115.  
  116. function makeGroups() {
  117. var html = '';
  118. for (var i in GROUPS) {
  119. var g = GROUPS[i];
  120. if (g.type == 'link')
  121. html += makeLinks(g);
  122. else if (g.type == 'option')
  123. html += makeOptions(g);
  124. else {
  125. continue;
  126. }
  127. html += '<span class="st_space"></span>';
  128. }
  129. return html;
  130. }
  131.  
  132. function makeLinks(g) {
  133. var html = '';
  134. for (var i in g.entries) {
  135. var e = g.entries[i];
  136. if (e[4]) {
  137. html += '<button id="'+e[0]+'" title="'+e[2]+'" class="st_cb st_button">'+
  138. e[1]+'</button>';
  139. cb[e[0]] = e[4];
  140. } else {
  141. html += '<a style="color:inherit" href="http://'+e[3]+'" target="sbt">' +
  142. '<button id="'+e[0]+'" title="'+e[2]+'" class="st_button">'+
  143. e[1]+'</button></a>';
  144. }
  145. }
  146. return html;
  147. }
  148.  
  149. function makeOptions(g) {
  150. var html = '';
  151. for (var i in g.entries) {
  152. var e = g.entries[i];
  153. if (e[1] == 'label') {
  154. html += '<span id="'+e[0]+'" class="st_label">'+e[2]+'</span>';
  155. } else if (e[1] == 'check') {
  156. var checked = e[4] ? ' checked="checked"' : ''
  157. html += '<input type="checkbox" id="'+e[0]+'" class="st_input" title="'+e[3]+'"' + checked + '/>' +
  158. '<span class="st_label" title="'+ e[3] + '"> ' + e[2] + '</span>';
  159. }
  160. }
  161. return html;
  162. }
  163.  
  164. function display() {
  165. place('toolbar', toolbarHTML, {
  166. position: 'fixed',
  167. 'top': 0,
  168. margin: 0,
  169. height: '45px',
  170. width: '100%',
  171. zIndex: 2999999999,
  172. paddingTop: '5px',
  173. backgroundColor: '#eeeeee',
  174. borderBottom: '1px solid orange',
  175. display:'none'
  176. });
  177. $('.st_space').css({
  178. marginLeft:'20px',
  179. verticalAlign:'middle',
  180. });
  181. $('.st_button').css({
  182. marginLeft:'10px',
  183. padding:'5px',
  184. verticalAlign:'middle',
  185. height:'35px',
  186. cursor: 'pointer',
  187. });
  188. $('.st_label').css({
  189. verticalAlign:'middle',
  190. padding:'0px',
  191. height:'30px',
  192. verticalAlign:'middle'
  193. });
  194. $('.st_input').css({
  195. marginLeft:'10px',
  196. verticalAlign:'middle',
  197. padding:'0px',
  198. height:'30px',
  199. verticalAlign:'middle'
  200. });
  201. place('icon', iconHTML, {
  202. cursor:'pointer',
  203. 'float':'right',
  204. padding: '0px 15px 18px',
  205. fontWeight:'bold',
  206. backgroundColor: 'white',
  207. color:'red',
  208. position:'fixed',
  209. right:0,
  210. bottom: 0,
  211. height:'10px',
  212. width:'10px',
  213. zIndex:9999
  214. });
  215. icon = $('#icon').get(0);
  216. $('#o_mini').prop('checked', GM_getValue('mini', true));
  217. $('#o_auto').prop('checked', GM_getValue('auto', true));
  218. $('#icon').on('click', toggle);
  219. $('.st_cb').on('click', function(e) {
  220. e.preventDefault();
  221. e.stopPropagation();
  222. var x = $(this).attr('id');
  223. special(x);
  224. });
  225. $('.st_option').on('change', function(e) {
  226. var x = $(this).attr('id');
  227. options[x] = $(this).val();
  228. });
  229. $('#o_mini').on('change', function(e) {
  230. GM_setValue('mini', $(this).prop('checked'));
  231. });
  232. $('#o_auto').on('change', function(e) {
  233. GM_setValue('auto', $(this).prop('checked'));
  234. });
  235. if (GM_getValue('status', 0))
  236. show();
  237. }
  238.  
  239. function place(id, html, css) {
  240. $('body').prepend(html);
  241. $('#'+id).css(css);
  242. }
  243.  
  244. function toggle() {
  245. var s = GM_getValue('status', 0);
  246. if (s == 0)
  247. show();
  248. else
  249. hide();
  250. }
  251.  
  252. function show() {
  253. $('#toolbar').css({
  254. 'display':'block',
  255. });
  256. $('body').css('marginTop', '50px');
  257. GM_setValue('status', 1);
  258. }
  259.  
  260. function hide() {
  261. $('#toolbar').css({
  262. 'display':'none',
  263. });
  264. $('body').css('marginTop', 0);
  265. GM_setValue('status', 0);
  266. }
  267.  
  268. function init() {
  269. document.addEventListener('mouseup', function(e) {
  270. var node = (e.target || e.srcElement);
  271. if (e.button != 0 || (node.nodeName == 'A' && !allowAnchor)
  272. || node.nodeName == 'BUTTON')
  273. return;
  274. var n = node;
  275. while (n) {
  276. if (n == icon)
  277. return;
  278. if (n.getAttribute) {
  279. var ce = n.getAttribute('contenteditable');
  280. if (ce)
  281. return;
  282. }
  283. n = n.parentNode;
  284. }
  285. if (++numClicks == 1) {
  286. window.setTimeout(function() {
  287. dictionaryLookup();
  288. numClicks = 0;
  289. }, 300);
  290. }
  291. }, false);
  292. }
  293.  
  294. function dictionaryLookup() {
  295. var selectedText = getSelectedText(true);
  296. if (selectedText != null && selectedText.length > 0) {
  297. if ($('#o_auto').prop('checked')) {
  298. if (selectedText.indexOf(' ') != -1)
  299. return;
  300. showDict(selectedText);
  301. }
  302. } else {
  303. hideDict();
  304. }
  305. }
  306.  
  307. function getSelectedText(trim) {
  308. var text =
  309. (window.getSelection) ? window.getSelection().toString() :
  310. (document.getSelection) ? document.getSelection().toString() :
  311. (document.selection) ? document.selection.createRange().text : null;
  312. if (trim && text != null)
  313. text = text.trim();
  314. return text;
  315. }
  316.  
  317. function showDict(text) {
  318. var vwidth;
  319. if (vdiv) vdiv.close();
  320. if ($('#o_mini').prop('checked')) {
  321. vwidth = 250;
  322. var url = 'http://m.spokensanskrit.de/index.php?tinput=';
  323. vdiv = window.open(url + text + '&trans=Translate', 'stdict',
  324. ',left=' + (screen.width-vwidth) +
  325. ',width=' + vwidth +
  326. ',top=' + 0 +
  327. ',height=' + screen.height +
  328. ',location=0,menubar=0,status=0,scrollbars=1,toolbar=0,dependent=1'
  329. );
  330. } else {
  331. var a = $('#a_dict');
  332. a.on('click', function(e) {
  333. a.attr('href',
  334. 'http://spokensanskrit.de/index.php?trans=Translate&tinput='+text);
  335. });
  336. a.get(0).click();
  337. }
  338. }
  339. function hideDict() {
  340. if (vdiv) {
  341. vdiv.close();
  342. vdiv = null;
  343. }
  344. }
  345.  
  346. function special(id) {
  347. showDict('');
  348. }
  349.  
  350. main();
  351. })();