Any jQuery,页面注入jQuery,方便控制台调试代码

write once run anywhere,注入jQuery,方便控制台调试代码

  1. // ==UserScript==
  2. // @name Any jQuery,页面注入jQuery,方便控制台调试代码
  3. // @namespace http://bbs.91wc.net/any-jquery.htm
  4. // @version 0.3.12
  5. // @description write once run anywhere,注入jQuery,方便控制台调试代码
  6. // @author Wilson
  7. // @match http*://*/*
  8. // @require https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js
  9. // @resource jquery https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js
  10. // @require https://gf.qytechs.cn/scripts/411517-mytips/code/MyTips.js?version=851635
  11. // @grant unsafeWindow
  12. // @grant GM_getResourceText
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_unregisterMenuCommand
  15. // @grant GM_setValue
  16. // @grant GM_getValue
  17. // ==/UserScript==
  18.  
  19. var myLibs = {
  20. "lodash" : "https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.20/lodash.min.js",
  21. "underscore" : "https://cdn.bootcdn.net/ajax/libs/underscore.js/1.11.0/underscore-min.js",
  22. "require" : "https://cdn.bootcdn.net/ajax/libs/require.js/2.3.6/require.min.js",
  23. "loadjs" : "https://unpkg.com/loadjs@4.2.0/dist/loadjs.min.js",
  24. };
  25. var manulUrls = {
  26. 'jquery' : 'http://www.shouce.ren/api/view/a/12780',
  27. "lodash" : "https://www.lodashjs.com/",
  28. "underscore" : "https://underscorejs.bootcss.com/",
  29. "require" : "https://requirejs.org/",
  30. "loadjs" : "https://github.com/muicss/loadjs",
  31. };
  32.  
  33. //jQuery注入成功后执行
  34. function afterInject() {
  35. //Your codes here
  36.  
  37.  
  38. //你也可以像这样向页面中写全局变量或函数
  39. //_g.a="test a";
  40. //_g.b = function(){
  41. // console.log("test b");
  42. //}
  43. }
  44.  
  45. function aferChangeJquery(){
  46.  
  47. }
  48.  
  49. function initJQ(){
  50. //初始化_jq变量
  51. _g._jq = _g.jQuery;
  52. _g._jq.version=_g._jq.fn.jquery;
  53. _g.ww = window;
  54. _g.ww.version = jQuery.fn.jquery;
  55. };
  56.  
  57. function initMyInfo(){
  58. //脚本信息
  59. _g.myinfo = {};
  60. _g.myinfo.name=GM_info.script.name;
  61. _g.myinfo.ver=GM_info.script.version;
  62. _g.myinfo.jqurl=GM_info.script.resources[0].url;
  63. };
  64.  
  65. function usefullTools(){
  66. //切换jQuery版本
  67. if(typeof _g.changeJquery == 'undefined'){
  68. _g.changeJquery=_g.chjq=function(ver){
  69. if(console && console.info) console.info("%cWarning: switching jQuery may destroy the data structure of the original page", "color:red");
  70. if(!ver && console && console.info){return _g.jqVersions;}
  71. if(_g.jq === null && console && console.error){return "error: Please inject jQuery first";}
  72. if(/^\d+$/.test(ver)){ver = _g.jqVersions[ver];}
  73. var jqurl = "https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js";
  74. jqurl=jqurl.replace(/@[\d.]+\//, '@'+ver+'/');
  75. $.get(jqurl, function(data) {
  76. if($("#_g_jquery").length>0) $("#_g_jquery").remove();
  77. $("body").append('<script id="_g_jquery">'+data+'</script>');
  78. initJQ();
  79. if(fns) _g._jq.fn=_g.$.fn=_g.jQuery.fn = fns;
  80. if(xtend) _g._jq.extend=_g.$.extend=_g.jQuery.extend = xtend;
  81. if(xtendfn) _g._jq.fn.extend=_g.$.fn.extend=_g.jQuery.fn.extend = xtendfn;
  82. if(jqprototype) _g._jq.prototype=_g.$.prototype=_g.jQuery.prototype = jqprototype;
  83. aferChangeJquery();
  84. if(console && console.info) console.info("%csucessful","color:green", "loaded from: "+jqurl);
  85. }).fail(function(jqxhr, settings, exception) {
  86. if(console && console.log) console.error(exception, "load from: "+jqurl);
  87. });
  88. return "loading...";
  89. }
  90. }
  91.  
  92. //把jQuery恢复到最初的状态
  93. if(typeof _g.changeToFirst == 'undefined'){
  94. _g.changeToFirst=_g.chfs=function(){
  95. if(_g.chfsjq){
  96. _g.$ = _g.jQuery = _g.chfsjq;
  97. initJQ();
  98. setTimeout(function(){
  99. if(console && console.info) console.info("%csucessful","color:green");
  100. });
  101. return "done";
  102. } else {
  103. return "failed";
  104. }
  105. }
  106. }
  107.  
  108. //模拟console.log
  109. if(typeof _g.cc === 'undefined'){
  110. _g.cc=function(v1,v2,v3,v4,v5){
  111. var spl="-----------------------------";
  112. if(v1 !== undefined && v2 !== undefined && v3 !== undefined && v4 !== undefined && v5 !== undefined) {console.log(v1,v2,v3,v4,v5);return spl;}
  113. if(v1 !== undefined && v2 !== undefined && v3 !== undefined && v4 !== undefined) {console.log(v1,v2,v3,v4);return spl;}
  114. if(v1 !== undefined && v2 !== undefined && v3 !== undefined) {console.log(v1,v2,v3);return spl;}
  115. if(v1 !== undefined && v2 !== undefined) {console.log(v1,v2);return spl;}
  116. if(v1 !== undefined) {console.log(v1);return spl;}
  117. console.log();return spl;
  118. }
  119. }
  120.  
  121. //打开手册
  122. if(typeof _g.manul === 'undefined'){
  123. _g.manul=function(name){
  124. name = name || 'jquery';
  125. window.open(manulUrls[name]);
  126. return "done";
  127. }
  128. }
  129.  
  130. //手册列表
  131. if(typeof _g.manuls === 'undefined'){
  132. _g.manuls=function(){
  133. console.log(manulUrls);
  134. }
  135. }
  136. //添加js代码
  137. if(typeof _g.addScript === 'undefined'){
  138. _g.addScript=function(jscode){
  139. $("body").append(`<script>`+jscode+`</script>`);
  140. return "done";
  141. }
  142. }
  143. //添加css代码
  144. if(typeof _g.addStyle === 'undefined'){
  145. _g.addStyle=function(csscode){
  146. $("body").append(`<style>`+csscode+`</style>`);
  147. return "done";
  148. }
  149. }
  150. //加载类库
  151. if(typeof _g.loadLib === 'undefined'){
  152. _g.loadLib=function(name){
  153. if(!name) return myLibs;
  154. $("body").append(`<script src="`+myLibs[name]+`"></script>`);
  155. return "done";
  156. }
  157. }
  158. //添加html
  159. if(typeof _g.addHtml === 'undefined'){
  160. _g.addHtml=function(html){
  161. $("body").append(html);
  162. return "done";
  163. }
  164. }
  165.  
  166. //页面进入编辑模式
  167. if(typeof _g.editPage === 'undefined'){
  168. _g.editPage=function(isEdit){
  169. isEdit = isEdit === undefined ? true : isEdit;
  170. if(isEdit){
  171. document.body.contentEditable=true;
  172. return "页面已进入编辑模式";
  173. } else {
  174. document.body.contentEditable=false;
  175. return "页面已退出编辑模式";
  176. }
  177. }
  178. }
  179.  
  180. //获取jQuery版本
  181. getJQVers();
  182. //$.getJSON('https://data.jsdelivr.com/v1/package/npm/jquery', function(data) {
  183. // if(data && data.versions){
  184. // _g.jqVersions = data.versions;
  185. // }
  186. //});
  187. }
  188.  
  189. function getJQVers() {
  190. Object.defineProperty(_g, 'jqVersions', {
  191. get: function() {
  192. let data = getJSONSync("https://data.jsdelivr.com/v1/package/npm/jquery");
  193. return data.versions;
  194. }
  195. });
  196. }
  197.  
  198. function getJSONSync(url){
  199. var request = new XMLHttpRequest(); // 创建新请求
  200. request.open("GET",url,false); // 传递false实现同步
  201. request.send(); // 立即发送请求
  202.  
  203. // 如果请求不是200 OK,就报错
  204. if(request.status!==200) throw new Error(request.statusText);
  205. // 如果类型报错
  206. var type = request.getResponseHeader("Content-Type");
  207. // if(!type.match(/^text/))
  208. // throw new Error("Expected textual response;got:"+type);
  209. var rs = {};
  210. try{rs=JSON.parse(request.responseText);}catch(e){console.log(e)}
  211. return rs;
  212. }
  213.  
  214. var fns, xtend, xtendfn,jqprototype;
  215. function injectJquery(){
  216. if(_g._jq === null){
  217. setTimeout(function(){
  218. //动态设置jQuery
  219. //var jq_code = GM_getResourceText('jquery');
  220. //$("body").append('<script id="_g_jquery">'+jq_code+'</script>');
  221. _g.$ = _g.jQuery = jQuery;
  222. //记住切换前的JQ
  223. _g.chfsjq = jQuery;
  224.  
  225. //初始化_jq变量
  226. initJQ();
  227. //call afterInject
  228. afterInject();
  229. //system use
  230. sysAfterInject
  231. }, 0);
  232. } else {
  233. //记住切换前的JQ
  234. _g.chfsjq = _g._jq;
  235.  
  236. //初始化_jq变量
  237. initJQ();
  238. //call afterInject
  239. afterInject();
  240. //system use
  241. sysAfterInject
  242.  
  243. fns = _g._jq.fn;
  244. xtend = _g._jq.extend;
  245. xtendfn = _g._jq.fn.extend;
  246. jqprototype = _g._jq.prototype;
  247. }
  248. };
  249.  
  250. function sysAfterInject(){
  251.  
  252. }
  253.  
  254. //防冲突
  255. this.$ = this.jQuery = jQuery.noConflict(true);
  256. //注入jQuery和全局变量_g
  257. $("body").append(`<script id="_g_script">var _g=window, _w=_g, _jq=(typeof jQuery === "undefined") ? null : jQuery;</script>`);
  258. var _g = _g||unsafeWindow;
  259. //注入脚本信息
  260. initMyInfo();
  261. //注入jQuery
  262. var __jq_switch = GM_getValue("__jq_switch")||"open";
  263. if(__jq_switch=="open"){
  264. injectJquery();
  265. }
  266. //一些工具
  267. usefullTools()
  268.  
  269. //开关
  270. var jquerySwitchMenu =function(type, callback){
  271. var closeMenu, closeCallback = function(){
  272. if(callback) callback("close");
  273. if(closeMenu) GM_unregisterMenuCommand(closeMenu);
  274. openMenu = GM_registerMenuCommand("立即注入", openCallback);
  275. }
  276. var openMenu, openCallback = function(){
  277. if(callback) callback("open");
  278. if(openMenu) GM_unregisterMenuCommand(openMenu);
  279. closeMenu = GM_registerMenuCommand("取消注入", closeCallback);
  280. }
  281. if(type==="open"){
  282. closeMenu=GM_registerMenuCommand("取消注入", closeCallback);
  283. } else {
  284. openMenu=GM_registerMenuCommand("立即注入", openCallback);
  285. }
  286. };
  287. jquerySwitchMenu(__jq_switch, function(type){
  288. GM_setValue("__jq_switch", type);
  289. if(type=="open"){
  290. injectJquery()
  291. if(MyTips){top.MyTips.sucessTips("注入成功",null,null,{width:400,height:80});} else {alert("注入成功");}
  292. }else{
  293. if(MyTips){top.MyTips.sucessTips("取消成功,下次刷新生效",null,null,{width:400,height:80});} else {alert("取消成功,下次刷新生效");}
  294. }
  295. });

QingJ © 2025

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