Jake.codes

OpenIn, JavaScript Bookmarklet

Friday, January 24, 2014

This bookmarklet detects what page you are on and opens it in the proper app.

Bookmarklet

javascript:var%20b%3Dlocation.href%3Bif%28b.indexOf%28%22gist.github.com%22%29%21%3D%3D-1%29%7Bb%3D%22pythonista%3A//InstallGist%3Faction%3Drun%26argv%3D%22%2Bb%7Delse%20if%28b.indexOf%28%22github.com%22%29%21%3D%3D-1%29%7Bb%3Db.replace%28%22http%3A//%22%2C%22ioc%3A//%22%29%3Bb%3Db.replace%28%22https%3A//%22%2C%22ioc%3A//%22%29%7Delse%20if%28b.indexOf%28%22twitter.com%22%29%21%3D%3D-1%29%7Bb%3Db.replace%28%22https%3A//twitter.com/%22%2C%22tweetbot%3A//%22%29%3Bb%3Db.replace%28%22statuses%22%2C%22status%22%29%3Bb%3Db.replace%28%22http%3A//twitter.com/%22%2C%22tweetbot%3A//%22%29%3Bb%3Db.replace%28%22http%3A//mobile.twitter.com/%22%2C%22tweetbot%3A//%22%29%3Bb%3Db.replace%28%22https%3A//mobile.twitter.com/%22%2C%22tweetbot%3A//%22%29%7Delse%20if%28b.indexOf%28%22wikia.com%22%29%21%3D%3D-1%7C%7Cb.indexOf%28%22wikipedia.org%22%29%21%3D%3D-1%7C%7Cb.indexOf%28%22wiki.%22%29%21%3D%3D-1%7C%7Cb.indexOf%28%22wiki/%22%29%21%3D%3D-1%29%7Bb%3Db.replace%28%22http%3A//%22%2C%22wplink%3A//%22%29%3Bb%3Db.replace%28%22https%3A//%22%2C%22wplink%3A//%22%29%7Delse%20if%28b.indexOf%28%22bing.com%22%29%21%3D%3D-1%7C%7Cb.indexOf%28%22google.com%22%29%21%3D%3D-1%7C%7Cb.indexOf%28%22yahoo.com%22%29%21%3D%3D-1%29%7Bb%3D%22https%3A//duckduckgo.com/%22%2Bb.match%28/%28%5C%3F%7C%5C%26%29%28q%7Cp%29%3D.%2B/g%29%5B0%5D.replace%28%22%3Fp%22%2C%22%3Fq%22%29%7Delse%20if%28b.indexOf%28%22imdb.com%22%29%21%3D%3D-1%29%7Bb%3Db.replace%28/http%28s%29%3F%3A%5C/%5C/%28www.%7Cm.%29%3Fimdb.com%5C//%2C%22imdb%3A///%22%29%7Delse%20if%28b.indexOf%28%22.zip%22%29%21%3D%3D-1%7C%7Cb.indexOf%28%22.rar%22%29%21%3D%3D-1%7C%7Cb.indexOf%28%22.torrent%22%29%21%3D%3D-1%29%7Bb%3D%22r%22%2Bb%7Delse%20if%28b.indexOf%28%22.tumblr.com%22%29%21%3D%3D-1%29%7Bvar%20tumblrUser%3Db.replace%28%22http%3A//%22%2C%22%22%29.replace%28/%5C.tumblr%5C.com%5C/%3F%28.%2B%29%3F/g%2C%22%22%29%2CtumblrPost%3D%22%22%3Bif%28b.indexOf%28%22/post/%22%29%21%3D%3D-1%29%7BtumblrPost%3Db.match%28/post%5C/%5Cd%2B/%29%5B0%5D.replace%28%22post/%22%2C%22%22%29%7Db%3D%22tumblr%3A//x-callback-url/blog%3FblogName%3D%22%2BtumblrUser%2B%22%26postID%3D%22%2BtumblrPost%7Dlocation.href%3Db%3B

Bookmarklet Source

var b = location.href;

if (b.indexOf('gist.github.com') !== -1) {
    // Install Gist in Pythonista
    // See: http://jake.codes/installgist/
    b = 'pythonista://InstallGist?action=run&argv=' + b;
} else if (b.indexOf('github.com') !== -1) {
    // Open in iOctocat
    b = b.replace('http://','ioc://');
    b = b.replace('https://','ioc://');
} else if (b.indexOf('twitter.com') !== -1) {
    // Open in Tweetbot
    b = b.replace('https://twitter.com/', 'tweetbot://');
    b = b.replace('statuses', 'status');
    b = b.replace('http://twitter.com/', 'tweetbot://');
    b = b.replace('http://mobile.twitter.com/', 'tweetbot://');
    b = b.replace('https://mobile.twitter.com/', 'tweetbot://');
} else if (b.indexOf('wikia.com') !== -1 ||
         b.indexOf('wikipedia.org') !== -1 ||
         b.indexOf('wiki.') !== -1 ||
         b.indexOf('wiki/') !== -1) {
    // Open in Wikipanion
    b = b.replace('http://','wplink://');
    b = b.replace('https://','wplink://');
} else if (b.indexOf('bing.com') !== -1 ||
         b.indexOf('google.com') !== -1 ||
         b.indexOf('yahoo.com') !== -1) {
    // Searches on Safari's Default Search Engines reroute' through DuckDuckGo
    b = 'https://duckduckgo.com/' + b.match(/(\?|\&)(q|p)=.+/g)[0].replace('?p', '?q');
} else if (b.indexOf('imdb.com') !== -1) {
    // Open in IMDB
    b = b.replace(/http(s)?:\/\/(www.|m.)?imdb.com\//, 'imdb:///');
} else if (b.indexOf('.zip') !== -1 ||
         b.indexOf('.rar') !== -1 ||
         b.indexOf('.torrent') !== -1) {
    // Open in Documents
    b = 'r' + b;
} else if (b.indexOf('.tumblr.com') !== -1) {
    // Open Tumblr blog or post in Tumblr
    var tumblrUser = b.replace('http://','').replace(/\.tumblr\.com\/?(.+)?/g,''),
    tumblrPost = '';
    
    if (b.indexOf('/post/') !== -1) {
        tumblrPost = (b.match(/post\/\d+/)[0]).replace('post/','');
    }
    
    b = 'tumblr://x-callback-url/blog?blogName=' + tumblrUser + '&postID=' + tumblrPost;
}

location.href = b;