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