Content script: Eruda injection + console bridge
Browse files
app/src/main/assets/devconsole/eruda-loader.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Nova Dev Console — content script.
|
| 3 |
+
* 1) Injects Eruda (bundled copy preferred, CDN fallback).
|
| 4 |
+
* 2) Bridges console.log / uncaught errors to the native app over
|
| 5 |
+
* GeckoView native messaging ("browser" native app).
|
| 6 |
+
*/
|
| 7 |
+
(function () {
|
| 8 |
+
"use strict";
|
| 9 |
+
|
| 10 |
+
// ---------- 1. Inject Eruda ----------
|
| 11 |
+
if (!window.__novaErudaInjected) {
|
| 12 |
+
window.__novaErudaInjected = true;
|
| 13 |
+
var CDN = "https://cdn.jsdelivr.net/npm/eruda/eruda.min.js";
|
| 14 |
+
var tryInject = function (src, fallback) {
|
| 15 |
+
var s = document.createElement("script");
|
| 16 |
+
s.src = src;
|
| 17 |
+
s.onload = function () {
|
| 18 |
+
if (window.eruda && typeof eruda.init === "function") eruda.init();
|
| 19 |
+
};
|
| 20 |
+
s.onerror = function () {
|
| 21 |
+
if (fallback) tryInject(fallback, null);
|
| 22 |
+
};
|
| 23 |
+
(document.head || document.documentElement).appendChild(s);
|
| 24 |
+
};
|
| 25 |
+
try {
|
| 26 |
+
tryInject(browser.runtime.getURL("eruda.min.js"), CDN);
|
| 27 |
+
} catch (e) {
|
| 28 |
+
tryInject(CDN, null);
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
// ---------- 2. Console bridge ----------
|
| 33 |
+
var send = function (payload) {
|
| 34 |
+
try {
|
| 35 |
+
browser.runtime.sendNativeMessage("browser", {
|
| 36 |
+
type: "console-log",
|
| 37 |
+
payload: payload,
|
| 38 |
+
});
|
| 39 |
+
} catch (e) {
|
| 40 |
+
// Privileged API unavailable (e.g. extension running outside the host) — ignore.
|
| 41 |
+
}
|
| 42 |
+
};
|
| 43 |
+
|
| 44 |
+
var stringify = function (args) {
|
| 45 |
+
return Array.prototype.map
|
| 46 |
+
.call(args, function (a) {
|
| 47 |
+
try {
|
| 48 |
+
return typeof a === "string" ? a : JSON.stringify(a);
|
| 49 |
+
} catch (err) {
|
| 50 |
+
return "[unprintable]";
|
| 51 |
+
}
|
| 52 |
+
})
|
| 53 |
+
.join(" ");
|
| 54 |
+
};
|
| 55 |
+
|
| 56 |
+
var origLog = console.log.bind(console);
|
| 57 |
+
console.log = function () {
|
| 58 |
+
origLog.apply(null, arguments);
|
| 59 |
+
send(stringify(arguments));
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
var origError = console.error.bind(console);
|
| 63 |
+
console.error = function () {
|
| 64 |
+
origError.apply(null, arguments);
|
| 65 |
+
send("[error] " + stringify(arguments));
|
| 66 |
+
};
|
| 67 |
+
|
| 68 |
+
window.addEventListener("error", function (ev) {
|
| 69 |
+
send("ERROR: " + ev.message + " @" + ev.filename + ":" + ev.lineno);
|
| 70 |
+
});
|
| 71 |
+
})();
|