I am currently coding in Javascript and trying to implement a feature that monitors new 'script' elements and blocks specific sources. I have experimented with using MutationObserver and __defineSetter__, both of which can monitor changes to the 'src' attribute, but they only intercept the change after the HTTP request has already been made. This means that even if I modify the src attribute, the resource is not effectively blocked.
window.HTMLScriptElement.prototype.__defineSetter__('src', function (d)
{
console.log("HTMLScriptElement: ",d);
if (d.indexOf('localhost') != -1) {
//BLOCK SCRIPT
}
});
new MutationObserver(function (a) {
a.forEach((e) => {
e.addedNodes.forEach((z) =>
{
if (z.nodeName.toLowerCase() == "script" && z.src.indexOf('localhost') != -1)
{
//BLOCK SCRIPT
}
})
})
}).observe(window.document, {
attributes: true,
childList:true,
subtree:true
});