Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get browser active tab URL in node/electron app on Linux/Mac/Windows?

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`,
}
like image 487
Vasil Enchev Avatar asked Nov 27 '17 11:11

Vasil Enchev


People also ask

How do I load links into electron app?

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.

Does electron use node JS?

Electron uses Chromium and Node.js so you can build your app with HTML, CSS, and JavaScript.

How does electron work 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.


1 Answers

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.

like image 67
Galin Tenev Avatar answered Oct 18 '22 13:10

Galin Tenev