tms-inject-jquery

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

  1. // ==UserScript==
  2. // @name tms-inject-jquery
  3. // @version 1.0.13
  4. // @description 为页面注入jQuery引用节点,便于使用控制台调试jQuery选择器等.
  5. // @license MIT
  6. // @namespace https://github.com/fjxhkj/tms-inject-jquery
  7. // @supportURL https://github.com/fjxhkj/tms-inject-jquery
  8. // @match *://*/*
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. console.log('check jquery');
  15. waitForEl('body', function() {
  16. if ((typeof $) === 'undefined') {
  17. var scheme = window.location.protocol;
  18. var node = document.createElement('script');
  19. node.setAttribute('src',
  20. scheme + '//cdn.staticfile.org/jquery/1.12.4/jquery.min.js');
  21. document.body.appendChild(node);
  22. console.log('jQuery v1.12.4 append!');
  23. }
  24. else {
  25. console.log('jQuery already exists, ver: ' + $.fn.jquery);
  26. }
  27. }, 3000);
  28.  
  29. /**
  30. * 等待指定CSS选择器的DOM元素出现
  31. *
  32. * @param selector [CSS选择器语法参考](https://www.runoob.com/cssref/css-selectors.html)
  33. * @param callback 回调函数,如果超时则回调函数参数为null,否则为目标DOM元素
  34. * @param maxtries 尝试次数,设为0时至少会尝试一次,设为 false 则一直尝试,设为大于0的整数则尝试该次数
  35. * @param interval 尝试间隔,单位毫秒
  36. */
  37. function waitForEl(selector, callback, maxtries = false, interval = 100) {
  38. var poller = setInterval(function() {
  39. var el = document.querySelector(selector);
  40. var retry = maxtries === false || maxtries-- > 0;
  41. if (el === null && retry) {
  42. return;
  43. }
  44. clearInterval(poller);
  45. callback(el || null);
  46. }, interval);
  47. }
  48.  
  49. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址