Jake.codes

BangSearch, JavaScript Bookmarklet

Friday, January 24, 2014

This is a JavaScript fork of search_script.py from kstrauser which is a fork of BangSearch.py from Phil Gruneich

Bookmarklet

javascript:var%20s%3D%5B%5B%22s%22%2C%22http%3A//stackoverflow.com/search%3Fq%3D%22%5D%2C%5B%22comix%22%2C%22http%3A//www.comixology.com/search%3Fsearch%3D%22%5D%2C%5B%221p%22%2C%22onepassword%3A//search/%22%5D%2C%5B%22op%22%2C%22onepassword%3A//search/%22%5D%2C%5B%22w%22%2C%22wikipanion%3A///%22%5D%2C%5B%22wiki%22%2C%22wikipanion%3A///%22%5D%2C%5B%22tw%22%2C%22tweetbot%3A///search%3Fquery%3D%22%5D%2C%5B%22tb%22%2C%22tweetbot%3A///search%3Fquery%3D%22%5D%2C%5B%22imdb%22%2C%22imdb%3A///find%3Fq%3D%22%5D%2C%5B%22rd%22%2C%22rdio%3A//search/%22%5D%2C%5B%22app%22%2C%22http%3A//phobos.apple.com/WebObjects/MZSearch.woa/wa/search%3Fmedia%3Dsoftware%26term%3D%22%5D%5D%2Cq%3Dwindow.prompt%28%22BangSearch%22%2C%22%22%29%2Co%3D%22%22%3Bfor%28var%20i%3D0%3Bi%3Cs.length%3Bi%2B%2B%29%7Bvar%20b%3D%22%21%22%2Bs%5Bi%5D%5B0%5D%3Bif%28q.indexOf%28b%29%3D%3D-1%29continue%3Bq%3DencodeURIComponent%28q.replace%28b%2C%22%22%29.trim%28%29%29%3Bo%3Ds%5Bi%5D%5B1%5D%2Bq%3Bbreak%7Dif%28o%3D%3D%3D%22%22%29%7Bo%3D%22https%3A//duckduckgo.com/%3Fq%3D%22%2Bq%7Dlocation.href%3Do%3B

Bookmarklet Source

var s = [
    // Web services
    ['s', 'http://stackoverflow.com/search?q='],
    ['comix', 'http://www.comixology.com/search?search='],

    // Apps
    ['1p', 'onepassword://search/'],
    ['op', 'onepassword://search/'],
    ['w', 'wikipanion:///'],
    ['wiki', 'wikipanion:///'],
    ['tw', 'tweetbot:///search?query='],
    ['tb', 'tweetbot:///search?query='],
    ['imdb', 'imdb:///find?q='],
    ['rd', 'rdio://search/'],
    ['app', 'http://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?media=software&term=']
],
    q = window.prompt('BangSearch', ''),
    o = '';


for (var i = 0; i < s.length; i++) {
    var b = '!' + s[i][0];
    if (q.indexOf(b) == -1)
        continue;
    q = encodeURIComponent(q.replace(b,'').trim());
    o = s[i][1] + q;
    break;
}
if (o === '') {
    o = 'https://duckduckgo.com/?q='+q;
}

location.href = o;