|
54 | 54 | <script> |
55 | 55 |
|
56 | 56 | (function output_console({mod,dom,host}){ |
57 | | - //console.log('output'); |
| 57 | + |
58 | 58 | var obj = { |
59 | 59 | version : 'v2.0', |
60 | 60 | }; |
61 | 61 |
|
62 | | - |
| 62 | + var df=false,did='output-console' |
| 63 | + ; |
| 64 | + |
| 65 | + |
| 66 | + |
63 | 67 | var ext,$ |
64 | 68 | ; |
65 | 69 |
|
|
100 | 104 |
|
101 | 105 |
|
102 | 106 | obj.init = async function(){ |
103 | | - |
| 107 | + debug('init',obj.version); |
104 | 108 | await libs(); |
105 | 109 |
|
106 | 110 | }//init |
|
118 | 122 |
|
119 | 123 |
|
120 | 124 | obj.initdom = function(rootnode){ |
121 | | - |
| 125 | + debug('initdom'); |
122 | 126 | shadow = host.shadowRoot; |
123 | 127 |
|
124 | 128 | chk.persist = $.chkbox(shadow,'#console-persist',false); |
|
140 | 144 |
|
141 | 145 |
|
142 | 146 | btn.kill = function(){ |
143 | | - |
| 147 | + debug('btn.kill'); |
144 | 148 | kill(); |
145 | 149 |
|
146 | 150 | }//kill |
147 | 151 |
|
148 | 152 |
|
149 | 153 | btn.clear = function(){ |
150 | | - |
| 154 | + debug('btn.clear'); |
151 | 155 | clear(); |
152 | 156 |
|
153 | 157 | }//clear |
|
165 | 169 |
|
166 | 170 |
|
167 | 171 | obj.run = async function(js,params={}){ |
168 | | - console.log('run'); |
| 172 | + debug('run'); |
169 | 173 | var sandbox = (()=>{ |
170 | 174 |
|
171 | 175 | var console = {}; |
|
221 | 225 |
|
222 | 226 |
|
223 | 227 | obj.run.iframe = async function(js,params={}){ |
224 | | - console.log('run.iframe'); |
| 228 | + debug('run.iframe'); |
225 | 229 | var resolve,promise=new Promise(res=>resolve=res); |
226 | 230 |
|
227 | 231 | if(iframe){ |
|
271 | 275 |
|
272 | 276 |
|
273 | 277 | obj.run.iframe2 = async function(js,params={}){ |
274 | | - console.log('run.iframe2'); |
| 278 | + debug('run.iframe2'); |
275 | 279 | var resolve,promise=new Promise(res=>resolve=res); |
276 | 280 |
|
277 | 281 | if(iframe){ |
|
675 | 679 | //: |
676 | 680 |
|
677 | 681 |
|
| 682 | + function datatype(v){return Object.prototype.toString.call(v).slice(8,-1).toLowerCase()} |
| 683 | + |
| 684 | + |
678 | 685 | function format(args){ |
679 | 686 |
|
680 | 687 | if (typeof args[0] !== "string") return args; |
|
727 | 734 |
|
728 | 735 | format.fn = function(val,seen=new WeakSet()){ |
729 | 736 |
|
730 | | - if (val === null) return 'null'; |
731 | | - if (val === undefined) return 'undefined'; |
| 737 | + if(val === null)return 'null'; |
| 738 | + if(val === undefined)return 'undefined'; |
732 | 739 |
|
733 | 740 | // Prevent circular references |
734 | | - if (typeof val === 'object' || typeof val === 'function') { |
735 | | - if (seen.has(val)) return '[Circular]'; |
| 741 | + if(typeof val==='object' || typeof val==='function'){ |
| 742 | + if(seen.has(val))return '[Circular]'; |
736 | 743 | seen.add(val); |
737 | 744 | } |
738 | 745 |
|
739 | 746 | // Primitive types |
740 | | - switch (typeof val) { |
741 | | - |
742 | | - case 'number': return String(val); |
743 | | - case 'string': return val; |
744 | | - case 'boolean': return String(val); |
745 | | - case 'bigint': return val.toString() + 'n'; |
746 | | - case 'symbol': return val.toString(); |
747 | | - case 'function': return `[Function${val.name ? ': ' + val.name : ''}]`; |
| 747 | + switch(typeof val){ |
| 748 | + |
| 749 | + case 'number' : return String(val); |
| 750 | + case 'string' : return val; |
| 751 | + case 'boolean' : return String(val); |
| 752 | + case 'bigint' : return val.toString() + 'n'; |
| 753 | + case 'symbol' : return val.toString(); |
| 754 | + case 'function' : return `[Function${val.name ? ': '+val.name : ''}]`; |
748 | 755 |
|
749 | 756 | }//switch |
750 | 757 |
|
751 | | - if (Array.isArray(val)) { |
752 | | - return '[' + val.map(v => fn(v, seen)).join(', ') + ']'; |
| 758 | + var type = datatype(val); |
| 759 | + |
| 760 | + if(type=='array'){ |
| 761 | + return '['+val.map(v=>fn(v,seen)).join(', ')+']'; |
753 | 762 | } |
754 | 763 |
|
755 | | - if (val instanceof Date) { |
| 764 | + if(type=='date'){//val instanceof Date){ |
756 | 765 | return `Date("${val.toISOString()}")`; |
757 | 766 | } |
758 | 767 |
|
759 | | - if (val instanceof RegExp) { |
| 768 | + if(val instanceof RegExp){ |
760 | 769 | return val.toString(); |
761 | 770 | } |
762 | 771 |
|
763 | | - if (val instanceof Error) { |
| 772 | + if(val instanceof Error){ |
764 | 773 | return `${val.name}: ${val.message}`; |
765 | 774 | } |
766 | 775 |
|
767 | | - if (val instanceof Map) { |
| 776 | + if(val instanceof Map){ |
768 | 777 | const entries = []; |
769 | 778 | for (const [k, v] of val.entries()) { |
770 | 779 | entries.push(`${fn(k, seen)} => ${fn(v, seen)}`); |
771 | 780 | } |
772 | 781 | return `Map { ${entries.join(', ')} }`; |
773 | 782 | } |
774 | 783 |
|
775 | | - if (val instanceof Set) { |
776 | | - const entries = [...val].map(v => fn(v, seen)); |
| 784 | + if(val instanceof Set){ |
| 785 | + const entries = [...val].map(v=>fn(v,seen)); |
777 | 786 | return `Set { ${entries.join(', ')} }`; |
778 | 787 | } |
779 | 788 |
|
780 | | - if (ArrayBuffer.isView(val) && !(val instanceof DataView)) { |
| 789 | + if(ArrayBuffer.isView(val) && !(val instanceof DataView)){ |
781 | 790 | return `${val.constructor.name} [ ${Array.from(val).join(', ')} ]`; |
782 | 791 | } |
783 | 792 |
|
784 | | - if (val instanceof Node) { |
785 | | - if (val.nodeType === 1) { |
786 | | - return `<${val.tagName.toLowerCase()}>`; |
| 793 | + if(val instanceof Node){ |
| 794 | + if(val.nodeType===1){ |
| 795 | + return `<${val.tagName.toLowerCase()}>`; |
787 | 796 | } |
788 | 797 | return val.toString(); |
789 | 798 | } |
790 | 799 |
|
791 | | - if (typeof val === 'object') { |
792 | | - const entries = Object.entries(val).map(([k, v]) => { |
| 800 | + if(typeof val==='object'){ |
| 801 | + const entries = Object.entries(val).map(([k, v])=>{ |
793 | 802 |
|
794 | 803 | return `${k}: ${fn(v, seen)}`; |
795 | 804 |
|
796 | 805 | }); |
797 | 806 | return `{ ${entries.join(', ')} }`; |
798 | 807 | } |
799 | 808 |
|
800 | | - try { |
| 809 | + try{ |
801 | 810 |
|
802 | 811 | return JSON.stringify(val); |
803 | 812 |
|
804 | 813 | }//try |
805 | | - catch { |
| 814 | + catch{ |
806 | 815 |
|
807 | 816 | return String(val); |
808 | 817 |
|
|
881 | 890 |
|
882 | 891 |
|
883 | 892 |
|
| 893 | + function debug(...args){ |
| 894 | + |
| 895 | + if(!df && !obj.df)return; |
| 896 | + args.unshift(`[ ${did} ]`); |
| 897 | + var fmt = Array.from({length:args.length}).fill('%O').join(' '); |
| 898 | + var args2 = [fmt].concat(args); |
| 899 | + console.groupCollapsed.apply(console,args2); |
| 900 | + console.trace(); |
| 901 | + console.groupEnd(); |
| 902 | + |
| 903 | + }//debug |
| 904 | + |
| 905 | + |
| 906 | + |
| 907 | + |
| 908 | + |
884 | 909 | return obj; |
885 | 910 |
|
886 | | -})//output_console |
| 911 | +//output_console |
| 912 | +}) |
| 913 | + |
887 | 914 |
|
888 | 915 | </script> |
889 | 916 |
|
| 917 | + |
890 | 918 | </output-console> |
891 | 919 |
|
| 920 | + |
| 921 | + |
0 commit comments