Jaroslav Daníček
Jaroslav Daníček

Reputation: 378

Is it possible to disable all javascript execution on certain page at given moment after page loads?

I sometimes bump into a page which offers interesting content and also with it big dose of annoying bulshit like adds, popups, redirections, today you can't just install noscript or addblock because they are stopping to work or they can get detected and site will prevent you from seeing the content.

When you block script domains and other resources before page loads you will get broken or blank page - so this approach really solves nothing, when you disable it after load (using noscript or quickjs) it does not stop anything at all (what is already loaded).

Let's say you are on page with invasive advertising where every link is handled through javascript and real link on anchor tags is never used, each time you click executes some click handler bound to links and opens eg a popup from base64 image or something like that so even adblock is helpless. For sure it cannot cover all scenarions.

Is there and add-on for firefox, chrome which could on demand remove all attached handlers, unload all scripts, inline scripts without interferring with displayed content and leave a page unmodified in an instant when I decide to and for currently opened tab? And no line of js code, ajax call, simply nothing could be executed and modified after that moment? So to complete the scenario, whatever I would do next would be controlled just by pure html and css (in scope of current tab)?

Upvotes: 3

Views: 2092

Answers (1)

Altimus Prime
Altimus Prime

Reputation: 2327

I found the following here. It works by requesting source of the page you're on and then stripping the script tags from it and then reloading the whole DOM with that source minus the scripts. For me it's bypassed the <noscript> type nuisances. I loaded into an extension and enable it for pages that are especially annoying.

You can modify it so that it only blocks certain scripts. I run this as a locally packed extension in Chrome.

var xhr = new XMLHttpRequest,
    content,
    doc,
    scripts;

xhr.open( "GET", document.URL, false );
xhr.send(null);
content = xhr.responseText;

doc = document.implementation.createHTMLDocument(""+(document.title || ""));

doc.open();
doc.write(content);
doc.close();


scripts = doc.getElementsByTagName("script");
//Modify scripts as you please
[].forEach.call( scripts, function( script ) {
    script.removeAttribute("src");
    script.innerHTML = 'alert("hello world");';
});

//Doing this will activate all the modified scripts and the "old page" will be gone as the document is replaced
document.replaceChild( document.importNode(doc.documentElement, true), document.documentElement);

Upvotes: 1

Related Questions