-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.js
More file actions
29 lines (27 loc) · 1 KB
/
Copy pathinject.js
File metadata and controls
29 lines (27 loc) · 1 KB
1
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
// Runs in the PAGE's own JS context (world: "MAIN"), so it can see the
// same `window.monaco` global that LeetCode's editor uses. Content scripts
// normally run in an isolated world and can't see this, so we bridge the
// two worlds with CustomEvents on `window`.
(function () {
function getMonacoCode() {
try {
if (!window.monaco || !window.monaco.editor) return null;
const models = window.monaco.editor.getModels();
if (!models || models.length === 0) return null;
// LeetCode usually keeps a single active model for the code editor.
const model = models[0];
return {
code: model.getValue(),
language: model.getLanguageId ? model.getLanguageId() : (model._languageId || "unknown"),
};
} catch (err) {
return null;
}
}
window.addEventListener("LC_ANALYZER_REQUEST_CODE", function () {
const result = getMonacoCode();
window.dispatchEvent(
new CustomEvent("LC_ANALYZER_RESPONSE_CODE", { detail: result })
);
});
})();