User Tools

Site Tools


keyword-bookmarks

Keyword Bookmarks

Description

A bookmarker that also handles keywords, like in vimperator with the -k switch. This makes browsing to your favorite website so much faster. You can map http://bbs.archlinux.org/search.php?action=show_24h to arch so when you type “g arch”, it loads the recent posts page on the arch forums.

Latest version http://gist.github.com/335989

Python

 
#!/usr/bin/python
# Vincent Kriek <vincent at vincentkriek dot nl>
# Mason Larobina <mason.larobina@gmail.com>

import sys
from os import environ as env, system
from os.path import join 
from textwrap import dedent
from urlparse import urlparse
from urllib import quote

XDG_DATA_DIR = env.get('XDG_DATA_HOME', join(env['HOME'], '.local/share/'))
DATA_DIR = join(XDG_DATA_DIR, 'uzbl/')

escape = lambda uri: uri.replace('@', '\\@')
#escape = lambda uri: uri.replace('@', '%40')
#escape = lambda uri: quote(uri, '.:/')

def addBookmark(url, keyword, title):
    print "HELLO GOT HERE"
    print join(DATA_DIR, 'keywordBookmarks.txt')
    fh = open(join(DATA_DIR, 'keywordBookmarks.txt'), 'a')
    fh.write("%s %s %s\n" % (url, keyword, title))
    fh.close()
    createBookmarkpage()

def getBookmark(keyword):
    fh = open(join(DATA_DIR, 'keywordBookmarks.txt'), 'r')
    for line in fh:
        (url, key) = line.split(' ', 2)[:2]
        if key == keyword:
            return url

def createBookmarkpage():
    fh = open(join(DATA_DIR, 'keywordBookmarks.html'), 'w')
    htmlStart = """
    <html>
    <head>
        <title>Bookmarks</title>
        <link href="bookmarks.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
        <h1>Bookmarks</h1>
    """
    fh.write(dedent(htmlStart))

    bookmarks = open(join(DATA_DIR, 'keywordBookmarks.txt'), 'r')
    for line in bookmarks:
        (url, keyword, title) = (line.strip().split(' ', 2) + ['',])[:3]
        urlp = urlparse(url)
        favicon = '%s://%s/favicon.ico' % (urlp.scheme, urlp.netloc)
        fh.write(dedent('''
            <p class="link"><img src="%s" />&nbsp;<a href="%s">%s</a>
            <span class="keyword">%s</span></p>
            ''' % (favicon, url, url, keyword)))

    fh.write('</body>\n</html>')
    fh.close()

# Read script args. 
for (index, value) in enumerate(sys.argv): print index, value
(config, pid, x_id, fifo, socket, uri, title) = sys.argv[1:8]
(action, keyword) = map(str.strip, (sys.argv[8:10] + [None,])[:2])

print "ACTION", action
# Add a bookmark
if action == "add":
    print "GOT HERE"
    assert keyword, "empty keyword"
    addBookmark(uri, keyword, title)

# Open a bookmark via keyword
elif action == "get":
    url = getBookmark(keyword)
    if url:
        system('echo "uri %s" > %r' % (escape(url), fifo))
        
elif action == "open":
    #Open the bookmark if found
    url = getBookmark(keyword)
    if url:
        system('echo "uri %s" > %r' % (escape(url), fifo))

    #Search for the words on google if it's not a url
    elif ' ' in keyword or '.' not in keyword:
        system('echo "uri http://www.google.com/search?q=%s" > %r'\
            % (quote(keyword, ''), fifo))

    #Open as url
    else:
        system('echo "uri %s" > %r' % (escape(keyword), fifo))

#Same as the previous elif but in a new window("tab")
elif action == "tab":
    #Open the bookmark if found
    url = getBookmark(keyword)
    if url:
        system('echo "event NEW_TAB %s" > %r' % (escape(url), fifo))

    #Search for the words on google if it's not a url
    elif ' ' in keyword or '.' not in keyword:
        system('echo "event NEW_TAB http://www.google.com/search?q=%s" > %r'\
            % (quote(keyword, ''), fifo))

    #Open as url
    else:
        system('echo "event NEW_TAB %s" > %r' % (escape(keyword), fifo))

Installation

This has to go in your uzbl/config

set kwbm = @scripts_dir/keyword_bookmarks.py
@cbind  a<add:>_  = @kwbm add %s
@cbind  g<get:>_  = @kwbm get %s
@cbind  t<tab:>_  = @kwbm tab %s
@cbind  o<open:>_  = @kwbm open %s

Quick Start

When the scripts gets a add, it adds a bookmark and when it sees a get, it navigates to the bookmark. Both times you have to supply a keyword, either for looking up or for adding.

The t and o first look if the entered string is a keyword, if it is, it opens it. If it isn't it checks if it could be a url, if it is, it opens it else it googles the arguments.

When you add a bookmark a page containing all bookmarks gets created. The bookmarks shortcut opens that page. You can customize it using css, $XDG_DATA_HOME/uzbl/bookmarks.css

keyword-bookmarks.txt · Last modified: 2016/08/31 14:10 (external edit)