I am looking for a smart way to get active browser URL for a cross-platform electron app. I have found a solution using AppleScript for Mac OS Safari and Chrome but still no solution for Firefox, Windows, and Linux :(
A solution for Chrome and Safari on MacOS using AppleScript:
Using node-applescript I was able to get safari and chrome URLs. Unfortunately, Firefox does not support AppleScript and the way to obtain the URL is hacky and unusable in production. Also, I still have no clue how to get the URLs on Linux and Windows.
const scripts = {
'chrome': `tell application "Google Chrome" to get URL of active tab of front window as string`,
'vivaldi': `tell application "Vivaldi" to return URL of active tab of front window`,
'safari': `tell application "Safari" to return URL of front document as string`,
'firefox': `tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using command down
keystroke "c" using command down
end tell
delay 0.5
return the clipboard`,
}
Moving between window sources, whether it be local (file) or remote (URL) can be accomplished by just calling window. loadFile(...) or window. loadURL(...) , but only after the instance of the window has been created.
Electron uses Chromium and Node.js so you can build your app with HTML, CSS, and JavaScript.
How Electron works. Electron embeds Chromium and Node. js in its binary, enabling web developers to write desktop applications without writing native code. Electron implements a multi-process model composed of the main and renderer processes, which is similar to the Chromium browser.
For Linux try command wmctrl. if there is no title tag on the web page, wmctrl should show you full url in the title bar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With