I\'m trying to write a script for Tampermonkey that prevents the execution of a specific inline script tag. The body of the page looks something like this
&l
Alternative version free of doc.write/XHR --
(() => {
'use strict';
let needle = '/window.location/';
if ( needle === '' || needle === '{{1}}' ) {
needle = '.?';
} else if ( needle.slice(0,1) === '/' && needle.slice(-1) === '/' ) {
needle = needle.slice(1,-1);
} else {
needle = needle.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
needle = new RegExp(needle);
const jsnode = () => {
try {
const jss = document.querySelectorAll('script');
for (const js of jss) {
if (js.outerHTML.match(needle)) {
js.remove();
}
}
} catch { }
};
const observer = new MutationObserver(jsnode);
observer.observe(document.documentElement, { childList: true, subtree: true });
})();
Delete script tag on document-start
(as suggested by wOxxOm):
(function() {
'use strict';
window.stop();
const xhr = new XMLHttpRequest();
xhr.open('GET', window.location.href);
xhr.onload = () => {
var html = xhr.responseText
.replace(/<script\b[\s\S]*?<\/script>/g, s => {
// check if script tag should be replaced/deleted
if (s.includes('window.location')) {
return '';
} else {
return s;
}
});
document.open();
document.write(html);
document.close();
};
xhr.send();
})();