Skip to content

Instantly share code, notes, and snippets.

@valeIT
Forked from nikcub/google-serp.user.js
Created May 25, 2016 16:19
Show Gist options
  • Select an option

  • Save valeIT/9fe40c8a758de6419439745b0cea270e to your computer and use it in GitHub Desktop.

Select an option

Save valeIT/9fe40c8a758de6419439745b0cea270e to your computer and use it in GitHub Desktop.

Revisions

  1. @nikcub nikcub created this gist Sep 14, 2012.
    67 changes: 67 additions & 0 deletions google-serp.user.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    // Google SERP URL rewrite
    //
    // User script will rewrite search engine results page for Google and place real
    // links to results rather than links that proxy back via google.
    //
    // So you go straight to the page when you click and you can copy/paste the link
    //
    // Install:
    // 1. Download to desktop/wherever
    // 2. Open Chrome extensions page (Window -> Extensions) or URL chrome://extensions
    // 3. Drag+drop this file into the extensions window
    //
    // Also compatable with Firefox + Greasemonkey
    //
    //
    // by nik cubrilovic - http://www.nikcub.com
    //
    // ==UserScript==
    // @name Google SERP URL rewrite
    // @namespace
    // @description rewrite URLs in google search result pages
    // @version 0.0.1
    // @include http://*.google.*/*
    // @include https://*.google.*/*
    // @include http://*.google.*.*/*
    // @include https://*.google.*.*/*
    // ==/UserScript==
    var search_results = document.evaluate("//div[@id='search']//h3/a", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    if(search_results.snapshotLength > 1) {
    for(var i=0; i<search_results.snapshotLength; i++) {
    var c = search_results.snapshotItem(i);
    c.removeAttribute('onmousedown');
    // c.setAttribute('href', rewrite_url(c.href));
    }
    }

    function rewrite_url(old_url) {
    var params = get_params(old_url);
    console.info(params);
    if('url' in params) {
    var new_url = params['url'];
    console.info('new url:', new_url);
    return new_url;
    }
    };

    function get_params(dest_url) {
    dest_url = dest_url.replace(/&amp;/g, '&');
    var params = dest_url.substr(dest_url.indexOf("?") + 1).split('&'),
    r = {};
    if (typeof params !== 'object' || params.length < 1) return false;
    for (var x = 0; x <= params.length; x++) {
    if (typeof params[x] == "string" && params[x].indexOf('=')) {
    var t = params[x].split('=');
    if (t instanceof Array) {
    var k = t[0];
    if (t.length > 1) {
    var z = t[1];
    r[k] = decodeURIComponent(z);
    } else {
    r[k] = '';
    }
    }
    }
    }
    return r;
    };