@@ -126,13 +126,18 @@ func main() {
126126
127127 go checkForUpdates ()
128128
129+ // Set up test URL handler
130+ window .TestUrlHandler = func (url string ) {
131+ go TestURLInternal (url )
132+ }
133+
129134 const oneDay = 24 * time .Hour
130135
131136 var showingWindow bool = false
132137 timeoutChan := time .After (1 * time .Second )
133138 updateChan := time .After (oneDay )
134139
135- shouldKeepRunning = getKeepRunning ( )
140+ shouldKeepRunning = getConfigOption ( "keepRunning" , true )
136141 if shouldKeepRunning {
137142 timeoutChan = nil
138143 }
@@ -186,12 +191,13 @@ func main() {
186191 case <- configChange :
187192 startTime := time .Now ()
188193 var setupErr error
194+ slog .Debug ("Config has changed" )
189195 vm , setupErr = setupVM (cfw , embeddedFiles , namespace )
190196 if setupErr != nil {
191197 handleRuntimeError (setupErr )
192198 }
193199 slog .Debug ("VM refresh complete" , "duration" , fmt .Sprintf ("%.2fms" , float64 (time .Since (startTime ).Microseconds ())/ 1000 ))
194- shouldKeepRunning = getKeepRunning ( )
200+ shouldKeepRunning = getConfigOption ( "keepRunning" , true )
195201
196202 case shouldShowWindow := <- queueWindowOpen :
197203 if ! showingWindow && shouldShowWindow {
@@ -219,8 +225,9 @@ func main() {
219225 }
220226 }()
221227
222- showIcon := ! getHideIcon ()
223- C .RunApp (C .bool (forceWindowOpen ), C .bool (showIcon ), C .bool (shouldKeepRunning ))
228+ hideIcon := getConfigOption ("hideIcon" , false )
229+
230+ C .RunApp (C .bool (forceWindowOpen ), C .bool (! hideIcon ), C .bool (shouldKeepRunning ))
224231}
225232
226233func handleRuntimeError (err error ) {
@@ -229,32 +236,21 @@ func handleRuntimeError(err error) {
229236 go QueueWindowDisplay (1 )
230237}
231238
232- func getKeepRunning () bool {
233- if vm == nil {
234- return false
235- }
236-
237- keepRunning , err := vm .Runtime ().RunString ("finickyConfigAPI.getOption('keepRunning', finalConfig, true)" )
238- if err != nil {
239- return false
239+ func getConfigOption (optionName string , defaultValue bool ) bool {
240+ if vm == nil || vm .Runtime () == nil {
241+ slog .Debug ("VM not initialized, returning default for config option" , "option" , optionName , "default" , defaultValue )
242+ return defaultValue
240243 }
241- result := keepRunning .ToBoolean ()
242244
243- return result
244- }
245-
246- func getHideIcon () bool {
247- if vm == nil {
248- return false
249- }
245+ script := fmt .Sprintf ("finickyConfigAPI.getOption('%s', finalConfig, %t)" , optionName , defaultValue )
246+ optionVal , err := vm .Runtime ().RunString (script )
250247
251- hideIcon , err := vm .Runtime ().RunString ("finickyConfigAPI.getOption('hideIcon', finalConfig, false)" )
252248 if err != nil {
253- return false
249+ slog .Error ("Failed to get config option" , "option" , optionName , "error" , err )
250+ return defaultValue
254251 }
255- result := hideIcon .ToBoolean ()
256252
257- return result
253+ return optionVal . ToBoolean ()
258254}
259255
260256//export HandleURL
@@ -289,16 +285,59 @@ func HandleURL(url *C.char, name *C.char, bundleId *C.char, path *C.char, openIn
289285 }
290286}
291287
288+ //export TestURL
289+ func TestURL (url * C.char ) {
290+ urlString := C .GoString (url )
291+ TestURLInternal (urlString )
292+ }
293+
294+ func TestURLInternal (urlString string ) {
295+ slog .Debug ("Testing URL" , "url" , urlString )
296+
297+ if vm == nil {
298+ slog .Error ("VM not initialized" )
299+ window .SendMessageToWebView ("testUrlResult" , map [string ]interface {}{
300+ "error" : "Configuration not loaded" ,
301+ })
302+ return
303+ }
304+
305+ browserConfig , err := evaluateURL (vm .Runtime (), urlString , nil )
306+ if err != nil {
307+ slog .Error ("Failed to evaluate URL" , "error" , err )
308+ window .SendMessageToWebView ("testUrlResult" , map [string ]interface {}{
309+ "error" : err .Error (),
310+ })
311+ return
312+ }
313+
314+ if browserConfig == nil {
315+ window .SendMessageToWebView ("testUrlResult" , map [string ]interface {}{
316+ "error" : "No browser config returned" ,
317+ })
318+ return
319+ }
320+
321+ window .SendMessageToWebView ("testUrlResult" , map [string ]interface {}{
322+ "url" : browserConfig .URL ,
323+ "browser" : browserConfig .Name ,
324+ "openInBackground" : browserConfig .OpenInBackground ,
325+ "profile" : browserConfig .Profile ,
326+ "args" : browserConfig .Args ,
327+ })
328+ }
329+
292330func evaluateURL (vm * goja.Runtime , url string , opener * ProcessInfo ) (* browser.BrowserConfig , error ) {
293331 resolvedURL , err := shorturl .ResolveURL (url )
332+ vm .Set ("originalUrl" , url )
333+
294334 if err != nil {
295335 // Continue with original URL if resolution fails
296- slog .Info ("Failed to resolve short URL" , "error" , err )
297-
298- } else {
299- url = resolvedURL
336+ slog .Info ("Failed to resolve short URL" , "error" , err , "url" , url , "using" , resolvedURL )
300337 }
301338
339+ url = resolvedURL
340+
302341 vm .Set ("url" , resolvedURL )
303342
304343 if opener != nil {
@@ -313,7 +352,7 @@ func evaluateURL(vm *goja.Runtime, url string, opener *ProcessInfo) (*browser.Br
313352 slog .Debug ("No opener detected" )
314353 }
315354
316- openResult , err := vm .RunString ("finickyConfigAPI.openUrl(url, opener, finalConfig)" )
355+ openResult , err := vm .RunString ("finickyConfigAPI.openUrl(url, opener, originalUrl, finalConfig)" )
317356 if err != nil {
318357 return nil , fmt .Errorf ("failed to evaluate URL in config: %v" , err )
319358 }
@@ -427,11 +466,11 @@ func tearDown() {
427466}
428467
429468func setupVM (cfw * config.ConfigFileWatcher , embeddedFS embed.FS , namespace string ) (* config.VM , error ) {
430- shouldLogToFile := true
469+ logRequests := true
431470 var err error
432471
433472 defer func () {
434- err = logger .SetupFile (shouldLogToFile )
473+ err = logger .SetupFile (logRequests )
435474 if err != nil {
436475 slog .Warn ("Failed to setup file logging" , "error" , err )
437476 }
@@ -444,14 +483,12 @@ func setupVM(cfw *config.ConfigFileWatcher, embeddedFS embed.FS, namespace strin
444483 }
445484
446485 if currentBundlePath != "" {
447- vm , err := config .New (embeddedFS , namespace , currentBundlePath )
486+ vm , err = config .New (embeddedFS , namespace , currentBundlePath )
487+
448488 if err != nil {
449489 return nil , fmt .Errorf ("failed to setup VM: %v" , err )
450490 }
451491
452- // Update logging preference based on VM if available
453- shouldLogToFile = vm .ShouldLogToFile (false )
454-
455492 currentConfigState = vm .GetConfigState ()
456493
457494 if currentConfigState != nil {
@@ -461,48 +498,26 @@ func setupVM(cfw *config.ConfigFileWatcher, embeddedFS embed.FS, namespace strin
461498 DefaultBrowser : currentConfigState .DefaultBrowser ,
462499 ConfigPath : configPath ,
463500 }
464-
465- keepRunning := getKeepRunning ()
466- hideIcon := getHideIcon ()
467-
468- // Get logRequests option
469- logRequests := false
470- if logRequestsVal , err := vm .Runtime ().RunString ("finickyConfigAPI.getOption('logRequests', finalConfig, false)" ); err == nil {
471- logRequests = logRequestsVal .ToBoolean ()
472- }
473-
474- // Get checkForUpdate option
475- checkForUpdate := true
476- if checkForUpdateVal , err := vm .Runtime ().RunString ("finickyConfigAPI.getOption('checkForUpdate', finalConfig, true)" ); err == nil {
477- checkForUpdate = checkForUpdateVal .ToBoolean ()
478- }
479-
480- window .SendMessageToWebView ("config" , map [string ]interface {}{
481- "handlers" : configInfo .Handlers ,
482- "rewrites" : configInfo .Rewrites ,
483- "defaultBrowser" : configInfo .DefaultBrowser ,
484- "configPath" : configInfo .ConfigPath ,
485- "keepRunning" : keepRunning ,
486- "hideIcon" : hideIcon ,
487- "logRequests" : logRequests ,
488- "checkForUpdate" : checkForUpdate ,
489- })
490- } else if configInfo != nil {
491- keepRunning := getKeepRunning ()
492- hideIcon := getHideIcon ()
493-
494- window .SendMessageToWebView ("config" , map [string ]interface {}{
495- "handlers" : 0 ,
496- "rewrites" : 0 ,
497- "defaultBrowser" : "" ,
498- "configPath" : configInfo .ConfigPath ,
499- "keepRunning" : keepRunning ,
500- "hideIcon" : hideIcon ,
501- "logRequests" : false ,
502- "checkForUpdate" : true ,
503- })
504501 }
505502
503+ keepRunning := getConfigOption ("keepRunning" , true )
504+ hideIcon := getConfigOption ("hideIcon" , false )
505+ logRequests = getConfigOption ("logRequests" , false )
506+ checkForUpdates := getConfigOption ("checkForUpdates" , true )
507+
508+ window .SendMessageToWebView ("config" , map [string ]interface {}{
509+ "handlers" : configInfo .Handlers ,
510+ "rewrites" : configInfo .Rewrites ,
511+ "defaultBrowser" : configInfo .DefaultBrowser ,
512+ "configPath" : configInfo .ConfigPath ,
513+ "options" : map [string ]interface {}{
514+ "keepRunning" : keepRunning ,
515+ "hideIcon" : hideIcon ,
516+ "logRequests" : logRequests ,
517+ "checkForUpdates" : checkForUpdates ,
518+ },
519+ })
520+
506521 return vm , nil
507522 }
508523
0 commit comments