lazyFingers

Tính năng: hiện 1 số button khi bôi đen text

  1. // ==UserScript==
  2. // @name lazyFingers
  3. // @version 2019.11.24.02
  4. // @description Tính năng: hiện 1 số button khi bôi đen text
  5. // @namespace idmresettrial
  6. // @author idmresettrial
  7. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js
  8. // @include *
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_setClipboard
  12. // @run-at document-start
  13. // ==/UserScript==
  14. this.$ = this.jQuery = jQuery.noConflict(true);
  15.  
  16. // Do not run on frames or iframes
  17. if (window.top !== window.self) {
  18. return;
  19. }
  20. document.addEventListener('DOMContentLoaded', function () {
  21. $('body').append('<div id="lazyFingers"></div>');
  22. $('head').append('<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">');
  23. $('head').append(
  24. '<style>#lazyFingers{line-height:20px;z-index:999999;cursor:pointer;display:none;position:fixed;top:0;left:0;padding:0px 3px;border-radius:3px;background:#fdfefe;font-size:13px;color:#000;border:1px solid #ccc}' +
  25. '#lazyFingers .lfbtn{display:table-cell;border-left:1px solid #ccc;border-bottom: 2px solid #00000000; padding:0px 5px;height:20px}#lazyFingers .lfbtn:hover{background:#f1f1f1;border-bottom:2px solid #63b144}' +
  26. '#lazyFingers .lfbtn.first-btn{border-left:none;margin-left:0px}#lazyFingers .lficon{line-height:13px;color:#333;position:relative;top:50%;transform:translateY(-50%)}#lazyFingers .lficon i{font-family:FontAwesome !important}' +
  27. '</style>');
  28. //
  29. var copyMe = '';
  30. var textarea = '';
  31. var body_mouseup = true;
  32. var buttons_selector = [
  33. ];
  34. var lazyFingers_html = '';
  35. var buttons = {
  36. copy: {
  37. css: 'fa fa-files-o',
  38. txt: '',
  39. do : function () {
  40. clearTextSelection();
  41. GM_setValue('copyMe', copyMe);
  42. GM_setClipboard(copyMe);
  43. },
  44. check2show: function (t, e) {
  45. return t.length > 0;
  46. }
  47. },
  48. paste: {
  49. css: 'fa fa-clipboard',
  50. txt: '',
  51. do : function () {
  52. insertAtCursor(textarea, GM_getValue('copyMe', ''));
  53. },
  54. check2show: function (t, e) {
  55. return e;
  56. }
  57. },
  58. openlink: {
  59. css: 'fa fa-link',
  60. txt: '',
  61. do : function () {
  62. var url = copyMe.match(/([a-z]+:\/\/)?(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal))(:[0-9]{1,5})?\S*/gi);
  63. for (let i = 0; i < url.length; i++) {
  64. var http = (url[i].indexOf('http') !== 0) ? 'http://' : '';
  65. window.open(http + url[i]);
  66. }
  67. },
  68. check2show: function (t, e) {
  69. return (/([a-z]+:\/\/)?(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|local|internal))(:[0-9]{1,5})?\S*/gi).test(t);
  70. }
  71. },
  72. searchGG: {
  73. css: 'fa fa-search',
  74. txt: '',
  75. do : function () {
  76. window.open('https://www.google.com/?gws_rd=ssl#q=' + copyMe);
  77. },
  78. check2show: function (t, e) {
  79. return t.length > 0;
  80. }
  81. },
  82. searchYT: {
  83. css: 'fa fa-youtube-play',
  84. txt: '',
  85. do : function () {
  86. window.open('https://www.youtube.com/results?search_query=' + copyMe);
  87. },
  88. check2show: function (t, e) {
  89. return t.length > 0;
  90. }
  91. },
  92. searchZING: {
  93. css: 'fa fa-music',
  94. txt: '',
  95. do : function () {
  96. window.open('http://mp3.zing.vn/tim-kiem/bai-hat.html?q=' + copyMe);
  97. },
  98. check2show: function (t, e) {
  99. return t.length > 0;
  100. }
  101. },
  102. linksvip: {
  103. css: '',
  104. txt: 'Linksvip',
  105. do : function () {
  106. window.open('http://linksvip.net/?link=' + copyMe + '&ref=idmresettrial');
  107. },
  108. check2show: function (t, e) {
  109. return (/fshare.vn\/file/i).test(t);
  110. }
  111. },
  112. divLookup: {
  113. // require: https://bit.ly/2qjWgRb
  114. css: '',
  115. txt: 'Dịch nhanh',
  116. do : function () {
  117. var e = document.createEvent('MouseEvents');
  118. e.initEvent('mouseover', true, true);
  119. $('#divLookup') [0].dispatchEvent(e);
  120. },
  121. init: function() {
  122. muIgnores.push("divDic", "divResult", "gdptrantxt");
  123. $('head').append('<style>#divUse, #divLookup, #divLookup-lf{display:none;}</style>');
  124. $('html').on('mousedown', '#divLookup-btn', function (e) {
  125. e.stopPropagation();
  126. disappear();
  127. buttons.divLookup.do_more();
  128. });
  129. },
  130. check2show: function (t, e) {
  131. let c = t.length && $('#divLookup').length;
  132. if (c) {
  133. if ($('#divLookup-lf').length) {
  134. $('#divLookup-lf').remove();
  135. }
  136. $('#divLookup')[0].setAttribute("id","divLookup-lf");
  137. }
  138. return c;
  139. },
  140. do_more: function () {
  141. body_mouseup = false;
  142. setTimeout(function() { // to prevent #divLookup from being removed
  143. var e = document.createEvent('MouseEvents');
  144. e.initEvent('mouseover', true, true);
  145. $('#divLookup-lf')[0].setAttribute("id","divLookup");
  146. $('#imgLookForw') [0].dispatchEvent(e);
  147. body_mouseup = true;
  148. }, 100);
  149. }
  150. },
  151. };
  152.  
  153. var muIgnores = [];
  154. $('html').on('mouseup', function (e) {
  155. if (body_mouseup) mu(e);
  156. });
  157. $('html').on('mouseup', 'textarea,input', function (e) {
  158. body_mouseup = false;
  159. mu(e, this);
  160. setTimeout(function () {
  161. body_mouseup = true;
  162. }, 500);
  163. });
  164. //
  165. $('html').on('keydown', 'textarea,input', function () {
  166. $('#lazyFingers').hide();
  167. });
  168. for (let i = 0; i < Object.keys(buttons).length; i++) {
  169. var button = buttons[Object.keys(buttons) [i]];
  170. if ("init" in button) {
  171. button.init();
  172. }
  173.  
  174. lazyFingers_html += '<div id="' + Object.keys(buttons) [i] + '-btn" class="lfbtn" ><div class="lficon"><i class="' + buttons[Object.keys(buttons) [i]].css + '"></i><span>' + buttons[Object.keys(buttons) [i]].txt + '</span></div></div>';
  175. buttons_selector[buttons_selector.length] = '#' + Object.keys(buttons) [i] + '-btn';
  176. }
  177. buttons_selector = buttons_selector.join(',');
  178. $('#lazyFingers').append(lazyFingers_html);
  179. $('html').on('mouseup', buttons_selector, function (e) {
  180. var button = e.target.parentElement.parentElement.id.replace('-btn', '');
  181. body_mouseup = false;
  182. buttons[button].do ();
  183. disappear();
  184. setTimeout(function () {
  185. body_mouseup = true;
  186. }, 500);
  187. });
  188.  
  189. function display(e, show_what) {
  190. clearDisplay();
  191.  
  192. show_what.sort(function (a, b) {
  193. return Object.keys(buttons).indexOf(a) - Object.keys(buttons).indexOf(b);
  194. });
  195. $(show_what.map(function (val) {
  196. return '#' + val + '-btn';
  197. }).join(',')).css('display', 'table-cell');
  198. $('.first-btn').removeClass('first-btn');
  199. $('#lazyFingers .lfbtn[style*="display: table-cell"]').first().addClass('first-btn');
  200.  
  201. let w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
  202. let h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
  203. let margin = 5; let tmpLeft = e.clientX, tmpTop = e.clientY;
  204. let lw = $('#lazyFingers').width(), lh = $('#lazyFingers').height();
  205.  
  206. $('#lazyFingers').css({
  207. 'left': tmpLeft + lw + margin < w ? tmpLeft + margin : tmpLeft - lw - margin,
  208. 'top': tmpTop + lh +margin < h ? tmpTop + margin : tmpTop - lh - margin
  209. });
  210. $('#lazyFingers').fadeIn(200);
  211. }
  212. function disappear() {
  213. $('#lazyFingers').hide();
  214. }
  215. function clearDisplay() {
  216. $('#lazyFingers, #lazyFingers .lfbtn').hide();
  217. }
  218.  
  219. function mu(e, el) {
  220. if (muIgnores.includes(e.target.id)) {
  221. return;
  222. }
  223.  
  224. setTimeout(function() {
  225.  
  226. var show_what = [
  227. ];
  228. var txt = '';
  229. var editable = (typeof el !== 'undefined');
  230. var displayBtn = false;
  231. if (editable) {
  232. txt = getSelectedText2(el);
  233. textarea = el;
  234. } else {
  235. txt = getSelectedText1();
  236. }
  237. for (let i = 0; i < Object.keys(buttons).length; i++) {
  238. if (buttons[Object.keys(buttons) [i]].check2show(txt, editable)) {
  239. show_what[show_what.length] = Object.keys(buttons) [i];
  240. displayBtn = true;
  241. }
  242. }
  243. copyMe = txt;
  244. if (displayBtn) {
  245. display(e, show_what);
  246. }
  247. else $('#lazyFingers').hide();
  248.  
  249. }, 100);
  250. }
  251. function getSelectedText1() {
  252. if (window.getSelection) {
  253. return window.getSelection().toString();
  254. } else if (document.selection) {
  255. return document.selection.createRange().text;
  256. }
  257. return '';
  258. }
  259. function getSelectedText2(textComponent)
  260. {
  261. var selectedText;
  262. // IE version
  263. if (document.selection !== undefined)
  264. {
  265. textComponent.focus();
  266. var sel = document.selection.createRange();
  267. selectedText = sel.text;
  268. }
  269. // Mozilla version
  270. else if (textComponent.selectionStart !== undefined)
  271. {
  272. var startPos = textComponent.selectionStart;
  273. var endPos = textComponent.selectionEnd;
  274. selectedText = textComponent.value.substring(startPos, endPos);
  275. }
  276. return selectedText;
  277. }
  278. function clearTextSelection() {
  279. if (window.getSelection) {
  280. if (window.getSelection().empty) { // Chrome
  281. window.getSelection().empty();
  282. } else if (window.getSelection().removeAllRanges) { // Firefox
  283. window.getSelection().removeAllRanges();
  284. }
  285. } else if (document.selection) { // IE?
  286. document.selection.empty();
  287. }
  288. }
  289. function insertAtCursor(myField, myValue) {
  290. //IE support
  291. if (document.selection) {
  292. myField.focus();
  293. var sel = document.selection.createRange();
  294. sel.text = myValue;
  295. }
  296. //MOZILLA and others
  297. else if (myField.selectionStart || myField.selectionStart == '0') {
  298. var startPos = myField.selectionStart;
  299. var endPos = myField.selectionEnd;
  300. myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
  301. } else {
  302. myField.value += myValue;
  303. }
  304. }
  305. });