Direct Install UserScript via Links

Install UserScript with the Greasy Fork Links using Shift Key

질문, 리뷰하거나, 이 스크립트를 신고하세요.
  1. // ==UserScript==
  2. // @name Direct Install UserScript via Links
  3. // @namespace UserScripts
  4. // @match https://*/*
  5. // @grant none
  6. // @version 1.0.0
  7. // @author CY Fung
  8. // @license MIT
  9. // @description Install UserScript with the Greasy Fork Links using Shift Key
  10. // @allFrames true
  11. // @unwrap
  12. // @run-at document-start
  13. // ==/UserScript==
  14.  
  15. (()=>{
  16.  
  17. const svg100 = (x)=>`data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cpath d='${x}'/%3E%3C/svg%3E`;
  18. const svgFileWhite=svg100(`M60 4a2 2 90 0 0 0 0H14v92h72v-2-64a2 2 90 0 0 0 0v-1h0a2 2 90 0 0-1-1L62 5a2 2 90 0 0-1-1h0-1a2 2 90 0 0 0 0zM18 8h40v24h24v60H18V8zm44 3l17 17H62V11zM52 44l-8 32h4l8-32h-4zm-16 5l-6 11 6 11 4-2-6-9 6-9-4-2zm28 0l-4 2 6 9-6 9 4 2 6-11-6-11z`)
  19. const svgFileBlack =svg100(`M61 4H14v92h72V29L61 4zM36 71l-6-11 6-11 4 2-6 9 6 9-4 2zm12 5h-4l8-32h4l-8 32zm16-5l-4-2 6-9-6-9 4-2 6 11-6 11zm-4-41V9l21 21H60z`)
  20.  
  21. let tmpLinks = [];
  22. let keys = new Set();
  23. let isCssAdded = false;
  24.  
  25. const divMouseEnter =(evt)=>{
  26. const target = (evt||0).target;
  27. if(!(target instanceof HTMLElement)) return;
  28. let clink = target.getAttribute('data-control-link');
  29. let elements = HTMLElement.prototype.querySelectorAll.call(target.parentNode, `a[href="${clink}"]`);
  30. for(const element of elements){
  31. element.classList.add('kds1e-hover');
  32. }
  33. // document.documentElement.setAttribute ('data-kds1e-cl', `${clink}`);
  34. }
  35.  
  36. const divMouseLeave =(evt)=>{
  37. const target = (evt||0).target;
  38. if(!(target instanceof HTMLElement)) return;
  39. let clink = target.getAttribute('data-control-link');
  40. let elements = HTMLElement.prototype.querySelectorAll.call(target.parentNode, `a[href="${clink}"]`);
  41. for(const element of elements){
  42. element.classList.remove('kds1e-hover');
  43. }
  44. // document.documentElement.setAttribute ('data-kds1e-cl', `${clink}`);
  45. }
  46.  
  47. const asyncFuncRun = async (baselink)=>{
  48.  
  49.  
  50.  
  51. const statusForUserScript = await new Promise(resolve=>{
  52. fetch(baselink+'/code/UserScript.user.js').then(e=>{
  53. resolve(e && e.status >= 200 && e.status <400 && e.ok);
  54. }).catch(e=>{
  55. resolve(false);
  56. })
  57. });
  58.  
  59. const statusForUserStyle = statusForUserScript ? false : await new Promise(resolve=>{
  60. fetch(baselink+'/code/UserStyle.user.css').then(e=>{
  61. resolve(e && e.status >= 200 && e.status <400 && e.ok);
  62. }).catch(e=>{
  63. resolve(false);
  64. })
  65. });
  66.  
  67. if(statusForUserScript){
  68. window.open(baselink+'/code/UserScript.user.js');
  69. }else if(statusForUserStyle){
  70. window.open(baselink+'/code/UserStyle.user.css');
  71.  
  72. }
  73.  
  74.  
  75. }
  76.  
  77. const divClick = (evt)=>{
  78. const target = (evt||0).target;
  79. let clink = target.getAttribute('data-control-link');
  80. if(!clink) return;
  81. let m = /https\:\/\/greasyfork\.org\/([-\w]+\/)?scripts\/\d+(\-[-\w]+)?/.exec(clink);
  82. if(m && m[0]){
  83.  
  84. asyncFuncRun(m[0])
  85. // window.open(m[0]+'/code/UserScript.user.js');
  86. }
  87. }
  88.  
  89. function onKeyDown(evt){
  90. if(!evt || !evt.isTrusted) return;
  91. if(!(evt instanceof KeyboardEvent)) return;
  92.  
  93.  
  94. if(!document.querySelector('a[href*="https://greatest.deepsurf.us/"][href*="scripts/"]')) return;
  95.  
  96. keys.add(evt.code);
  97.  
  98. if(evt.key !== 'Shift') return;
  99.  
  100. if(tmpLinks.length>0) return;
  101.  
  102. if(!isCssAdded){
  103. isCssAdded = true;
  104. document.head.appendChild(document.createElement('style')).textContent = `
  105.  
  106. .kds1e-div{
  107. cursor: pointer !important;
  108. display: inline-flex !important;
  109. min-width: calc(1rem + 4px) !important;
  110. min-height: initial !important;
  111. max-width: initial !important;
  112. max-height: initial !important;
  113. margin: 0 !important;
  114. padding: 0 !important;
  115. background-color: white !important;
  116. background-image: url("${svgFileWhite}") !important;
  117. background-repeat: no-repeat !important;
  118. background-size: contain !important;
  119. background-position: center !important;
  120. position: absolute !important;
  121. z-index: 999999 !important;
  122. user-select: none !important;
  123. filter: drop-shadow(1px 1px 3px rgba(0, 0, 0, 0.5)) !important;
  124. }
  125.  
  126. [dark] .kds1e-div{
  127.  
  128. background-color: black !important;
  129. background-image: url("${svgFileBlack}") !important;
  130. }
  131.  
  132.  
  133. .kds1e-hover {
  134. color: #d5e4f4 !important;
  135. background-color: #d42b14 !important;
  136. }
  137.  
  138. `
  139. }
  140.  
  141.  
  142. let links = document.querySelectorAll('a[href*="https://greatest.deepsurf.us/"][href*="scripts/"]');
  143.  
  144. for(const link of links){
  145. const div = document.createElement('div');
  146. div.classList.add('kds1e-div');
  147. div.setAttribute('data-control-link', link.getAttribute('href'));
  148. div.addEventListener('mouseenter', divMouseEnter, false);
  149. div.addEventListener('mouseleave', divMouseLeave, false);
  150. div.addEventListener('click', divClick, false);
  151. div.appendChild(document.createElement('a')).textContent='\u200B';
  152. tmpLinks.push(div);
  153. link.parentNode.insertBefore(div, link.nextSibling);
  154. }
  155.  
  156.  
  157. }
  158.  
  159. function onKeyUp(evt){
  160. if(keys.size > 0){
  161. keys.delete(evt.code);
  162. if(keys.size === 0){
  163. if(tmpLinks.length===0) return;
  164. for(const s of document.querySelectorAll('.kds1e-hover')) s.classList.remove('kds1e-hover')
  165.  
  166. for(const link of tmpLinks) link.remove();
  167. tmpLinks.length=0;
  168. }
  169. }
  170.  
  171. }
  172.  
  173. let mz = 0;
  174. document.addEventListener("visibilitychange", () => {
  175.  
  176. if(keys.size>0 || tmpLinks.length>0){
  177.  
  178. if(!mz){
  179. mz=1;
  180. window.requestAnimationFrame(()=>{
  181.  
  182. mz = 0;
  183. keys.clear();
  184. for(const s of document.querySelectorAll('.kds1e-hover')) s.classList.remove('kds1e-hover')
  185.  
  186. for(const link of tmpLinks) link.remove();
  187. tmpLinks.length=0;
  188.  
  189. })
  190. }
  191.  
  192.  
  193. }
  194.  
  195. }, false);
  196.  
  197. document.addEventListener('keydown', onKeyDown, true);
  198. document.addEventListener('keyup', onKeyUp, true);
  199.  
  200. })();