0

I'm constantly opening a lot of tabs in my browser and sometimes I need to restart my computer and don't wanna lose the open tabs. So I've started working on a small project to save the urls of current open tabs in my browser and later restore them. I use Firefox as my default browser and I managed to write a simple shell script and a simpler Python script that combined allow me to save and restore the tabs. The bash script is:

#!/bin/bash

check_dependecies() {
    if ! pip freeze | grep -q lz4 ;then
        pip install lz4 --break-system-packages
    fi
}

save_pages() {
    if [[ $1 == 'firefox' ]];then
        export opentabs=$(find ~/ -name "recovery.jsonlz4" -type f);
        export browser='firefox';
    else
        echo "We don't have support for this browser yet."
        exit 1
    fi
    python3 amanuensis.py
}

restore_pages() {
    if [[ $1 == 'firefox' ]]; then
        while read -r line; do
            firefox -new-tab "$line" 2>/dev/null &
            sleep 1
        done < "saved_urls.txt"
    elif [[ $1 == 'chrome' ]]; then
        while read -r line; do
            google-chrome "$line" 2>/dev/null &
            sleep 1
        done < "saved_urls.txt"
    else
        echo "We don't have support for this browser yet."
    fi
}

check_dependecies

if [[ $# -lt 2 ]];then
    echo "Missing arguments. Usage: ./amanuensis.sh [mode] [browser]"
    echo "mode: 'save' for saving the urls | 'restore' to restore tabs"
    echo "browser: 'firefox' | 'chrome'"
    exit 1
elif [[ $1 == 'save' ]];then
    save_pages $2
elif [[ $1 == 'restore' ]];then
    restore_pages $2
else
    echo "Invalid input"
fi

And the Python script is:

import os, json, lz4.block

f = open(os.environ['opentabs'], 'rb')
browser = os.environ['browser']
print(browser)
magic = f.read(8)
jdata = json.loads(lz4.block.decompress(f.read()).decode('utf-8'))
f.close()

with open(f'saved_urls.txt', 'w') as file:
    for win in jdata['windows']:
        for tab in win['tabs']:
            i = int(tab['index']) - 1
            urls = tab['entries'][i]['url']
            file.write(f'{urls}\n')
            print(urls)

It's not a well written code but it does the job.

I showed my project to a couple friends of mine and they asked if I could implement a version to be used with Chrome. Heres's the problem: I'm able to restore the urls on Chrome but I can't save the urls from Chrome itself. With Firefox I can grab the content from the recovery.jsonlz4 but I couldn't find an equivalent for Chrome.

Can anyone help me to find a solution for this problem? Is there an equivalent file for Firefox's recovery.jsonlz4 in Chrome? If not, how can I get the urls os the current open tabs? Considering that this is a CLI application I don't want to work with Selenium or PyAutoGUI...

Thanks in advance!

  • See [How to continue where you left off Chrome, Edge, Firefox](https://pureinfotech.com/continue-where-left-off-chrome-edge-firefox/). – pjh Jun 26 '23 at 20:12
  • Does this answer your question? [Chrome on Linux - query the browser to see what tabs are open?](https://stackoverflow.com/questions/4968271/chrome-on-linux-query-the-browser-to-see-what-tabs-are-open) – Nic3500 Jun 26 '23 at 21:23
  • You can use an addon for this. [Tab Session Manager](https://chrome.google.com/webstore/detail/tab-session-manager/iaiomicjabeggjcfkbimgmglanimpnae) automatically and periodically stores tabs and windows. I use it on Firefox to save sessions as well as to restore tabs when I end up closing a window in a hurry. – user47 Jun 27 '23 at 05:35

0 Answers0