diff --git a/browser/firefox/aliases.zsh b/browser/firefox/aliases.zsh new file mode 100644 index 00000000..20a5e724 --- /dev/null +++ b/browser/firefox/aliases.zsh @@ -0,0 +1,2 @@ +alias bm='ffbookmarks' +alias hist='ffhistory' diff --git a/browser/firefox/bin/ffbookmarks b/browser/firefox/bin/ffbookmarks new file mode 100755 index 00000000..ab3104ee --- /dev/null +++ b/browser/firefox/bin/ffbookmarks @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +from pyfzf import FzfPrompt +from lib.FirefoxProfile import FirefoxProfile + + +if __name__ == '__main__': + ff = FirefoxProfile.default() + choice = FzfPrompt().prompt(ff.bookmarks()) + if choice: + ff.open("http" + choice[0].split(", http")[1]) + else: + print("Aborted") diff --git a/browser/firefox/bin/ffhistory b/browser/firefox/bin/ffhistory new file mode 100755 index 00000000..84a68ca6 --- /dev/null +++ b/browser/firefox/bin/ffhistory @@ -0,0 +1,13 @@ +#!/usr/bin/env python + +from pyfzf import FzfPrompt +from lib.FirefoxProfile import FirefoxProfile + + +if __name__ == '__main__': + ff = FirefoxProfile.default() + choice = FzfPrompt().prompt(ff.history()) + if choice: + ff.open("http" + choice[0].split(", http")[1]) + else: + print("Aborted") diff --git a/browser/firefox/bin/lib/FirefoxProfile.py b/browser/firefox/bin/lib/FirefoxProfile.py new file mode 100644 index 00000000..c823df5b --- /dev/null +++ b/browser/firefox/bin/lib/FirefoxProfile.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +import sqlite3 as sqlite +import os +from glob import glob +from pyfzf import FzfPrompt +from sys import platform as _platform + +fzf = FzfPrompt() + +class FirefoxProfile: + def __init__(self, dbfile): + self.db = sqlite.connect(dbfile) + self.cur = self.db.cursor() + + @staticmethod + def default(): + dbfile = None + paths = ["~/.mozilla/firefox", "~/Library/Application Support/Firefox/Profiles"] + while paths: + path = os.path.expanduser(paths.pop()) + files = glob('{}/*.default/places.sqlite'.format(path)) + if files: + dbfile = files[0] + break + if not dbfile: + raise Exception("No places.sqlite file") + return FirefoxProfile(dbfile) + + def bookmarks(self): + self.cur.execute( + 'SELECT DISTINCT mb.title, mp.url ' + + 'FROM moz_bookmarks mb ' + + 'INNER JOIN moz_places mp ' + + 'ON mp.id = mb.fk ' + + 'WHERE mb.type = 1 AND mb.title IS NOT NULL ' + 'ORDER BY mp.frecency DESC' + ) + return self._process(self.cur.fetchall()) + + def history(self): + self.cur.execute( + 'SELECT DISTINCT title, url ' + + 'FROM moz_places ' + + 'WHERE title IS NOT NULL ' + 'ORDER BY last_visit_date DESC' + ) + return self._process(self.cur.fetchall()) + + def _process(self, data): + lookup = {} + result = [] + for row in data: + title = (row[0] or "").encode('utf-8').strip() + url = (row[1] or "").encode('utf-8').strip() + key = ", ".join((title, url)) + if key not in lookup and row[1].startswith("http"): + lookup[key] = 1 + result.append(key) + return result + + def open(self, url): + if _platform.startswith("linux"): + os.system('xdg-open "{}"'.format(url)) + elif _platform == "darwin": + os.system('open "{}"'.format(url)) + else: + raise Exception("What you doin' fool") + + def close(self): + self.db.close() diff --git a/browser/firefox/bin/lib/__init__.py b/browser/firefox/bin/lib/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/browser/firefox/install b/browser/firefox/install new file mode 100755 index 00000000..4c59e809 --- /dev/null +++ b/browser/firefox/install @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +case $OSTYPE in + darwin*) + brew cask install firefox + ;; + linux*) + if [[ -f /etc/arch-release ]]; then + sudo pacman --needed --noconfirm -S firefox + fi + ;; +esac