Last active
October 29, 2018 20:22
-
-
Save leonbrandt/3fe02a8aca3929d97c5a840370c4e052 to your computer and use it in GitHub Desktop.
Link-Cleaner (Greasemonkey / Tampermonkey)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Link-Cleaner | |
// @namespace https://www.leonbrandt.com | |
// @version 1.0 | |
// @description Press SHIFT + ALT + C to automatically clean Links | |
// @author Leon Brandt | |
// @homepage https://www.leonbrandt.com | |
// @updateURL https://gist.github.com/leonbrandt/3fe02a8aca3929d97c5a840370c4e052/raw/link-cleaner.user.js | |
// @match http*://*/* | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
/* | |
Supports: Amazon, Youtube, IMDb, mobile.de | |
*/ | |
function trimHost(host) { | |
var hostRegex = /(?:[\w-]+)?\.?([\w-]+)\.(?:[\w-]+)/g; | |
var match = hostRegex.exec(host); | |
return match[1]; | |
} | |
function remove_all_get_parameters(url) { | |
var href = url.href; | |
url.search = ""; | |
if(href === url.href) return null; | |
return url; | |
} | |
function amazon(url) { | |
var href = url.href; | |
var dp_detected = false; | |
var dp = null; | |
url.pathname.split("/").forEach(t => { | |
if(dp_detected){ | |
dp = t; | |
dp_detected = false; | |
} | |
if(t === "dp") dp_detected = true; | |
}); | |
if(dp == null) return null; | |
url.search = ""; | |
url.pathname = "/dp/" + dp; | |
if(href === url.href) return null; | |
return url; | |
} | |
function youtube(url) { | |
var href = url.href; | |
var v = url.searchParams.get("v"); | |
url.search = "?v=" + v; | |
if(href === url.href) return null; | |
return url; | |
} | |
function mobile(url) { | |
var href = url.href; | |
var id = url.searchParams.get("id"); | |
url.search = "?id=" + id; | |
if(href === url.href) return null; | |
return url; | |
} | |
function detect(url) { | |
switch(trimHost(url.host).toLowerCase()){ | |
case "amazon": | |
return amazon(url); | |
break; | |
case "youtube": | |
if(url.pathname.startsWith("/watch")) return youtube(url); | |
break; | |
case "imdb": | |
if(url.pathname.startsWith("/title")) return remove_all_get_parameters(url); | |
break; | |
case "mobile": | |
if(url.pathname.startsWith("/fahrzeuge/details.html")) return mobile(url); | |
if(url.pathname.startsWith("/auto-inserat")) return remove_all_get_parameters(url); | |
break; | |
} | |
return null; | |
} | |
function run() { | |
var target = detect(new URL(window.location.href)); | |
if(target != null) window.location = target; | |
} | |
(function() { | |
'use strict'; | |
document.onkeydown = function(evt){ | |
evt = evt || window.event; | |
if(evt.shiftKey && evt.altKey && evt.keyCode == 67){ | |
run(); | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment