@@ -33,17 +33,6 @@ class ChromeControlServer {
3333 this . setupHandlers ( ) ;
3434 }
3535
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-
4736 async checkChromeAvailable ( ) {
4837 try {
4938 const script = 'tell application "Google Chrome" to return "available"' ;
@@ -289,10 +278,23 @@ class ChromeControlServer {
289278 throw new Error ( "URL is required and must be a string" ) ;
290279 }
291280
292- const escapedUrl = this . escapeForAppleScript ( url ) ;
281+ try {
282+ new URL ( url ) ;
283+ } catch ( error ) {
284+ return {
285+ content : [
286+ {
287+ type : "text" ,
288+ text : `Error: url is not a valid url - ${ error . message } ` ,
289+ } ,
290+ ] ,
291+ isError : true ,
292+ } ;
293+ }
294+
293295 const script = new_tab
294- ? `tell application "Google Chrome" to open location " ${ escapedUrl } " `
295- : `tell application "Google Chrome" to set URL of active tab of front window to " ${ escapedUrl } " ` ;
296+ ? `tell application "Google Chrome" to open location ${ JSON . stringify ( url ) } `
297+ : `tell application "Google Chrome" to set URL of active tab of front window to ${ JSON . stringify ( url ) } ` ;
296298
297299 await this . executeAppleScript ( script ) ;
298300 return {
@@ -644,16 +646,14 @@ class ChromeControlServer {
644646 })();
645647 ` ;
646648
647- const escapedCode = this . escapeForAppleScript ( wrappedCode ) ;
648-
649649 const script =
650650 safeTabId != null
651651 ? `
652652 tell application "Google Chrome"
653653 repeat with w in windows
654654 repeat with t in tabs of w
655655 if (id of t as string) is "${ safeTabId } " then
656- set result to execute t javascript " ${ escapedCode } "
656+ set result to execute t javascript ${ JSON . stringify ( wrappedCode ) }
657657 return result
658658 end if
659659 end repeat
@@ -663,7 +663,7 @@ class ChromeControlServer {
663663 `
664664 : `
665665 tell application "Google Chrome"
666- execute active tab of front window javascript " ${ escapedCode } "
666+ execute active tab of front window javascript ${ JSON . stringify ( wrappedCode ) }
667667 end tell
668668 ` ;
669669
0 commit comments