Android and Linux

Tuesday, April 5, 2011

Get current url from Android browser

I've been using an app called Wave Launcher, which allows you to swipe a certain area of the screen to pop up a launch bar. The launch bar contains 5 icons which you can set up to open an app or shortcut. With Folder Organizer, you can have it open a folder of apps, or, even cooler, a folder full of Tasker tasks.

I wanted to create a Tasker task to open my computer's browser to the same url that my phone's browser is on. I can do this by copying the url and running a task to share it, but that's too much work. After much digging, I found a way to extract the current url from the browser database.
sqlite3 /data/data/ "SELECT * FROM bookmarks;" | tr '|' '\n' | grep http | tail -n1

If you go to on the phone and run that command, the output is

Using that, I can make a short script to make it open on my home computer via ssh:

#! /system/bin/sh
url=$(sqlite3 /data/data/ "SELECT * FROM bookmarks;" | tr '|' '\n' | grep http | tail -n1)
ssh -i /PATH/TO/SSHKEY USER@IP "firefox $url"

If I'm browsing a page on the phone and want to read it on the computer instead, I can swipe Wave Launcher and select the task that runs this command. This will grab the last url visited, so it doesn't matter if the browser is open or closed.