jQuery and common function shortcuts everywhere

injects jquery if not exists

2023-04-25 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         jQuery and common function shortcuts everywhere
// @namespace    https://github.com/Alistair1231/my-userscripts/
// @version      0.4.1
// @description  injects jquery if not exists
// @author       Alistair1231
// @match      *://*/*
// @grant        GM_xmlhttpRequest
// @license GPL-3.0
// ==/UserScript==
// https://greasyfork.org/en/scripts/439017-jquery-and-common-function-shortcuts-everywhere
(function () {
  'use strict';
  let e = document.createElement('script');
  e.id = 'injected-script';
  e.innerText = "";
  document.head.appendChild(e);


  if (typeof jQuery == 'undefined') {
    // https://stackoverflow.com/questions/54499985/how-can-i-load-an-external-script-on-a-webpage-using-tampermonkey
    GM_xmlhttpRequest({
      method: "GET",
      // from other domain than the @match one (.org / .com):
      url: "https://code.jquery.com/jquery-3.6.0.min.js",
      onload: (ev) => {
        e.innerText += ev.responseText;
        jQuery.noConflict();
      }
    });
  }

  function removeIndent(str) {
    const lines = str.split('\n');
    if (lines[0] !== '' || lines[lines.length - 1] !== '') {
      return str;
    }
    lines.shift();
    lines.pop();

    const lens = lines.map(l => l.match(/ */)[0].length)
    const minLen = Math.min(...lens);
    return '\n' + lines.map(l => l.substr(minLen)).join('\n') + '\n';
  }

  const helpString = removeIndent`
    al: jQuery and Method shortcuts everywhere
    ------------------------------------------
    al.js(obj) - JSON.stringify(obj)
    al.jsp(obj) - JSON.stringify(obj, null, 2)
    al.jp(str) - JSON.parse(str)
    al.qs(selector) - document.querySelector(selector)
    al.qsa(selector) - document.querySelectorAll(selector)
    al.gid(id) - document.getElementById(id)
    ------------------------------------------`;

  const shortcuts = removeIndent`
    const al = {
      help: () => console.log(${helpString}),
      js: (obj) => JSON.stringify(obj),
      jsp: (obj) => JSON.stringify(obj, null, 2),
      jp: (str) => JSON.parse(str),
      qs: (selector) => document.querySelector(selector),
      qsa: (selector) => document.querySelectorAll(selector),
      gid: (id) => document.getElementById(id),
    };
`;

  e.innerText += shortcuts;
})();