Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get url from browser #20

Closed
uffou opened this issue Nov 25, 2017 · 9 comments · Fixed by #63
Closed

Get url from browser #20

uffou opened this issue Nov 25, 2017 · 9 comments · Fixed by #63

Comments

@uffou
Copy link

uffou commented Nov 25, 2017

Any way we can get URL from a browser? At the moment it is getting the title of a webpage pretty accurately but I need the URL for my purposes.

app:"Google Chrome"
id:122
pid:1053
title:"New Issue · sindresorhus/active-win"
@sindresorhus
Copy link
Owner

That is app specific. On macOS, you could use AppleScript to get. On other systems, I have no idea.

https://stackoverflow.com/questions/2483033/get-the-url-of-the-frontmost-tab-from-chrome-on-os-x

@uffou
Copy link
Author

uffou commented Nov 27, 2017

Thanks for pointing me in the right direction!

I ended up using node applescript
which works perfectly. I am pasting my apple scripts incase someone else is looking for the same thing.

safari and chrome work perfectly. 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 = {
  'itunes': `tell application "iTunes" to get {name, artist, album} of current track`,
  'spotify': `tell application "Spotify" to get {name, artist, album} of current track`,
  '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`,
}

@starsoftmongolia
Copy link

@uffou Did you solve your your problem ? I also need to detect URL of active window in Windows.
If you have a solution, please share it ?

@tiangolo
Copy link
Contributor

There isn't a standard way to get the URL from a browser that works in all operating systems.

There are some possible "hacks" and scripts for macOS only.

Probably, the best way to solve it, while trying to be as "cross-platform" as possible, would be with a browser extension. That extension could talk to your Electron app via a local Electron (Node.js) server. Or it could talk directly to your backend server.

An extension is just JS. And I understand Firefox and Edge were about to support the same extension formats and APIs as Chrome, so you would probably be able to have only one code base. But I'm not aware of how that went.

Also, I understand that in Windows you can set a key in the registry that tells Chrome to install an extension the next time it opens, or something similar. I'm not sure how it works for macOS or Ubuntu (although Ubuntu is my main OS...)

@starsoftmongolia
Copy link

Ok, then i will try to use OCR solution. We can get top side screenshot of active window and try to detect characters from image, i think it will be easiest way to solve my problem. I only need website domain name.

@sindresorhus
Copy link
Owner

This is now available in the latest version: https://github.com/sindresorhus/active-win/releases/tag/v6.1.0

@bharat303
Copy link

Any solution for getting URL of current browser tab in windows ???

@creaz35
Copy link

creaz35 commented Jun 8, 2022

Following up on windows thanks!

@sachinsrinivas12
Copy link

Can anyone say how can I get URL for active window in "Windows"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants