Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.