jQuery and common function shortcuts everywhere

injects jquery if not exists

当前为 2023-04-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name jQuery and common function shortcuts everywhere
  3. // @namespace https://github.com/Alistair1231/my-userscripts/
  4. // @version 0.4.1
  5. // @description injects jquery if not exists
  6. // @author Alistair1231
  7. // @match *://*/*
  8. // @grant GM_xmlhttpRequest
  9. // @license GPL-3.0
  10. // ==/UserScript==
  11. // https://greasyfork.org/en/scripts/439017-jquery-and-common-function-shortcuts-everywhere
  12. (function () {
  13. 'use strict';
  14. let e = document.createElement('script');
  15. e.id = 'injected-script';
  16. e.innerText = "";
  17. document.head.appendChild(e);
  18.  
  19.  
  20. if (typeof jQuery == 'undefined') {
  21. // https://stackoverflow.com/questions/54499985/how-can-i-load-an-external-script-on-a-webpage-using-tampermonkey
  22. GM_xmlhttpRequest({
  23. method: "GET",
  24. // from other domain than the @match one (.org / .com):
  25. url: "https://code.jquery.com/jquery-3.6.0.min.js",
  26. onload: (ev) => {
  27. e.innerText += ev.responseText;
  28. jQuery.noConflict();
  29. }
  30. });
  31. }
  32.  
  33. function removeIndent(str) {
  34. const lines = str.split('\n');
  35. if (lines[0] !== '' || lines[lines.length - 1] !== '') {
  36. return str;
  37. }
  38. lines.shift();
  39. lines.pop();
  40.  
  41. const lens = lines.map(l => l.match(/ */)[0].length)
  42. const minLen = Math.min(...lens);
  43. return '\n' + lines.map(l => l.substr(minLen)).join('\n') + '\n';
  44. }
  45.  
  46. const helpString = removeIndent`
  47. al: jQuery and Method shortcuts everywhere
  48. ------------------------------------------
  49. al.js(obj) - JSON.stringify(obj)
  50. al.jsp(obj) - JSON.stringify(obj, null, 2)
  51. al.jp(str) - JSON.parse(str)
  52. al.qs(selector) - document.querySelector(selector)
  53. al.qsa(selector) - document.querySelectorAll(selector)
  54. al.gid(id) - document.getElementById(id)
  55. ------------------------------------------`;
  56.  
  57. const shortcuts = removeIndent`
  58. const al = {
  59. help: () => console.log(${helpString}),
  60. js: (obj) => JSON.stringify(obj),
  61. jsp: (obj) => JSON.stringify(obj, null, 2),
  62. jp: (str) => JSON.parse(str),
  63. qs: (selector) => document.querySelector(selector),
  64. qsa: (selector) => document.querySelectorAll(selector),
  65. gid: (id) => document.getElementById(id),
  66. };
  67. `;
  68.  
  69. e.innerText += shortcuts;
  70. })();