Skip to content

Instantly share code, notes, and snippets.

@townivan
Last active June 17, 2021 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save townivan/08c5a495775faccba7f2a70dd34d8617 to your computer and use it in GitHub Desktop.
Save townivan/08c5a495775faccba7f2a70dd34d8617 to your computer and use it in GitHub Desktop.
Those annoying tracking pixels mess up my a11y scan. This is an attempt to add alt, role, and aria attributes to these guys. Uses MutationObserver to immediately update the img tags when they are added to the DOM by the tracking snippet's action.
// early in the head as possible
var observer = new MutationObserver(function(mutations){
for (var i=0; i < mutations.length; i++){
for (var j=0; j < mutations[i].addedNodes.length; j++){
checkNode(mutations[i].addedNodes[j]);
}
}
});
observer.observe(document.documentElement, {
childList: true,
subtree: true
});
checkNode = function(addedNode) {
if (addedNode.nodeType === 1 && addedNode.tagName === 'IMG'){
var isTracker = false;
// add your expected tracking pixel domains here
if (addedNode.src.includes("pages01.net")){ isTracker = true; }
if (addedNode.src.includes("bat.bing.com")){ isTracker = true; }
if (addedNode.src.includes("apt.techtarget.com")){ isTracker = true; }
if (isTracker){
addedNode.setAttribute("alt", "");
addedNode.setAttribute("role", "presentation");
addedNode.setAttribute("aria", "hidden");
console.log('alt text added to tracker img: ', addedNode)
}
}
}
window.onload = (event) => {
console.log('page is fully loaded');
setTimeout(function() { console.log('disconnecting DOM observer...'); observer.disconnect(); }, 5000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment