Spaces:
Runtime error
Runtime error
Update src/addons/addons/debug-console/userscript.js
Browse files
src/addons/addons/debug-console/userscript.js
CHANGED
|
@@ -1,234 +1,31 @@
|
|
| 1 |
-
export default async ({ addon }) => {
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 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;
|
| 107 |
-
document.head.appendChild(css);
|
| 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を入力して実行...(実行ボタンかctrl+shift+enterで実行)"></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();
|
| 157 |
-
window.__injectedConsole = false;
|
| 158 |
-
};
|
| 159 |
-
|
| 160 |
-
let minimized = false;
|
| 161 |
-
const minBtn = el.querySelector("#minBtn");
|
| 162 |
-
const header = el.querySelector("#floatingConsoleHeader");
|
| 163 |
-
const title = el.querySelector("#consoleTitle");
|
| 164 |
-
|
| 165 |
-
minBtn.onclick = () => {
|
| 166 |
-
minimized = !minimized;
|
| 167 |
-
logBox.style.display = minimized ? "none" : "block";
|
| 168 |
-
el.querySelector("#floatingConsoleInput").style.display = minimized ? "none" : "flex";
|
| 169 |
-
title.style.display = minimized ? "none" : "inline";
|
| 170 |
-
el.querySelector("#closeBtn").style.display = minimized ? "none" : "inline";
|
| 171 |
-
minBtn.textContent = minimized ? "∨" : "∧";
|
| 172 |
-
|
| 173 |
-
if (minimized) {
|
| 174 |
-
el.style.width = "auto";
|
| 175 |
-
el.style.height = "auto";
|
| 176 |
-
el.style.overflow = "visible";
|
| 177 |
-
} else {
|
| 178 |
-
el.style.width = "400px";
|
| 179 |
-
el.style.maxHeight = "60vh";
|
| 180 |
-
}
|
| 181 |
-
};
|
| 182 |
-
|
| 183 |
-
// ドラッグ移動
|
| 184 |
-
(function makeDraggable(target, handle) {
|
| 185 |
-
let offsetX = 0, offsetY = 0, dragging = false;
|
| 186 |
-
handle.addEventListener("mousedown", (e) => {
|
| 187 |
-
dragging = true;
|
| 188 |
-
offsetX = e.clientX - target.offsetLeft;
|
| 189 |
-
offsetY = e.clientY - target.offsetTop;
|
| 190 |
-
document.body.style.userSelect = "none";
|
| 191 |
-
});
|
| 192 |
-
document.addEventListener("mousemove", (e) => {
|
| 193 |
-
if (dragging) {
|
| 194 |
-
target.style.left = (e.clientX - offsetX) + "px";
|
| 195 |
-
target.style.top = (e.clientY - offsetY) + "px";
|
| 196 |
-
target.style.right = "auto";
|
| 197 |
-
target.style.bottom = "auto";
|
| 198 |
}
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
dragging = false;
|
| 202 |
-
document.body.style.userSelect = "";
|
| 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 |
-
};
|
|
|
|
| 1 |
+
export default async ({ addon, console, msg }) => {
|
| 2 |
+
while (true) {
|
| 3 |
+
const targetElem = await addon.tab.waitForElement(
|
| 4 |
+
'div[class*="menu-bar_file-group"] > div:last-child:not(.sa-record)',
|
| 5 |
+
{ markAsSeen: true }
|
| 6 |
+
);
|
| 7 |
+
|
| 8 |
+
if (!document.querySelector('.sa-eruda-button')) {
|
| 9 |
+
const erudaButton = document.createElement("div");
|
| 10 |
+
erudaButton.className = "sa-eruda-button " + targetElem.className;
|
| 11 |
+
erudaButton.textContent = "開発者ツール";
|
| 12 |
+
erudaButton.style.cursor = "pointer";
|
| 13 |
+
erudaButton.style.marginLeft = "10px";
|
| 14 |
+
|
| 15 |
+
erudaButton.addEventListener("click", () => {
|
| 16 |
+
if (!window.eruda) {
|
| 17 |
+
const script = document.createElement('script');
|
| 18 |
+
script.src = 'https://cdn.jsdelivr.net/npm/eruda';
|
| 19 |
+
script.onload = () => {
|
| 20 |
+
eruda.init();
|
| 21 |
+
};
|
| 22 |
+
document.body.appendCh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