1

I'm trying to override document.write so i will be able to take the raw html parse it make some manipulations on the code and return the call. My entire process is async so it's needless to say that when document.write is called if the page has finished loading the document.write will erase the entire document, so i can't recall document.write. I've searched and found many discussions about that but almost every one of them is very very old so i can't find any good answer. my code for now is very basic:

 const prevDocWrite = document.write;
 document.write = function(str,patched) {
        if(patched){

            prevDocWrite.call(this, str);
        }else{
            Dom_Parser(str, context).then(newStr => {

                prevDocWrite.call(this, newStr);
            })};
    };

I have added the "patched" part because i'm also calling document.write on my code so i call it like that document.write("str",true) and it will call the original document.write immediately.

I would really appreciate any help or ideas how to make this happen (other projects for reference will be great)

BTW i saw a lot of implementation using innerHtml but that's screwing up the <script> tags:(

Thanks a LOT

mplungjan
  • 169,008
  • 28
  • 173
  • 236
avi dahan
  • 539
  • 3
  • 19
  • 1
    What is the problem? You can just do `prevDocWrite(str)` no need to call. HOWEVER you cannot call prevDocWrite after the page has loaded - there is no difference between native document.write and your prevDocWrite after the document has been closed. There is nothing NEW about replacing document.write. You CAN use innerHTML - I do not see why that would screw up any tags unless you want to show the original tags. Please provide a [mcve] and show expected in and output – mplungjan Jun 12 '18 at 17:04
  • Why are you using document.write in the first place? There are better ways to modify the DOM (such as using innerHTML. You'd probably be better off asking about whatever you mean by it "screwing up the tags" than by trying to homeroll your own dom insertion methods .) – Daniel Beck Jun 12 '18 at 17:06
  • @DanielBeck - there are usecases - for example when loading external adverts, where you want to intercept document.write – mplungjan Jun 12 '18 at 17:07
  • 1
    @mplungjan i know there is no difference between my implementation and the native one that's why i asked for help... and if you didn't know when using innerHtml with script tags the script tags wont execute.(https://stackoverflow.com/questions/1197575/can-scripts-be-inserted-with-innerhtml) – avi dahan Jun 12 '18 at 17:10
  • Note that the function actually lives on `document.__proto__.write` (aka `HTMLDocument.prototype.write`), the way you are doing it, the original is still accessible that way. – ASDFGerte Jun 12 '18 at 17:11
  • @ASDFGerte Thanks i will check that out , but most of the examples i saw did that just like that and until now it works , except for the problem i described – avi dahan Jun 12 '18 at 17:13
  • @avidahan The ` – mplungjan Jun 12 '18 at 17:15
  • @mplungjan My apologies it somehow got deleted. you are right i can extract them but how should i parse the html via Domparser? because the Domparser adds html tags to the code so i will have to trim them , and how should i mark where should i insert my modified html in the dom because when i finish with my manipulations on the html the document is most likely finished loading so i need to know where to insert it . – avi dahan Jun 12 '18 at 17:17
  • Perhaps you could consider document.writing everything to an iFrame - it will execute the script in that window context - PS:it is not clear why you need a domparser here – mplungjan Jun 12 '18 at 17:18
  • @mplungjan its an advert code so it needs access to the ad document and inside an iframe the code wont be able to access the ad dom , like via getDocumentById – avi dahan Jun 12 '18 at 17:20
  • How so? Normally an iFrame is a standalone window object - i.e. just document.write the ad script into the iframe and it will create whatever it needs – mplungjan Jun 12 '18 at 17:23
  • @mplungjan I think you probably misunderstood me , the ad code runs inside an iframe and than this code calls document.write with some more HTML code and I want to make some manipulations to that code in an async matter , so if I will create another iframe and redirect the call to that iframe they will have different content windows – avi dahan Jun 12 '18 at 17:27
  • Right. I thought you just wanted to for example move the output. So read the ad code on the server and modify it there – mplungjan Jun 12 '18 at 18:03
  • @mplungjan there is got to be a better way other than parsing the ad code and extracting the document.write , also it will be most likely a variable so I will need to actually render the ad server side which is highly complicated and load wasting . – avi dahan Jun 12 '18 at 18:13
  • Hard to tell without code, input and expected output – mplungjan Jun 12 '18 at 18:14
  • @mplungjan every ad acts differently so there are a lot of different situations but at least some basic implementation, I saw some different projects that does that but they are very old – avi dahan Jun 12 '18 at 18:18
  • But what do you want to change in the code? I have successfully just intercepted document write and for some simply hand hacked advert code. – mplungjan Jun 12 '18 at 18:20
  • @mplungjan I need to make some requests while modifying the code so it got to be async and when I finish most of the time the document finished loading so I need to find a way to use document.write async as close to the original call as possible – avi dahan Jun 12 '18 at 18:23

0 Answers0