Skip to content

Instantly share code, notes, and snippets.

@chadlavi
Last active February 26, 2020 00:33
Show Gist options
  • Save chadlavi/f5bfa39c1bd2a1f8470b3530173d6ec3 to your computer and use it in GitHub Desktop.
Save chadlavi/f5bfa39c1bd2a1f8470b3530173d6ec3 to your computer and use it in GitHub Desktop.
a greasemonkey script to remove bad UTM tags from link HREFs
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