jQuery and common function shortcuts everywhere

injects jquery if not exists and adds some common function shortcuts to the window object. See al.help() for details.

目前为 2023-04-26 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name jQuery and common function shortcuts everywhere
  3. // @namespace https://github.com/Alistair1231/my-userscripts/
  4. // @version 0.4.4
  5. // @description injects jquery if not exists and adds some common function shortcuts to the window object. See al.help() for details.
  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.  
  15. const helpString = `
  16. al: jQuery and Method shortcuts everywhere
  17. ------------------------------------------
  18. al.js(obj) - JSON.stringify(obj)
  19. al.jsp(obj) - JSON.stringify(obj, null, 2)
  20. al.jp(str) - JSON.parse(str)
  21. al.qs(selector) - document.querySelector(selector)
  22. al.qsa(selector) - document.querySelectorAll(selector)
  23. al.gid(id) - document.getElementById(id)
  24. ------------------------------------------`;
  25.  
  26. const shortcuts = `
  27. const al = {
  28. help: () => console.log('${helpString}'),
  29. js: (obj) => JSON.stringify(obj),
  30. jsp: (obj) => JSON.stringify(obj, null, 2),
  31. jp: (str) => JSON.parse(str),
  32. qs: (selector) => document.querySelector(selector),
  33. qsa: (selector) => document.querySelectorAll(selector),
  34. gid: (id) => document.getElementById(id)
  35. };
  36. `;
  37.  
  38. let e = document.createElement('script');
  39. e.id = 'injectedScript';
  40. e.innerText = shortcuts;
  41. document.head.appendChild(e);
  42.  
  43. if (typeof jQuery == 'undefined') {
  44. // https://stackoverflow.com/questions/54499985/how-can-i-load-an-external-script-on-a-webpage-using-tampermonkey
  45. GM_xmlhttpRequest({
  46. method: "GET",
  47. // from other domain than the @match one (.org / .com):
  48. url: "https://code.jquery.com/jquery-3.6.0.min.js",
  49. onload: (ev) => {
  50. e.innerText += ev.responseText;
  51. }
  52. });
  53. }
  54.  
  55.  
  56. })();