Jeb Wilkins

Coding and climbing in Cumbria.

Offline Web Browser

Whilst on my travels I’ve moved from using reader.google.com to liferea, and to using evolution, acccessing my gmail account via imap. This works great until I click on a webpage link - if I’m still in the internet cafe, no problem, otherwise I end up cutting and pasting the links into a text file to be read later.

This seemed something well suited to automation, in the end this turned out to be surprisingly easy - the dbus part took all of 5 minutes following the excellent tutorial at http://trac.luon.net/data/ruby-dbus/tutorial/basic_client.html

The first script is set as my default browser in gnome. It queries NetworkManager over dbus to check whether I’m online and either opens the link to cats it onto a text file, and sends a desktop notification to say its saved the link.

`

!/usr/bin/env ruby

require ‘dbus’

def online? bus = DBus::SystemBus.instance nm_service = bus.service(“org.freedesktop.NetworkManager”) nm = nm_service.object(‘/org/freedesktop/NetworkManager’) nm.introspect nm.default_iface = “org.freedesktop.NetworkManager” nm.state.first == 3 end

if /https?:\/\/localhost/ =~ ARGV[0] or online? system “/usr/bin/firefox ‘#{ARGV[0]}’ &” else File.open(“#{ENV[‘HOME’]}/.offline_links”, “a”) do |f| f.write “#{ARGV[0]}\n” end notify-send -t 3000 -i info 'Added Offline Link' '#{ARGV[0]}' end

`

The second script is hooked up to a button in my gnome-panel - this opens all the links in their own tabs in firefox, then removes the file of links.

`

!/usr/bin/env ruby

require ‘fileutils’

f = File.read “#{ENV[‘HOME’]}/.offline_links” links = f.map { |link| link.strip } system “/usr/bin/firefox #{links.join(‘ ’)} &” FileUtils.rm “#{ENV[‘HOME’]}/.offline_links” `