Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions examples/chrome-applescript/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ class ChromeControlServer {
this.setupHandlers();
}

// Helper methods
escapeForAppleScript(str) {
if (typeof str !== "string") return str;
// Basic AppleScript string escaping
return str
.replace(/\\/g, "\\\\") // Escape backslashes first
.replace(/"/g, '\\"') // Then escape double quotes
.replace(/\n/g, "\\n") // Escape newlines
.replace(/\r/g, "\\r"); // Escape carriage returns
}

async checkChromeAvailable() {
try {
const script = 'tell application "Google Chrome" to return "available"';
Expand Down Expand Up @@ -289,10 +278,23 @@ class ChromeControlServer {
throw new Error("URL is required and must be a string");
}

const escapedUrl = this.escapeForAppleScript(url);
try {
new URL(url);
} catch (error) {
return {
content: [
{
type: "text",
text: `Error: url is not a valid url - ${error.message}`,
},
],
isError: true,
};
}

const script = new_tab
? `tell application "Google Chrome" to open location "${escapedUrl}"`
: `tell application "Google Chrome" to set URL of active tab of front window to "${escapedUrl}"`;
? `tell application "Google Chrome" to open location ${JSON.stringify(url)}`
: `tell application "Google Chrome" to set URL of active tab of front window to ${JSON.stringify(url)}`;

await this.executeAppleScript(script);
return {
Expand Down Expand Up @@ -644,16 +646,14 @@ class ChromeControlServer {
})();
`;

const escapedCode = this.escapeForAppleScript(wrappedCode);

const script =
safeTabId != null
? `
tell application "Google Chrome"
repeat with w in windows
repeat with t in tabs of w
if (id of t as string) is "${safeTabId}" then
set result to execute t javascript "${escapedCode}"
set result to execute t javascript ${JSON.stringify(wrappedCode)}
return result
end if
end repeat
Expand All @@ -663,7 +663,7 @@ class ChromeControlServer {
`
: `
tell application "Google Chrome"
execute active tab of front window javascript "${escapedCode}"
execute active tab of front window javascript ${JSON.stringify(wrappedCode)}
end tell
`;

Expand Down
Loading