soiz commited on
Commit
a32c2ad
·
verified ·
1 Parent(s): 1a28ce3

Create cl.js

Browse files
Files changed (1) hide show
  1. cl.js +259 -0
cl.js ADDED
@@ -0,0 +1,259 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ (function() {
2
+ var s;
3
+ if (!document.querySelector('style.EVALDOTJSSTYLETHING')) {
4
+ s=document.createElement("style");
5
+ s.className="EVALDOTJSSTYLETHING";
6
+ s.innerHTML=`
7
+ evalcontainer {
8
+ display: block;
9
+ position: fixed;
10
+ width: 500px;
11
+ height: 350px;
12
+ background: rgba(0,0,0,0.8);
13
+ top: 50px; /* 初期位置を指定 */
14
+ left: 0;
15
+ right: 0;
16
+ margin: auto;
17
+ font-size: 0;
18
+ z-index: 100000;
19
+ cursor: move; /* ドラッグカーソルを追加 */
20
+ }
21
+ evalcontainer > textarea.EVALTEXTAREA {
22
+ width: 500px;
23
+ border: none;
24
+ box-sizing: border-box;
25
+ font-family: monospace;
26
+ font-size: 12px;
27
+ height: 200px;
28
+ padding: 5px;
29
+ resize: none;
30
+ position: absolute;
31
+ bottom: 0;
32
+ background: rgba(0,0,0,0.8);
33
+ color: white;
34
+ }
35
+ evalcontainer > evaloutput {
36
+ display: block;
37
+ width: 500px;
38
+ max-height: 150px;
39
+ font-size: 12px;
40
+ font-family: monospace;
41
+ overflow-y: auto;
42
+ color: white;
43
+ box-sizing: border-box;
44
+ padding: 5px;
45
+ position: absolute;
46
+ bottom: 200px;
47
+ }
48
+ evalcontainer > evaloutput > evaloutputentry {
49
+ display: block;
50
+ white-space: pre;
51
+ }
52
+ evalcontainer > evaloutput > evaloutputentry::before {
53
+ content: ">\\00a0";
54
+ color: rgba(255,255,255,0.5);
55
+ }
56
+ evalcontainer > evaloutput > evaloutputentry.EVALRESULT {
57
+ border-bottom: 1px solid rgba(255,255,255,0.1);
58
+ }
59
+ evalcontainer > evaloutput > evaloutputentry.EVALRESULT::before {
60
+ content: "<\\00a0";
61
+ font-style: normal;
62
+ }
63
+ evalcontainer > evaloutput > evaloutputentry.EVALWARN {
64
+ background: #FFEB3B;
65
+ color: black;
66
+ }
67
+ evalcontainer > evaloutput > evaloutputentry.EVALSTRING {
68
+ color: #FFEB3B;
69
+ }
70
+ evalcontainer > evaloutput > evaloutputentry.EVALNUMBER {
71
+ color: #2196F3;
72
+ }
73
+ evalcontainer > evaloutput > evaloutputentry.EVALFUNCTION {
74
+ font-style: italic;
75
+ }
76
+ evalcontainer > evaloutput > evaloutputentry.EVALUNDEFINED {
77
+ color: #9E9E9E;
78
+ }
79
+ evalcontainer > evaloutput > evaloutputentry.EVALERROR {
80
+ background: #f44336;
81
+ }
82
+ evalcontainer > evaloutput > evaloutputentry.EVALWARN.EVALSTRING {
83
+ color: black;
84
+ }
85
+ .close-button {
86
+ position: absolute;
87
+ top: 5px;
88
+ right: 5px;
89
+ background: rgba(255, 0, 0, 0.8);
90
+ color: white;
91
+ border: none;
92
+ padding: 5px;
93
+ cursor: pointer;
94
+ }
95
+ `;
96
+ document.head.appendChild(s);
97
+ }
98
+ if (!document.querySelector('evalcontainer')) {
99
+ s=document.createElement("evalcontainer");
100
+ s.setAttribute("draggable", "true"); // draggable属性を追加
101
+
102
+ // 右上の「×」ボタンを追加
103
+ var closeButton = document.createElement("button");
104
+ closeButton.className = "close-button";
105
+ closeButton.innerText = "×";
106
+ closeButton.onclick = function() {
107
+ evaljs.close();
108
+ };
109
+ s.appendChild(closeButton);
110
+
111
+ var output=document.createElement("evaloutput");
112
+ function createOutputEntry(words) {
113
+ var t=document.createElement("evaloutputentry");
114
+ t.innerHTML=words;
115
+ t.className='EVALLOG';
116
+ output.appendChild(t);
117
+ }
118
+ createOutputEntry('To close this window, do evaljs.close();');
119
+ createOutputEntry('To clear, do evaljs.clear();');
120
+ s.appendChild(output);
121
+
122
+ var textarea=document.createElement("textarea");
123
+ textarea.className='EVALTEXTAREA';
124
+ textarea.focus();
125
+ textarea.onkeypress=e=>{
126
+ if (e.keyCode===13&&!e.shiftKey) {
127
+ var t=document.createElement("evaloutputentry"),
128
+ evaloutput;
129
+ t.textContent=textarea.value;
130
+ output.appendChild(t);
131
+ t=document.createElement("evaloutputentry");
132
+ t.classList.add('EVALRESULT');
133
+ try {
134
+ evaloutput=eval(textarea.value);
135
+ switch (typeof evaloutput) {
136
+ case 'object':
137
+ evaloutput=JSON.stringify(evaloutput);
138
+ t.classList.add('EVALFUNCTION');
139
+ break;
140
+ case 'string':
141
+ evaloutput=`"${evaloutput}"`;
142
+ t.classList.add('EVALSTRING');
143
+ break;
144
+ case 'number':
145
+ case 'boolean':
146
+ t.classList.add('EVALNUMBER');
147
+ break;
148
+ case 'function':
149
+ evaloutput=evaloutput.toString();
150
+ t.classList.add('EVALFUNCTION');
151
+ break;
152
+ case 'undefined':
153
+ t.classList.add('EVALUNDEFINED');
154
+ break;
155
+ case 'symbol':
156
+ evaloutput=evaloutput.toString();
157
+ t.classList.add('EVALSTRING');
158
+ break;
159
+ }
160
+ } catch(e) {
161
+ evaloutput=e;
162
+ t.classList.add('EVALERROR');
163
+ }
164
+ t.textContent=evaloutput+'';
165
+ output.appendChild(t);
166
+ output.scrollTop=output.scrollHeight;
167
+ textarea.value='';
168
+ e.preventDefault();
169
+ return false;
170
+ }
171
+ };
172
+ s.appendChild(textarea);
173
+ document.body.appendChild(s);
174
+ evaljs={
175
+ window:s,
176
+ clear() {
177
+ while (output.hasChildNodes()) output.removeChild(output.lastChild);
178
+ },
179
+ close() {
180
+ document.body.removeChild(this.window);
181
+ }
182
+ };
183
+
184
+ // ドラッグ機能を実装
185
+ s.ondragstart = function(e) {
186
+ e.dataTransfer.setData('text/plain', null); // Firefox用にデータをセット
187
+ };
188
+
189
+ document.ondragover = function(e) {
190
+ e.preventDefault(); // ドロップを許可
191
+ };
192
+
193
+ document.ondrop = function(e) {
194
+ e.preventDefault();
195
+ // ドロップした位置にエディタを移動
196
+ const rect = s.getBoundingClientRect();
197
+ s.style.left = (e.clientX - rect.width / 2) + 'px';
198
+ s.style.top = (e.clientY - rect.height / 2) + 'px';
199
+ };
200
+
201
+ function merp(u,t) {
202
+ switch (typeof u) {
203
+ case 'object':
204
+ u=JSON.stringify(u);
205
+ t.classList.add('EVALFUNCTION');
206
+ break;
207
+ case 'string':
208
+ u=`"${u}"`;
209
+ t.classList.add('EVALSTRING');
210
+ break;
211
+ case 'number':
212
+ case 'boolean':
213
+ t.classList.add('EVALNUMBER');
214
+ break;
215
+ case 'function':
216
+ u=u.toString();
217
+ t.classList.add('EVALFUNCTION');
218
+ break;
219
+ case 'undefined':
220
+ t.classList.add('EVALUNDEFINED');
221
+ break;
222
+ case 'symbol':
223
+ u=u.toString();
224
+ t.classList.add('EVALSTRING');
225
+ break;
226
+ }
227
+ return u;
228
+ }
229
+ console.log=function(){
230
+ for (var i=0;i<arguments.length;i++) {
231
+ var t=document.createElement("evaloutputentry"),u=arguments[i];
232
+ t.classList.add('EVALLOG');
233
+ u=merp(u,t);
234
+ t.textContent=(u+'').replace(/\s/g,'\\u00a0');
235
+ output.appendChild(t);
236
+ }
237
+ };
238
+ console.warn=function(){
239
+ for (var i=0;i<arguments.length;i++) {
240
+ var t=document.createElement("evaloutputentry"),u=arguments[i];
241
+ t.classList.add('EVALLOG');
242
+ t.classList.add('EVALWARN');
243
+ u=merp(u,t);
244
+ t.textContent=(u+'').replace(/\s/g,'\\u00a0');
245
+ output.appendChild(t);
246
+ }
247
+ };
248
+ console.error=function(){
249
+ for (var i=0;i<arguments.length;i++) {
250
+ var t=document.createElement("evaloutputentry"),u=arguments[i];
251
+ t.classList.add('EVALLOG');
252
+ t.classList.add('EVALERROR');
253
+ u=merp(u,t);
254
+ t.textContent=(u+'').replace(/\s/g,'\\u00a0');
255
+ output.appendChild(t);
256
+ }
257
+ };
258
+ }
259
+ }());