| 17 | const RETRY_DELAY = 1000; // 1 second |
| 18 | |
| 19 | class ChromeControlServer { |
| 20 | constructor() { |
| 21 | this.server = new Server( |
| 22 | { |
| 23 | name: "chrome-applescript", |
| 24 | version: "0.1.0", |
| 25 | }, |
| 26 | { |
| 27 | capabilities: { |
| 28 | tools: {}, |
| 29 | }, |
| 30 | }, |
| 31 | ); |
| 32 | |
| 33 | this.setupHandlers(); |
| 34 | } |
| 35 | |
| 36 | // Helper methods |
| 37 | escapeForAppleScript(str) { |
| 38 | if (typeof str !== "string") return str; |
| 39 | // Basic AppleScript string escaping |
| 40 | return str |
| 41 | .replace(/\\/g, "\\\\") // Escape backslashes first |
| 42 | .replace(/"/g, '\\"') // Then escape double quotes |
| 43 | .replace(/\n/g, "\\n") // Escape newlines |
| 44 | .replace(/\r/g, "\\r"); // Escape carriage returns |
| 45 | } |
| 46 | |
| 47 | async checkChromeAvailable() { |
| 48 | try { |
| 49 | const script = 'tell application "Google Chrome" to return "available"'; |
| 50 | const result = await this.executeAppleScript(script); |
| 51 | return result === "available"; |
| 52 | } catch (error) { |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | async executeAppleScript(script, retries = MAX_RETRIES) { |
| 58 | for (let attempt = 0; attempt <= retries; attempt++) { |
| 59 | try { |
| 60 | const { stdout, stderr } = await execFileAsync( |
| 61 | "osascript", |
| 62 | ["-e", script], |
| 63 | { |
| 64 | timeout: APPLESCRIPT_TIMEOUT, |
| 65 | maxBuffer: 1024 * 1024, // 1MB buffer |
| 66 | }, |
| 67 | ); |
| 68 | if (stderr) { |
| 69 | console.error("AppleScript stderr:", stderr); |
| 70 | } |
| 71 | return stdout.trim(); |
| 72 | } catch (error) { |
| 73 | if (attempt === retries) { |
| 74 | console.error("AppleScript execution error after retries:", error); |
| 75 | throw new Error(`AppleScript error: ${error.message}`); |
| 76 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…