| 156 | |
| 157 | /** Install a script by injecting code into the Monaco editor and saving */ |
| 158 | export async function installScriptByCode(context: BrowserContext, extensionId: string, code: string): Promise<void> { |
| 159 | const page = await openEditorPage(context, extensionId); |
| 160 | // Wait for Monaco editor DOM and default template content to be ready |
| 161 | await focusMonacoEditor(page); |
| 162 | // Select all existing content |
| 163 | await page.keyboard.press("ControlOrMeta+a"); |
| 164 | // Capture current content fingerprint, then paste replacement |
| 165 | const initialText = await page.locator(".view-lines").textContent(); |
| 166 | await page.evaluate((text) => navigator.clipboard.writeText(text), code); |
| 167 | await page.keyboard.press("ControlOrMeta+v"); |
| 168 | // Wait for Monaco to finish rendering the pasted content (content will differ from template) |
| 169 | await page.waitForFunction((init) => document.querySelector(".view-lines")?.textContent !== init, initialText, { |
| 170 | timeout: 5_000, |
| 171 | }); |
| 172 | // Save |
| 173 | await saveCurrentEditor(context, extensionId, page); |
| 174 | await page.close(); |
| 175 | } |
| 176 | |
| 177 | /** A sample userscript for testing */ |
| 178 | export const sampleUserScript = `// ==UserScript== |