3

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)?

  • It might be possible to just "recreate" the page by cloning every element one by one, virtually making a copy of the dom, setting all "onclick" attributes to nothing, not cloning scripts and so on. I dont see another way. Keep in mind you get severely reduced functionality without JS. – Dellirium Jan 28 '17 at 14:26
  • I am aware of that but sometimes there is no other way when some sites decided to torture its visitors for their incomes. About the recreating It is quite interesting idea but I am not that much into javascript to be able to write an addon like that. I was generally asking. – Jaroslav Daníček Jan 28 '17 at 14:37
  • I would probably be able to do it through javascript but I do not know how to make an addon, and also its just hypothetical, I didn't really try to do it, but it is not that complex honestly. – Dellirium Jan 28 '17 at 14:46
  • I am also thinking of a way where javascript would only unbind all event handlers bound to html elements and for all script elements there should be a way how to unload them from memory by simply dynamically loading "nothing doing" scripts into them – Jaroslav Daníček Jan 29 '17 at 18:37
  • I am not sure you can unload them that way, once the function is defined and the interpreter reads it, you can (afaik) change the source all you want, it will stay in memory. Could be wrong though. However even if they are in memory if no part of the page is actually attemptign to access them, you should be fine, this of course leaves out the `timeout` functions, which are predestined to occur, need to boggle down on that one a bit more – Dellirium Jan 31 '17 at 16:26
  • I just read some article about unloading js by replacing their script content, never tested if that works. But it looks there is no way how to stop any javascript from executing once it has been loaded. I wonder why from security concerns is not possible in browsers. – Jaroslav Daníček Feb 19 '17 at 23:51

1 Answers1

1

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);
Altimus Prime
  • 2,207
  • 2
  • 27
  • 46