sfs-utils-0.1.5

Utility functions such as log, js console, etc.

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/375360/652240/sfs-utils-015.js

  1. /* sfs-utils.js v0.1.5 */
  2.  
  3. // ==UserLibrary==
  4. // @pseudoHeader
  5. // @name SFS Utils
  6. // @version 0.1.5
  7. // ==/UserLibrary==
  8.  
  9. // Script must have a version of:
  10. //
  11. // // @require https://raw.githubusercontent.com/SloaneFox/code/master/gm4-polyfill-1.0.1.js
  12. //
  13. // in header to use this module userscript. Also for use of cmdrepl console function below grant of GM register command is required.
  14.  
  15. // See below for functions:
  16. // function log(arguments); // prints correct line number under GM4, due to scope wrapper the given one is incorrect.
  17. // function cmdrepl(e,immediate_flag); // Launch a js console from any place in the code &/or register one from a menu.
  18. // function logNewNodes(); // Logs to console any new nodes (uses a mutation observer).
  19. // var sname; // Set to GM info's script.name.
  20. //
  21.  
  22. function logError(msg,e) { console.error("Error,",msg,". On line:",Elineno(e),"lineno offset:",-log.lineoffset,{Error:e}); }
  23. function typeofObj(unknown_obj){ return ({}).toString.call(unknown_obj).substr(8).slice(0,-1); }
  24. function Elineno(e) { return e.lineNumber-log.lineoffset; }
  25.  
  26. function objInfo(obj) {
  27.  
  28. switch(typeofObj(obj)) {
  29. case "Event": return "Event:"+obj.type+" "+objInfo(obj.target);
  30. case "Function": return obj.toString().substr(0,200);
  31. case "String":
  32. case "Number": return obj;
  33. default:
  34. var node=obj;
  35. if(node.jquery) node=node[0];
  36. if (!node) return "<empty>";
  37. return (node.tagName||node.nodeName)+" "+(node.id?"#"+node.id:"")
  38. +(node.className?node.className:"").replace(/\b(?=\w)/g,".");
  39. }
  40. }
  41.  
  42. function log() { // Prints lineno of logging not this lineno. //if (!Plat_Chrome) old_GM_log(t);};
  43. var args=Array.from(arguments), lineno=parseInt(logStack(0,1))-log.lineoffset, pnewline,
  44. locator="[ "+lineno +":"+ sname+ " "+( window!=parent? ("wname:"+window.name? window.name:"-") +" @"+location+", rstate: "+document.readyState:"") + " ]\t";
  45. args.unshift(locator);
  46. console.log.apply(console, args);
  47. // In general it is console.log("%c a msg and another %c meggss","float:right","float:left;",anobj,"text","etc");
  48.  
  49. function logStack(fileToo, lineno_of_callee) { // deepest first.
  50. var res="", e=new Error;
  51. var s=e.stack.split("\n"); //if (fileToo) res="Stack of callers:\n\t\t"; //+s[1].split("@")[0]+"():\n\t\t"
  52. if (lineno_of_callee) return (s[2].match(/:\d+(?=:)/)||[""])[0].substr(1); // Just give line number of one who called the function that called this, ie, geives lineno -f callee.
  53. for (var i=1;i<s.length-1;i++)
  54. res+=s[i].split("@")[0]+"() "+s[i].split(":").slice(-2)+"\n";
  55. return !fileToo ? res : {Stack:s[0]+"\n"+res};
  56. }
  57. };
  58.  
  59. if (log.lineoffset==undefined) { // cos ff58 has linon at 360 + script lineno.
  60. var ver=0,offset=0,ver_pos=navigator.userAgent.indexOf("Firefox/");
  61. if (ver_pos!=-1) {
  62. let v=parseInt(navigator.userAgent.substr(ver_pos+8));
  63. if (v>=58 && v<60) offset=360;
  64. if (v>=60) offset=492;
  65. }
  66. log.lineoffset=offset;
  67. }
  68.  
  69. function logNewNodes() {
  70. new MutationObserver((mutations, observer) => {try{
  71. for (m of mutations) for (n of m.addedNodes) {
  72. console.log("Node added",n.nodeName,(n.nodeType!=3 ? n : n.textContent.substr(0,40)+"..."));
  73. if(n.nodeName=="IFRAME") {
  74. $(n).removeAttr("sandbox");
  75. n.addEventListener('load', function (e) {console.log("Loaded IFRAME",location,n,"this:",this);});
  76. }
  77. }
  78. }catch(e){console.error("logNewNodes error",e.lineNumber,e);}})
  79. .observe(document.documentElement, {childList: true,subtree:true});
  80. }
  81.  
  82. // Call cmdreply to get js console at that point. Pass reg in to register cmd console as a menu command.
  83. // If cant register cmd, invoke immediately.
  84.  
  85. var sname= typeof GM != "undefined" ? GM.info && GM.info.script.name : "noscript name";
  86.  
  87. async function cmdrepl(e={},immediate,...args) { // When called from GM menu e is set to event.
  88. if(!immediate && !cmdrepl.regdone) { // if (typeof GM_registerMenuCommand!="undefined" && document.body)
  89. cmdrepl.regdone=true;
  90. setTimeout(function(){
  91. //console.log("in sfs_utils GM.","abc"+GM_registerMenuCommand);
  92. GM_registerMenuCommand("JS repl",cmdrepl); GM.registerMenuCommand("JS repl",cmdrepl); // reg in both
  93. },2000);
  94. return;
  95. }
  96. ls={};
  97. try { let tmp=localStorage.reply; ls=localStorage; } catch(e){}
  98. var res=e.message||sname+", enter command:",reply=ls.reply||"cmd";
  99. while(reply) {
  100. reply=prompt(res,reply);
  101. if(!reply) break;
  102. ls.reply=reply;
  103. try{ res=await eval(reply); console.log(reply,"==>",res);res="==>"+res; } catch(e) {console.log("cmd err",e); cmdrepl(e);}
  104. }
  105. }
  106.  
  107.  

QingJ © 2025

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