Google anti-evil sanitiser script for Opera
Note: This was tested on 2012-07-07 using Opera 12.00 on Linux.
As time goes on, Google becomes less and less of the simple, useful search engine that
it used to be, becomes more and more of a pain in the arse, and implements more and more
evil crap. The latest piece of evil crap that I noticed was it deciding to redirect all
search results links via Google, so it can monitor everything you use from a search.
To which I thought, "fuck this". So I wrote a UserJS for Opera (my preferred browser)
to put a stop to that, and also fix a bunch of other annoying crap.
This script does the following...
To use this script, simply copy and paste it into a file in your UserJS folder and
make sure UserJS is enabled.
Note that this script is for Opera. It won't work with Firefox... but I don't suppose
it would be too much trouble to convert it to run on Firefox, or any other decent
browser for that matter. Probably more awkward to make it run on Internet Explorer,
but then that's not a decent browser anyway, is it.
Use at your own risk... :)
Download the script
(function() {if (location.hostname.match(/google((\.[a-z]+)+)$/ig)) {
function checkStyleWidths(s, w) {
var i, p, r, v;
p = 0;
while ((i = s.indexOf('width', p)) != -1) {
p = i + 5;
r = eval('/(^.{' + p + '}[^:]*:)\s*([0-9]+)\s*(px[^;}]*[;}].*$)/');
v = parseInt(s.replace(r, '$2'));
if (v > w) s = s.replace(r, '$1' + w + '$3');
}
return s;
}
function getRealStyle(e, s) {
return (document.defaultView.getComputedStyle(e,null).getPropertyValue(s));
}
function bs(e) {
// Image thumbnails in web results pages work in a mindbogglingly stupid manner.
// The HTML inline data is for a 1x1px transparent GIF. Then some javascript runs
// to replace that with inline data for the actual image. Why not just put the
// real image inline data in the HTML to start with? Stupid. Anyway, the easiest
// way to make the thumbnails work is to allow that particular bit of javascript
// to run... after making sure they haven't sneaked anything else into it...
if (e.element.text.indexOf('(function(x){x&&(x.src=\'data:image/jpeg;base64') == 0) {
e.element.text = e.element.text.replace(/(\(function\(x\)\{x&&\(x\.src='data:image\/jpeg;base64,[A-Za-z0-9/+\\]*')[^']+getElementById\('([A-Za-z0-9-_]+)'.*$/g, '$1);})(document.getElementById(\'$2\'));');
return;
}
// Any other javascript... make sure it doesn't run!
e.preventDefault();
// Belt and braces!
e.element.text = '';
e.element.parentNode.removeChild(e.element);
}
function bl(f) {
f.preventDefault();
var a, b, c, d, e, i, j;
// Don't run on images etc
if (!(f.event.target instanceof Document)) return;
// Alert for embedded iframes - eg. /blank.html
if (window.location != window.parent.location) {
alert('Iframe loaded! UserJS cannot block iframes because it does not run until after they have loaded.\n\nPlease manually add this URL to the Blocked Content list for ' + window.parent.location.hostname + ':\n\n' + window.location);
return;
}
// Alert for having forgotten to turn cookies off
document.cookie = 'cookietest=set; path=/;'
c = document.cookie.split(',');
for (i=0; c[i]; i++) if (c[i].indexOf('cookietest=set') != -1) {
document.cookie = 'cookietest=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'
alert('You have forgotten to block cookies from Google! Check your cookie settings...');
break;
}
// Is this an image results page? If so, make sure it is the
// basic version and has safe search turned off
a = location.href;
if (a.match(/google((\.[a-z]+)+)\/search.*imghp/) || a.match(/images\.google((\.[a-z]+)+)\/search/)) {
a = a.replace(/(^.*\/)[a-z]+\.(google.*$)/, '$1images.$2');
if (a.indexOf('sout=') != -1) {
a = a.replace(/([?&]sout)=[^&]*/, '$1=1');
} else {
a = a + '&sout=1';
}
if (a.indexOf('safe=') != -1) {
a = a.replace(/([?&]safe)=[^&]*/, '$1=off');
} else {
a = a + '&safe=off';
}
if (location.href != a) location.href = a;
}
e = document.getElementsByTagName('*');
for (i=0; e[i]; i++) {
for (j=0; (a = e[i].attributes[j]); j++) {
// Delete all on(foo) attributes from all elements
if (a.name.match(/^on[a-z]+$/)) a.value = '';
// Make sure no style attributes are trying to be wider than the browser
if (a.name == 'style') a.value = checkStyleWidths(a.value, window.innerWidth);
}
a = e[i];
if (a.nodeName == 'A') {
// Make sure URLs are direct, and do not redirect via Google
if (a.href.match(/^[^?]*\/url\?q=/) || a.href.match(/^[^?]*\/url\?url=/)) {
a.href = a.href.replace(/&.*$/, '');
b = a.href.replace(/^[^?]*\/url\?q=/, '');
b = b.replace(/^[^?]*\/url\?url=/, '');
if (b != a.href) {
b = unescape(b.replace(/&.*$/, ''));
a.href = b;
}
}
// Make image links sane: Make the thumbnail link point directly to
// the original image, and make the site title for it a link to the
// image's referring page.
if (a.href.match(/^.*imgurl=([^\&]*).*$/)) {
b = document.createElement('a');
b.href = unescape(a.href.replace(/^.*imgrefurl=([^\&]*).*$/, '$1'));
// If it's on an image results page...
if (c = a.parentNode.getElementsByTagName('cite')[0]) {
b.appendChild(document.createTextNode(c.innerHTML));
c.parentNode.replaceChild(b, c);
} else {
// if it's an "Images for..." section on a web results page...
if (a.getElementsByTagName('img')[0]) {
c = b.href.substr(b.href.indexOf('://') + 3);
c = c.substring(0, c.indexOf('/'));
b.appendChild(document.createTextNode(c));
a.parentNode.appendChild(b);
// Also fix up the styles so you can actually see the link
c = parseInt(getRealStyle(b, 'line-height'));
d = c + parseInt(getRealStyle(a.parentNode, 'height')) + 'px';
b.style.fontSize = (c / 2) + 'px';
a.style.margin = '0 auto';
a.parentNode.style.width = 'auto';
a.parentNode.style.height = d;
a.parentNode.parentNode.style.height = d;
a.parentNode.parentNode.parentNode.style.height = d;
}
}
a.href = unescape(a.href.replace(/^.*imgurl=([^\&]*).*$/, '$1'));
}
// Make sure "Images" link goes to the basic version of the page
if ((a.href.match(/google((\.[a-z]+)+)\/.*(imghp|tbm=isch)/)) && (a.href.indexOf('sout=1') == -1)) {
a.href = a.href.replace(/(^.*\/)[a-z]+\.(google.*$)/, '$1images.$2&sout=1');
}
}
// Make sure no style attributes are trying to be wider than the browser
if (a.nodeName == 'STYLE') {
a.innerHTML = checkStyleWidths(a.innerHTML, window.innerWidth);
}
}
// Make sure "Cached" links are displayed
c = '.vshid { display: inline !important; margin-left: 10px; } .vshid a { color: #1122cc !important; } ';
// Rejigger column widths etc
if (b = document.getElementById('leftnav')) {
d = parseInt(getRealStyle(b, 'width'));
} else {
d = 132;
}
d += 15;
a = window.innerWidth - d - 50;
c = c + '#cnt #center_col, #cnt #foot, #cnt .ab_center_col { width:100%; margin-right: auto; margin-left: ' + d + 'px; max-width: ' + a + 'px; } ';
c = c + 'ul.rg_ul li { text-align: center; } ';
c = c + '.s { max-width: ' + a + 'px; } ';
// Remove ads
c = c + '.c, #rhs { display: none; } ';
a=document.createElement('style');
a.type = 'text/css';
a.appendChild(document.createTextNode(c));
document.getElementsByTagName('head')[0].appendChild(a);
// These have inline styles so can't be overridden with a style sheet...
if (b = document.getElementById('nav')) b.style.margin = '17px auto 0 ' + d + 'px';
if (b = document.getElementById('bfl')) b.style.textAlign = 'left';
if (b = document.getElementById('fll')) b.style.textAlign = 'left';
}
window.opera.addEventListener('BeforeScript', bs, true);
window.opera.addEventListener('BeforeEvent.DOMContentLoaded', bl, true);
}})();
Back to Pigeon's Nest
Be kind to pigeons