soiz1 commited on
Commit
547546f
·
verified ·
1 Parent(s): c10a59b

Update src/addons/addons/debug-console/userscript.js

Browse files
src/addons/addons/debug-console/userscript.js CHANGED
@@ -24,71 +24,83 @@ export default async ({ addon }) => {
24
  if (window.__injectedConsole) return;
25
  window.__injectedConsole = true;
26
 
27
- const style = `
28
- #floatingConsole {
29
- position: fixed;
30
- bottom: 20px;
31
- right: 20px;
32
- width: 400px;
33
- max-height: 60vh;
34
- background: #111;
35
- color: #0f0;
36
- font-family: monospace;
37
- font-size: 14px;
38
- border: 1px solid #0f0;
39
- border-radius: 8px;
40
- box-shadow: 0 0 10px #0f0;
41
- z-index: 999999;
42
- display: flex;
43
- flex-direction: column;
44
- resize: both;
45
- overflow: hidden;
46
- }
47
- #floatingConsoleHeader {
48
- background: #222;
49
- cursor: move;
50
- padding: 5px 10px;
51
- user-select: none;
52
- display: flex;
53
- justify-content: space-between;
54
- align-items: center;
55
- }
56
- #floatingConsoleHeader button {
57
- background: transparent;
58
- color: #0f0;
59
- border: none;
60
- font-weight: bold;
61
- margin-left: 5px;
62
- cursor: pointer;
63
- }
64
- #floatingConsoleLog {
65
- flex: 1;
66
- overflow-y: auto;
67
- padding: 10px;
68
- border-top: 1px solid #0f0;
69
- border-bottom: 1px solid #0f0;
70
- background: #000;
71
- }
72
- #floatingConsoleInput {
73
- display: flex;
74
- border-top: 1px solid #0f0;
75
- }
76
- #floatingConsoleInput input {
77
- flex: 1;
78
- background: #000;
79
- color: #0f0;
80
- border: none;
81
- padding: 10px;
82
- font-family: monospace;
83
- }
84
- #floatingConsoleInput button {
85
- background: #333;
86
- color: #0f0;
87
- border: none;
88
- padding: 10px;
89
- cursor: pointer;
90
- }
91
- `;
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  const css = document.createElement("style");
94
  css.textContent = style;
@@ -96,42 +108,49 @@ export default async ({ addon }) => {
96
 
97
  const el = document.createElement("div");
98
  el.id = "floatingConsole";
99
- el.innerHTML = `
100
- <div id="floatingConsoleHeader">
101
- <span id="consoleTitle">🖥 Console</span>
102
- <div>
103
- <button id="minBtn">∧</button>
104
- <button id="closeBtn">×</button>
105
- </div>
106
- </div>
107
- <div id="floatingConsoleLog">[Console Ready]</div>
108
- <div id="floatingConsoleInput">
109
- <input id="floatingConsoleCmd" placeholder="JavaScriptを入力..." />
110
- <button id="execBtn">実行</button>
111
- </div>
112
- `;
113
  document.body.appendChild(el);
114
 
115
  const logBox = el.querySelector("#floatingConsoleLog");
116
  const input = el.querySelector("#floatingConsoleCmd");
117
 
118
- const log = (msg) => {
119
- const div = document.createElement("div");
120
- div.textContent = "> " + msg;
121
- logBox.appendChild(div);
122
- logBox.scrollTop = logBox.scrollHeight;
123
- };
 
 
 
 
 
 
 
124
 
125
- el.querySelector("#execBtn").onclick = () => {
126
- const code = input.value;
127
- input.value = "";
128
- try {
129
- const result = eval(code);
130
- log("結果: " + result);
131
- } catch (e) {
132
- log("エラー: " + e);
133
- }
134
- };
135
 
136
  el.querySelector("#closeBtn").onclick = () => {
137
  el.remove();
@@ -184,21 +203,32 @@ export default async ({ addon }) => {
184
  });
185
  })(el, header);
186
 
187
- // console.* フック
188
- const hookConsole = (name) => {
189
- const original = console[name];
190
- console[name] = function (...args) {
191
- original.apply(console, args);
192
- log("[" + name + "] " + args.join(" "));
193
- };
194
- };
195
- ["log", "warn", "error"].forEach(hookConsole);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
- window.onerror = (msg, src, line, col, err) => {
198
- log("[onerror] " + msg + " @ " + line + ":" + col);
199
- };
200
- window.addEventListener("unhandledrejection", (e) => {
201
- log("[unhandledrejection] " + e.reason);
202
- });
203
  }
204
  };
 
24
  if (window.__injectedConsole) return;
25
  window.__injectedConsole = true;
26
 
27
+ const style = `
28
+ #floatingConsole {
29
+ position: fixed;
30
+ bottom: 20px;
31
+ right: 20px;
32
+ width: 400px;
33
+ max-height: 60vh;
34
+ background: #111;
35
+ color: #0f0;
36
+ font-family: monospace;
37
+ font-size: 14px;
38
+ border: 1px solid #0f0;
39
+ border-radius: 8px;
40
+ box-shadow: 0 0 10px #0f0;
41
+ z-index: 999999;
42
+ display: flex;
43
+ flex-direction: column;
44
+ resize: both;
45
+ overflow: hidden;
46
+ }
47
+ #floatingConsoleHeader {
48
+ background: #222;
49
+ cursor: move;
50
+ padding: 5px 10px;
51
+ user-select: none;
52
+ display: flex;
53
+ justify-content: space-between;
54
+ align-items: center;
55
+ }
56
+ #floatingConsoleHeader button {
57
+ background: transparent;
58
+ color: #0f0;
59
+ border: none;
60
+ font-weight: bold;
61
+ margin-left: 5px;
62
+ cursor: pointer;
63
+ }
64
+ #floatingConsoleLog {
65
+ flex: 1;
66
+ overflow-y: auto;
67
+ padding: 10px;
68
+ border-top: 1px solid #0f0;
69
+ border-bottom: 1px solid #0f0;
70
+ background: #000;
71
+ }
72
+ .log-info {
73
+ color: #0f0;
74
+ }
75
+ .log-warn {
76
+ color: #ff0;
77
+ }
78
+ .log-error {
79
+ color: #f00;
80
+ }
81
+ #floatingConsoleInput {
82
+ display: flex;
83
+ border-top: 1px solid #0f0;
84
+ }
85
+ #floatingConsoleInput textarea {
86
+ flex: 1;
87
+ background: #000;
88
+ color: #0f0;
89
+ border: none;
90
+ padding: 10px;
91
+ font-family: monospace;
92
+ resize: none;
93
+ height: 80px;
94
+ }
95
+ #floatingConsoleInput button {
96
+ background: #333;
97
+ color: #0f0;
98
+ border: none;
99
+ padding: 10px;
100
+ cursor: pointer;
101
+ white-space: nowrap;
102
+ }
103
+ `;
104
 
105
  const css = document.createElement("style");
106
  css.textContent = style;
 
108
 
109
  const el = document.createElement("div");
110
  el.id = "floatingConsole";
111
+ el.innerHTML = `
112
+ <div id="floatingConsoleHeader">
113
+ <span id="consoleTitle">🖥 Console</span>
114
+ <div>
115
+ <button id="minBtn">∧</button>
116
+ <button id="closeBtn">×</button>
117
+ </div>
118
+ </div>
119
+ <div id="floatingConsoleLog">[Console Ready]</div>
120
+ <div id="floatingConsoleInput">
121
+ <textarea id="floatingConsoleCmd" placeholder="JavaScriptを入力...(複数行対応)"></textarea>
122
+ <button id="execBtn">実行</button>
123
+ </div>
124
+ `;
125
  document.body.appendChild(el);
126
 
127
  const logBox = el.querySelector("#floatingConsoleLog");
128
  const input = el.querySelector("#floatingConsoleCmd");
129
 
130
+ const log = (msg, type = "info") => {
131
+ const div = document.createElement("div");
132
+ div.className = `log-${type}`;
133
+ div.textContent = "> " + msg;
134
+ logBox.appendChild(div);
135
+ // 区切り線を追加
136
+ const hr = document.createElement("hr");
137
+ hr.style.border = "0";
138
+ hr.style.borderTop = "1px solid #0f0";
139
+ hr.style.margin = "2px 0";
140
+ logBox.appendChild(hr);
141
+ logBox.scrollTop = logBox.scrollHeight;
142
+ };
143
 
144
+ el.querySelector("#execBtn").onclick = () => {
145
+ const code = input.value;
146
+ input.value = "";
147
+ try {
148
+ const result = eval(code);
149
+ log("結果: " + result, "info");
150
+ } catch (e) {
151
+ log("エラー: " + e, "error");
152
+ }
153
+ };
154
 
155
  el.querySelector("#closeBtn").onclick = () => {
156
  el.remove();
 
203
  });
204
  })(el, header);
205
 
206
+ // consoleフックもタイプ別に変更
207
+ const hookConsole = (name) => {
208
+ const original = console[name];
209
+ console[name] = function (...args) {
210
+ original.apply(console, args);
211
+ let type = "info";
212
+ if (name === "warn") type = "warn";
213
+ if (name === "error") type = "error";
214
+ log(`[${name}] ` + args.join(" "), type);
215
+ };
216
+ };
217
+ ["log", "warn", "error"].forEach(hookConsole);
218
+
219
+ // window.onerrorのログ
220
+ window.onerror = (msg, src, line, col, err) => {
221
+ log(`[onerror] ${msg} @ ${line}:${col}`, "error");
222
+ };
223
+ window.addEventListener("unhandledrejection", (e) => {
224
+ log(`[unhandledrejection] ${e.reason}`, "error");
225
+ });
226
+ input.addEventListener("keydown", (e) => {
227
+ if (e.key === "Enter" && e.ctrlKey && e.shiftKey) {
228
+ e.preventDefault(); // 改行を防ぐ(必要なら外してもOK)
229
+ el.querySelector("#execBtn").click();
230
+ }
231
+ });
232
 
 
 
 
 
 
 
233
  }
234
  };