tms-inject-jquery

为页面注入jQuery引用节点,便于使用控制台调试jQuery选择器等.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        tms-inject-jquery
// @version     1.0.13
// @description 为页面注入jQuery引用节点,便于使用控制台调试jQuery选择器等.
// @license     MIT
// @namespace   https://github.com/fjxhkj/tms-inject-jquery
// @supportURL  https://github.com/fjxhkj/tms-inject-jquery
// @match       *://*/*
// @run-at      document-end
// ==/UserScript==

(function() {
  'use strict';
  console.log('check jquery');
  waitForEl('body', function() {
    if ((typeof $) === 'undefined') {
      var scheme = window.location.protocol;
      var node = document.createElement('script');
      node.setAttribute('src',
           scheme + '//cdn.staticfile.org/jquery/1.12.4/jquery.min.js');
      document.body.appendChild(node);
      console.log('jQuery v1.12.4 append!');
    }
    else {
      console.log('jQuery already exists, ver: ' + $.fn.jquery);
    }
  }, 3000);

  /**
   * 等待指定CSS选择器的DOM元素出现
   *
   * @param selector [CSS选择器语法参考](https://www.runoob.com/cssref/css-selectors.html)
   * @param callback 回调函数,如果超时则回调函数参数为null,否则为目标DOM元素
   * @param maxtries 尝试次数,设为0时至少会尝试一次,设为 false 则一直尝试,设为大于0的整数则尝试该次数
   * @param interval 尝试间隔,单位毫秒
   */
  function waitForEl(selector, callback, maxtries = false, interval = 100) {
    var poller = setInterval(function() {
      var el = document.querySelector(selector);
      var retry = maxtries === false || maxtries-- > 0;
      if (el === null && retry) {
        return;
      }
      clearInterval(poller);
      callback(el || null);
    }, interval);
  }

})();