How to export Android Chrome tabs to an HTML file in Linux (as of February 2023)

Link post

Let’s say you have a few million tabs open in your mobile Chrome browser, because you never close anything, but now your browser is getting slow and laggy. You want to stick the URLs of those tabs somewhere for safekeeping so that you can close them all.

There’s a lot of advice on doing this on the Internet, most of which doesn’t work.

Here’s a method that does work. It’s a bit of a hack, but gives good results:

  • Enable developer tools on your Android phone: go to Settings → About phone, scroll down to “Build number”, and tap it repeatedly until it tells you you’re a developer. (Seriously.)

  • Enable USB debugging on your phone: go to Settings → System → Developer options and make sure the “USB debugging” slider is enabled.

  • Install Android Debug Tools on your Linux desktop: run these commands [h/​t this StackOverflow answer]:
    sudo apt install android-tools-adb android-tools-fastboot
    adb device

  • Open USB debugging page in desktop Chrome: go to chrome://​inspect/​#devices [h/​t this Android StackExchange answer]

  • Right-click → Inspect, or press F12

  • Go to the Sources tab. Edit inspect.js (top/​inspect/​inspect.js) and remove this URL-truncating code [h/​t this comment on that answer]:

  if (text.length > 100) {
   text = text.substring(0, 100) + '\u2026';
 }
  • Do not reload! Leave that page open.

  • Tether your phone by USB cable. If a phone pop-up asks you to authorize remote debugging, say yes.

  • You should now see a list of page titles and URLs on the USB debugging page in desktop Chrome.

  • Inspect the page again.

  • Under the Elements tab, navigate to <body>, then <div id="container">, then <div id="content">, then <div id=”devices” class=”selected”>. Right click that last one and Copy → Copy element.

  • Now you have all your tabs on your clipboard… all on the same line. This will crash a lot of text editors if you try to paste it normally. So we’ll use xclip instead.

  • If you don’t have it, sudo apt install xclip

  • Run xclip -selection c -o > my_tabs_file to put that huge HTML element in a file.

  • This’ll be easier with linebreaks, so run cat my_tabs_file | sed “s/​<div/​\n<div/​g” > my_better_tabs_file

  • Edit the paths at the beginning as appropriate, then run this Python script:

import re

# Put the actual path to the input file here:
INPUT_FILE = '/home/blah/blah/my_better_tabs_file'
# Put the desired path to the output file here:
OUTPUT_FILE = '/home/blah/blah/phone_tabs_list.html'

with open(TABSFILE) as f:
    lines = f.readlines()

prefix = """<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My Phone Tabs</title>
  </head>
  <body>"""
outlines = [prefix]

for line in lines:
    name_match = re.match(r'<div class="name">(.*)</div>\n', line)
    url_match = re.match(r'<div class="url">(.*)</div></div>\n', line)
    if name_match:
        name = name_match.group(1)
        outlines.append(f'<br/><br/><b>{name}</b>\n')
    elif url_match:
        url = url_match.group(1)
        outlines.append(f'<br/><a href="{url}">{url}</a>\n')
    elif 'class="name"' in line or 'class="url"' in line:
        raise ValueError(f'Could not parse line:\n{line}')

suffix = """  </body>
</html>"""
outlines.append(suffix)

with open(OUTPUT_FILE, 'w') as f:
     f.writelines(outlines)
  • If you get a ValueError or the file doesn’t look right, please tell me!

  • Open phone_tabs_list.html (or whatever you named it) in your desktop browser of choice. Confirm that it has a bunch of page titles and clickable URLs.

  • Enjoy!