Skip to content

Instantly share code, notes, and snippets.

@cogumbreiro
Forked from bbqsrc/google-unmangler.user.js
Last active August 21, 2019 18:37
Show Gist options
  • Save cogumbreiro/f0e6d92a5f8cb5812eebbc426d3ca448 to your computer and use it in GitHub Desktop.
Save cogumbreiro/f0e6d92a5f8cb5812eebbc426d3ca448 to your computer and use it in GitHub Desktop.
Outlook / Google Search Unmangler - Greasemonkey Script
// ==UserScript==
// @name Google Search / Outlook /Gmail Unmangler
// @namespace URLUnmangle
// @description Gmail / Google Search URLs
// @include http://google.tld/*
// @include http://www.google.tld/*
// @include https://mail.google.tld/*
// ==/UserScript==
var URLUnmangle = {};
URLUnmangle.unmangleURL = function(url) {
var urlIndex = url.indexOf("url="), lastParam, unmangled;
if (urlIndex == -1) {
return url;
}
urlIndex += 4;
unmangled = url.substr(urlIndex);
lastParam = unmangled.indexOf('&');
unmangled = decodeURIComponent(unmangled.substring(0, lastParam));
return unmangled;
};
function textNodesUnder(el){
var n, result = "", walk = document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
while (n=walk.nextNode()) {
result += n.textContent;
}
return result;
}
URLUnmangle.findMangledURLs = function() {
var links = document.getElementsByTagName("a"), link, attrs;
for (var i = 0, ii = links.length; i < ii; ++i) {
link = links[i];
attrs = link.attributes;
for (var attr in attrs) {
if (attr == "href" || attr == "class") {
continue;
}
link.removeAttribute(attr);
}
const new_url = URLUnmangle.unmangleURL(link.href);
// Outlook rewrites plaintext URLs, which then Gmail picks up and renders
// them as a big encoded mess.
if (link.innerHTML.startsWith("http") && textNodesUnder(link) == link.href) {
link.innerHTML = new_url;
}
link.setAttribute('href', new_url);
}
};
window.addEventListener("load", function(e) {
unsafeWindow.rwt = function(){};
URLUnmangle.findMangledURLs();
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment