如何在 Linux/Mac/Windows 上的节点/电子应用程序中获取浏览器活动标签 URL?

Vas*_*hev 6 javascript linux firefox node.js electron

我正在寻找一种智能方法来获取跨平台电子应用程序的活动浏览器 URL。我找到了一个使用 AppleScript 的 Mac OS Safari 和 Chrome 解决方案,但仍然没有适用于 Firefox、Windows 和 Linux 的解决方案:(

在 MacOS 上使用 AppleScript 的 Chrome 和 Safari 解决方案:

使用node-applescript我能够获得 safari 和 chrome URL。不幸的是,Firefox 不支持 AppleScript,并且获取 URL 的方式很笨拙且无法在生产中使用。另外,我仍然不知道如何在 Linux 和 Windows 上获取 URL。

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`,
}
Run Code Online (Sandbox Code Playgroud)

小智 1

对于 Linux,请尝试命令 wmctrl。如果网页上没有标题标签,wmctrl 应该在标题栏中显示完整的 url。