Last active
February 26, 2020 00:33
-
-
Save chadlavi/f5bfa39c1bd2a1f8470b3530173d6ec3 to your computer and use it in GitHub Desktop.
a greasemonkey script to remove bad UTM tags from link HREFs
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
const removeBadTags = (badTags) => { | |
const links = [...document.querySelectorAll('a')] | |
links.forEach( | |
(l) => { | |
badTags.forEach( | |
(b) => { | |
const badFirstRegex = RegExp(`[\?]${b}=[^\?&#]*`, 'gi') | |
const badSecondRegex = RegExp(`[&]${b}=[^\?&#]*`, 'gi') | |
if (l.href.match(badFirstRegex)) { | |
const hrefNoBad = l.href | |
.replace(badFirstRegex, '?') | |
.replace(/\?[\?&]/, '?') | |
.replace(/[\?&]\#/, '#') | |
.replace(/[\?&]$/, '') | |
l.href = hrefNoBad | |
} | |
if (l.href.match(badSecondRegex)) { | |
const hrefNoBad = l.href | |
.replace(badSecondRegex, '&') | |
.replace(/\&[\&]/, '&') | |
.replace(/[\?&]\#/, '#') | |
.replace(/[\?&]$/, '') | |
l.href = hrefNoBad | |
} | |
} | |
) | |
} | |
) | |
} | |
const badTags = [ | |
// put your bad UTM tags here | |
'badUTM', | |
] | |
removeBadTags(badTags) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment