Sanskrit Tools - Toolbar

Sanskrit Language Tools

目前為 2014-11-25 提交的版本,檢視 最新版本

  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.3
  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 is one of link, option.
  23. type:'link', entries:
  24. [
  25. // Each link entry is of the form:
  26. // id (in case we want to refer to the element), label
  27. // tooltip, url, (optional) impl function. If an impl function is
  28. // not provided, we simply go to the url.
  29. [
  30. 'l_news', 'वार्ताः',
  31. 'Doordarshan Sanskrit News', 'www.youtube.com/user/sanskritanews/videos'
  32. ],
  33. [
  34. 'l_mag', 'पत्रिका',
  35. 'Sambhaashana Sandesha magazine', 'www.sambhashanasandesha.in/'
  36. ],
  37. [
  38. 'l_books', 'पुस्तकानि',
  39. 'Books', 'www.sanskrit.nic.in/ebook.htm'
  40. ],
  41. [
  42. 'l_search', 'गवेषिका',
  43. 'Text Search Engine', 'sanskrit.uohyd.ernet.in:8080/searchengine/index1.jsp',
  44. ],
  45. [
  46. 'l_wiki', 'विकिपीडिय&#2366',
  47. 'Wikipedia', 'sa.wikipedia.org'
  48. ],
  49. [
  50. 'l_msutra', 'माहेश्वरसूत्राणि',
  51. 'Maheshwara Sutras', 'en.wikipedia.org/wiki/Siva_Sutra#Text'
  52. ],
  53. ]
  54. },
  55. {
  56. type:'link', entries: [
  57. [
  58. 'l_dict', 'शब्दकोशः',
  59. 'Dictionary', 'spokensanskrit.de', special
  60. ],
  61. /*
  62. [
  63. 'l_grammar', 'तिङन्त-/सुबन्त-रूपाणि',
  64. 'Inflections', 'sanskrit.inria.fr/DICO/grammar.fr.html'
  65. ],
  66. */
  67. [
  68. 'l_thes', ' अमरकोशः',
  69. 'Thesaurus', 'sanskrit.uohyd.ernet.in/scl/amarakosha/frame.html',
  70. ],
  71. [
  72. 'l_sandhi', 'सन्धिः',
  73. 'Split words', 'tdil-dc.in/san/sandhi_splitter/index_dit.html',
  74. ],
  75. ]
  76. },
  77. {
  78. type:'option', entries: [
  79. // Each option entry is of the form:
  80. // id (in case we want to refer to the element),
  81. // type (currently: label or check), label, tooltip, and (if
  82. // type = check) whether checked or not.
  83. [ 'o_auto', 'check', 'Auto-dictionary ', 'Double-clicking a word will automatically launch the dictionary', true],
  84. [ 'o_mini', 'check', 'Mini dictionary ', 'Show the dictionary in a slim window', true],
  85. [ 'o_win', 'check', 'Reuse window ', 'Show all tools in one window', true],
  86. ]
  87. }
  88. ];
  89.  
  90. var toolbarHTML;
  91. var iconHTML;
  92. var icon;
  93. var cb = {};
  94. var numClicks = 0;
  95. var vdiv = null;
  96. var allowAnchor = false;
  97. var selectedText = null;
  98.  
  99. function main() {
  100. for (var i in IGNORES) {
  101. if (document.URL.indexOf(IGNORES[i]) != -1) {
  102. return;
  103. }
  104. }
  105. for (var i in ALLOW_ANCHORS) {
  106. if (document.URL.indexOf(ALLOW_ANCHORS[i]) != -1) {
  107. allowAnchor = true;
  108. break;
  109. }
  110. }
  111. init();
  112. if (window.top != window.self) return;
  113. make();
  114. display();
  115. }
  116.  
  117. function make() {
  118. var elements = makeGroups();
  119. toolbarHTML =
  120. '<div id="s_toolbar" style="overflow:hidden; float:left">' +
  121. '<div style="float:left; display:inline-block">' +
  122. elements +
  123. '</div>' +
  124. '</div>' +
  125. '<a id="a_dict" style="display:none" href="" target="l_dict"></a>'
  126. ;
  127. iconHTML =
  128. '<div id="icon" title="Click to show/hide Sanskrit Toolbar">\u0938' +
  129. '</div>'
  130. ;
  131. }
  132.  
  133. function makeGroups() {
  134. var html = '<ul style="list-style:none;margin:0;padding:0">';
  135. for (var i in GROUPS) {
  136. var g = GROUPS[i];
  137. if (g.type == 'link')
  138. html += makeLinks(g);
  139. else if (g.type == 'option')
  140. html += makeOptions(g);
  141. else {
  142. continue;
  143. }
  144. html += '<li class="st_li st_space"/>';
  145. }
  146. return html + '</ul>';
  147. }
  148.  
  149. function makeLinks(g) {
  150. var html = '';
  151. for (var i in g.entries) {
  152. var e = g.entries[i];
  153. html += '<li class="st_li">';
  154. if (e[4]) {
  155. html += '<a id="'+e[0]+'" title="'+e[2]+'" class="st_cb st_common st_link" target="l_dict">' +
  156. e[1]+'</a>';
  157. cb[e[0]] = e[4];
  158. } else {
  159. html += '<a id="'+e[0]+'" title="'+e[2]+'" class="st_common st_link" href="http://'+e[3]+'" target="l_dict">' +
  160. e[1]+'</a>';
  161. }
  162. html += '</li>';
  163. }
  164. return html;
  165. }
  166.  
  167. function makeOptions(g) {
  168. var html = '<li class="st_li">';
  169. for (var i in g.entries) {
  170. var e = g.entries[i];
  171. if (e[1] == 'label') {
  172. html += '<span id="'+e[0]+'" class="st_common st_label">'+e[2]+'</span>';
  173. } else if (e[1] == 'check') {
  174. var checked = GM_getValue(e[0], e[4]) ? ' checked="checked"' : '';
  175. 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 + '/>' +
  176. '<span class="st_label">' + e[2] + '</span></div>';
  177. }
  178. }
  179. return html + '</li>';
  180. }
  181.  
  182. function display() {
  183. place('s_toolbar', toolbarHTML, {
  184. position: 'fixed',
  185. 'top': 0,
  186. margin: 0,
  187. minHeight: '30px',
  188. width: '100%',
  189. zIndex: 2999999999,
  190. paddingTop: '3px',
  191. paddingBottom: '3px',
  192. backgroundColor: 'white',
  193. borderBottom: '1px solid orange',
  194. display:'none'
  195. });
  196. $('.st_li').css({
  197. background: 'none',
  198. border: 0,
  199. display:'inline',
  200. padding: 0,
  201. });
  202. $('.st_space').css({
  203. marginLeft:'20px',
  204. });
  205. $('.st_common').css({
  206. border: 0,
  207. margin: 0,
  208. padding: 0,
  209. height: '30px',
  210. fontSize: '15px',
  211. verticalAlign:'middle',
  212. });
  213. $('.st_link').css({
  214. textDecoration: 'none',
  215. fontWeight: 'bolder',
  216. marginLeft:'5px',
  217. padding:'5px',
  218. cursor: 'pointer',
  219. backgroundColor: '#eeeeee',
  220. color: 'black',
  221. });
  222. $('.st_label').css({
  223. marginLeft: '5px',
  224. });
  225. $('.st_option').css({
  226. display: 'inline-block'
  227. });
  228. $('.st_link').hover(function() {
  229. $(this).css({color:'orange'});
  230. }, function() {
  231. $(this).css({color:'black'});
  232. });
  233. $('.st_checkbox').css({
  234. marginLeft:'10px',
  235. });
  236. place('icon', iconHTML, {
  237. cursor:'pointer',
  238. 'float':'right',
  239. padding: '0px 15px 18px',
  240. fontWeight:'bold',
  241. backgroundColor: 'transparent',
  242. color:'red',
  243. position:'fixed',
  244. right:0,
  245. bottom: 0,
  246. height:'10px',
  247. width:'10px',
  248. zIndex:9999
  249. });
  250. icon = $('#icon').get(0);
  251. $('#icon').on('click', toggle);
  252. $('.st_cb').on('click', function(e) {
  253. e.preventDefault();
  254. e.stopPropagation();
  255. var x = $(this).attr('id');
  256. special(x);
  257. });
  258. $('#o_mini').on('change', function(e) {
  259. GM_setValue('mini', $(this).prop('checked'));
  260. });
  261. $('#o_auto').on('change', function(e) {
  262. GM_setValue('auto', $(this).prop('checked'));
  263. });
  264. $('#o_win').on('change', function(e) {
  265. var v = $(this).prop('checked');
  266. GM_setValue('win', v);
  267. $('a.st_link').each(function(k,val) {
  268. if (v) {
  269. $(this).attr('target', 'l_dict');
  270. } else {
  271. $(this).attr('target', $(this).attr('id'));
  272. }
  273. });
  274. });
  275. if (GM_getValue('status', 0))
  276. show();
  277. }
  278.  
  279. function place(id, html, css) {
  280. $('body').prepend(html);
  281. $('#'+id).css(css);
  282. }
  283.  
  284. function toggle() {
  285. var s = GM_getValue('status', 0);
  286. if (s == 0)
  287. show();
  288. else
  289. hide();
  290. }
  291.  
  292. function show() {
  293. $('#s_toolbar').css({
  294. 'display':'block',
  295. });
  296. $('body').css('marginTop', '50px');
  297. GM_setValue('status', 1);
  298. }
  299.  
  300. function hide() {
  301. $('#s_toolbar').css({
  302. 'display':'none',
  303. });
  304. $('body').css('marginTop', 0);
  305. GM_setValue('status', 0);
  306. }
  307.  
  308. function init() {
  309. document.addEventListener('mouseup', function(e) {
  310. var node = (e.target || e.srcElement);
  311. if (e.button != 0 || (node.nodeName == 'A' && !allowAnchor)
  312. || node.nodeName == 'INPUT') {
  313. return;
  314. }
  315. var n = node;
  316. while (n) {
  317. if (n == icon) {
  318. return;
  319. }
  320. if (n.getAttribute) {
  321. var ce = n.getAttribute('contenteditable');
  322. if (ce) {
  323. return;
  324. }
  325. }
  326. n = n.parentNode;
  327. }
  328. if (++numClicks == 1) {
  329. window.setTimeout(function() {
  330. dictionaryLookup();
  331. numClicks = 0;
  332. }, 300);
  333. }
  334. }, false);
  335. }
  336.  
  337. function dictionaryLookup() {
  338. selectedText = getSelectedText(true);
  339. if (selectedText != null && selectedText.length > 0) {
  340. if (selectedText.indexOf(' ') != -1) {
  341. selectedText = null;
  342. return;
  343. }
  344. if ($('#o_auto').prop('checked')) {
  345. showDict(selectedText);
  346. }
  347. } else {
  348. hideDict();
  349. }
  350. }
  351.  
  352. function getSelectedText(trim) {
  353. var text =
  354. (window.getSelection) ? window.getSelection().toString() :
  355. (document.getSelection) ? document.getSelection().toString() :
  356. (document.selection) ? document.selection.createRange().text : null;
  357. if (trim && text != null)
  358. text = text.trim();
  359. return text;
  360. }
  361.  
  362. function showDict(text) {
  363. var vwidth;
  364. hideDict();
  365. if ($('#o_mini').prop('checked')) {
  366. vwidth = 250;
  367. var url = 'http://m.spokensanskrit.de/index.php?tinput=';
  368. vdiv = window.open(url + text + '&trans=Translate', 'stdict',
  369. ',left=' + (screen.availWidth-vwidth-65) +
  370. ',width=' + vwidth +
  371. ',top=' + 0 +
  372. ',height=' + screen.height +
  373. ',location=0,menubar=0,status=0,scrollbars=1,toolbar=0,dependent=1'
  374. );
  375. } else {
  376. var a = $('#a_dict');
  377. a.on('click', function(e) {
  378. a.attr('href',
  379. 'http://spokensanskrit.de/index.php?trans=Translate&tinput='+text);
  380. });
  381. a.get(0).click();
  382. }
  383. }
  384. function hideDict() {
  385. if (vdiv) {
  386. vdiv.close();
  387. vdiv = null;
  388. }
  389. }
  390.  
  391. function special(id) {
  392. showDict(selectedText ? selectedText : '');
  393. }
  394.  
  395. main();
  396. })();