Greasy Fork is available in English.

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.

  1. // ==UserScript==
  2. // @name jQuery and common function shortcuts everywhere
  3. // @namespace https://github.com/Alistair1231/my-userscripts/
  4. // @version 0.5.1
  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 MIT
  10. // ==/UserScript==
  11. // https://greasyfork.org/en/scripts/439017-jquery-and-common-function-shortcuts-everywhere
  12. (function () {
  13. 'use strict';
  14. const helpString = `
  15. '\
  16. al: jQuery and Method shortcuts everywhere\\n\
  17. ------------------------------------------\\n\
  18. al.cl(str) - console.log(str)\\n\
  19. al.js(obj) - JSON.stringify(obj)\\n\
  20. al.jsp(obj) - JSON.stringify(obj, null, 2)\\n\
  21. al.jp(str) - JSON.parse(str)\\n\
  22. al.qs(selector) - document.querySelector(selector)\\n\
  23. al.qsa(selector) - document.querySelectorAll(selector)\\n\
  24. al.gid(id) - document.getElementById(id)\\n\
  25. al.print() - Prints the object definition\\n\
  26. al.eurl(str) - document.encodeURIComponent(str)\\n\
  27. al.durl(str) - document.decodeURIComponent(str)\\n\
  28. ------------------------------------------\\n\
  29. '
  30. `;
  31.  
  32. const shortcuts = `
  33. const al = {
  34. help: () => console.log(${helpString}),
  35. cl: (str) => console.log(str),
  36. js: (obj) => JSON.stringify(obj),
  37. jsp: (obj) => JSON.stringify(obj, null, 2),
  38. jp: (str) => JSON.parse(str),
  39. qs: (selector) => document.querySelector(selector),
  40. qsa: (selector) => document.querySelectorAll(selector),
  41. gid: (id) => document.getElementById(id),
  42. print: () => {
  43. const { help, ...rest } = al;
  44. console.log(rest);
  45. },
  46. eurl: (str) => document.encodeURIComponent(str),
  47. durl: (str) => document.decodeURIComponent(str)
  48. };
  49. `;
  50.  
  51. const e = document.createElement('script');
  52. e.id = 'injectedScript';
  53. e.innerText = shortcuts;
  54.  
  55. document.head.appendChild(e);
  56.  
  57. if (typeof $ != 'function') {
  58. window.$ = function (selector, target) {
  59. if (typeof target == 'undefined') {
  60. target = document;
  61. }
  62. return target.querySelector(selector);
  63. }
  64. }
  65. if (typeof $$ != 'function') {
  66. window.$$ = function (selector, target) {
  67. if (typeof target == 'undefined') {
  68. target = document;
  69. }
  70. return Array.from(target.querySelectorAll(selector));
  71. }
  72. }
  73.  
  74. })();