Greasy Fork is available in English.

custom Script

快捷键执行自定义脚本

  1. // ==UserScript==
  2. // @name custom Script
  3. // @namespace https://greatest.deepsurf.us/zh-CN/scripts/418877-custom-script
  4. // @version 0.1
  5. // @description 快捷键执行自定义脚本
  6. // @author neysummer2000
  7. // @match *://*/*
  8. // @grant GM_addStyle
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. GM_addStyle(`
  13. #_rightMenu {
  14. z-index: 999999;
  15. background-color: rgba(0, 0, 0, 0.8);
  16. padding: 20px 20px 20px 0px;
  17. display: none;
  18. min-width: 100px;
  19. min-height: 100px;
  20. position: absolute;
  21. border: 1px solid black;
  22. }
  23.  
  24. #_rightMenu a {
  25. color: white;
  26. }
  27.  
  28. #_rightMenu li {
  29. border-bottom: 1px solid black;
  30. cursor: pointer;
  31. }
  32.  
  33. #_rightMenu li:hover {
  34. background-color: rgba(0, 0, 0, 0.4);
  35. }
  36. `);
  37.  
  38. var container = document.createElement("div");
  39. container.id = '_rightMenu';
  40. container.style.display = 'none';
  41. container.appendChild(document.createElement("ul"));
  42. document.body.appendChild(container);
  43.  
  44. var g_menu = document.getElementById('_rightMenu');
  45. var g_scripts = [
  46. {
  47. name: 'open as popup',
  48. script: `
  49. var select = getSelectHtml();
  50. var s = getString(select, 'href="', '"');
  51. if(s == '') s = getString(select, 'src="', '"');
  52. if(s == '') s = select;
  53. if(s.substr(0, 4) == 'http'){
  54. window.open(s, new Date().getTime().toString(), 'height=1080, width=1920, top=0,left=0, toolbar=no, menubar=no, scrollbars=no, resizable=yes,location=no,status=no');
  55. }
  56. `
  57. },
  58. ];
  59. var a, li;
  60. var g_b_show = false;
  61. for(var script of g_scripts){
  62. li = document.createElement('li');
  63.  
  64. a = document.createElement('a');
  65. a.href = 'javascript: void(0);';
  66. a.innerText = script.name;
  67. a.onclick = function(){
  68. eval(script.script);
  69. hideMenu();
  70. }
  71.  
  72. li.appendChild(a);
  73. _rightMenu.children[0].appendChild(li);
  74. }
  75.  
  76. var movex; var movey;
  77. document.addEventListener('mousemove', function(e){
  78. e=e || window.event;
  79. if(e.pageX || e.pageY)
  80. {
  81. movex=e.pageX;
  82. movey=e.pageY;
  83.  
  84. if(g_b_show){
  85. var x = g_menu.offsetLeft;
  86. var y = g_menu.offsetTop;
  87. if(movex < x || movex > x + g_menu.offsetWidth || movey < y || movey > y + g_menu.offsetHeight){
  88. hideMenu();
  89. }
  90. }
  91. }
  92. });
  93.  
  94. window.addEventListener('keydown', function(e){
  95. if(e.altKey && e.code == 'KeyM'){
  96. showMenu(movex, movey);
  97. }
  98. });
  99.  
  100. g_menu.onblur = function(e){
  101. hideMenu();
  102. }
  103. function showMenu(x, y){
  104. g_menu.style.left = x+'px';
  105. g_menu.style.top = y+'px';
  106. g_menu.style.display = 'unset';
  107. g_b_show = true;
  108. }
  109.  
  110. function hideMenu(){
  111. g_menu.style.display = 'none';
  112. g_b_show = false;
  113. }
  114.  
  115. function getSelectHtml(){
  116. if(window.getSelection){
  117. documentFragment = window.getSelection().getRangeAt(0).cloneContents();
  118. }else if(document.selection){
  119. documentFragment = document.selection.createRange().HtmlText;
  120. }
  121. var selectedHtml = '';
  122. for(var i=0;i<documentFragment.childNodes.length;i++){
  123. var childNode = documentFragment.childNodes[i];
  124. if(childNode.nodeType==3){ // Text 节点
  125. selectedHtml+=childNode.nodeValue;
  126. }else{
  127. var nodeHtml = childNode.outerHTML;
  128. selectedHtml+=nodeHtml;
  129. }
  130. }
  131. return selectedHtml;
  132. }
  133.  
  134. function getString(str, s, e){
  135. var i_start = str.indexOf(s);
  136. if(i_start != -1){
  137. i_start += s.length;
  138. var i_end = str.indexOf(e, i_start);
  139. if(i_end != -1){
  140. return str.substr(i_start, i_end - i_start);
  141. }
  142. }
  143. return '';
  144. }
  145.  
  146. })();