Skip to content

Instantly share code, notes, and snippets.

@nikcub
Created September 14, 2012 07:21
Show Gist options
  • Save nikcub/3720482 to your computer and use it in GitHub Desktop.
Save nikcub/3720482 to your computer and use it in GitHub Desktop.
rewrite google search result page to link directly to search result url
// 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;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment