How to: Block specific tags with a custom JS injection

The following JavaScript can be used to block specific tags from running while performing an instant scan. However, there is some execution time required to inject the script and in some cases it may run after the script it is intending to block. In these situations the best option would be to block the children of that script, or to run the script in synchronous mode, though these may not always be possible.

Note that this script simply an example of how to approach this, and you are able to implement your own solutions.  For more on using custom JavaScript with instant scan see here.

function absURL(src) {
var absURLHref = document.createElement("a");
absURLHref.href = src;
return absURLHref.href;
}
var isLoadingDenied = function (url) {
url = absURL(url);
return url.indexOf("<Name of script to be blocked>") >= 0;
|| url.indexOf("<Second script to be blocked>") >= 0;};
var proto = window.HTMLScriptElement.prototype;
var setter = proto.__lookupSetter__("src");
var getter = proto.__lookupGetter__("src");
proto.__defineSetter__("src", function (attrValue) {
if (isLoadingDenied(attrValue)) {
attrValue = "data:,";
}
setter.call(this, attrValue);
});
proto.__defineGetter__("src", getter);
var xhrProto = XMLHttpRequest.prototype;
var oldXMLHttpRequestOpen = xhrProto.open;
xhrProto.open = function (method, url) {
if (isLoadingDenied(url)) {
this.__mtm_nosend = 1;
url = "data:,";
}
return oldXMLHttpRequestOpen.call(this, method, url);
};
var oldXMLHttpRequestSend = xhrProto.send;
xhrProto.send = function () {
if (this.__mtm_nosend) {
return;
}
return oldXMLHttpRequestSend.apply(this, arguments);
};
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.