is there a way to get the url from current tab in google-chrome?

9,977

Solution 1

I had the same issue and got through there while trying to resolve it, so I'll post my solution (which is pretty bad) here.

I use wmctrl (you could use xprop instead) and xdotool to do this. Previously, I used an extension to make the URL visible in the title bar (you can then access the url via xprop or wmctrl). Both methods work pretty well though it's not really "clean".

id=$(wmctrl -l | grep -oP "(?<=)(0x\w+)(?=.*Chromium)") //Put here the regex for the browser you use
xdotool key --window $id "ctrl+l"
xdotool key --window $id "ctrl+c"

You now have the url in your clipboard. I then use xclip to work with the URL.

I'd love to see a real solution to this if anyone find one.

Solution 2

Could you tell the error, that you get after execution, because the script is working for me. There is the same question here, answered using php and perl:

Output URL of open firefox tabs in terminal

Since the main part in python, here is a purely python script, to do the same thing, try it as well:

    #!/usr/bin/python
    import json
    f = open("recovery.js","r")
    jdata = json.loads(f.read())
    f.close()

    number_of_selected_tab = jdata["windows"][0]["selected"]

    tab_number = 1
    for win in jdata.get("windows"):
        for tab in win.get("tabs"):
            if number_of_selected_tab == tab_number :
                tab_index = tab.get("index") - 1
                print tab.get("entries")[tab_index].get("url")
            tab_number = tab_number + 1

I replaced /home/username/.mozilla/firefox/xxxxx.default/sessionstore.js file with recovery.js. In my case ( Mozilla Firefox 44.0, openSUSE 13.1) the file is ~/.mozilla/firefox/*.default/sessionstore-backups/recovery.js. And last but not the least, script doesn't work if you have two instances of Firefox running.

Share:
9,977

Related videos on Youtube

Yunus
Author by

Yunus

a Hobbyist Programmer!

Updated on September 18, 2022

Comments

  • Yunus
    Yunus over 1 year

    I used to do that on firefox using a python command, but recently something changed and I can't get the URL anymore!

    • What used to work with firefox:

      #!/bin/bash
      current_tab_num () {
          python2 <<< $'import json\nf = open("/home/username/.mozilla/firefox/xxxxx.default/sessionstore.js", "r")\njdata = json.loads(f.read())\nf.close()\nprint str(jdata["windows"][0]["selected"])'
      }
      current_tab_url () {
          sed -n "$(current_tab_num)p" <(python2 <<< $'import json\nf = open("/home/username/.mozilla/firefox/xxxxx.default/sessionstore.js", "r")\njdata = json.loads(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")')
      }
      current_tab_url
      

    Can anyone tell me how to do this in firefox and/or chrome ?

    NOTE : I don't understand python, I just found those commands somewhere and used them in bash!

    • Admin
      Admin over 8 years
      Something like selenium will be more suitable for this
  • Gaurav Parashar
    Gaurav Parashar over 6 years
    What is the equivalent file for Chrome?