12

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!

  • Something like selenium will be more suitable for this – cristi Nov 10 '15 at 16:31
6

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.

3

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.

  • What is the equivalent file for Chrome? – gaurav parashar Jan 21 '18 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.