0

I'm currently working on a webExtension, and injected the html of my webExtension into an iframe. I want to drag stuff from arbitrary web pages into a droppable area in the iframe, and display them in the droppable area. I have a dragstartListener that uses postMessage to send the HTMLdocument of the element dragged everytime it listens to a dragstart event. But postMessage cannot send objects with methods inside. Are there any alternatives to implement the function? Thank you!

  • JSON.stringify/parse ... So yes you can. But that means that you cannot simply pass a function and call it on the other side. – Jonas Wilms Jun 06 '17 at 20:04
  • why dont you implement the function on the Site in the Iframe so you can just "send" the content to the Iframe and then use the function on the other side – Matthias Gwiozda Jun 06 '17 at 20:04
  • Thanks for the comments! Jonas: I tried using JSON.stringify/parse, but a large portion of the objects became null after being parsed back, since they have methods inside. @Matthias: I'm trying to send the content to the Iframe, but my functions need the HTMLdocument of the element, and it cannot be sent via postMessage. – Willing Blueberry Jun 06 '17 at 20:17
  • I found this: https://stackoverflow.com/questions/12651977/why-cant-you-stringify-a-function-expression . Basicly you could find all functions in the Elements that you want to stringify and add ``"" +` before it. so it will be handeled as a string. then you can post it to your iframe – Matthias Gwiozda Jun 06 '17 at 20:40
  • So with regular Expressions you could do: `string.replace(/function()/gi, "\"\" + function()")` – Matthias Gwiozda Jun 06 '17 at 20:45
  • @MatthiasGwiozda Thank you for the answer! I tried similar approach through deleting the functions recursively (the document has document inside of it). But the recursion was so deep that it reaches the limit for system stack. – Willing Blueberry Jun 06 '17 at 20:49
  • :O why do you have a document in the document ? – Matthias Gwiozda Jun 06 '17 at 20:50
  • @MatthiasGwiozda Yeah it's an HTMLDocument so it has the attribute parentNode, which itself is also an HTMLDocument. – Willing Blueberry Jun 06 '17 at 20:53
  • Would it be bad to set this property to null so you can transport it to the iframe? – Matthias Gwiozda Jun 06 '17 at 21:02
  • @MatthiasGwiozda It is bad since the info are needed in those functions. – Willing Blueberry Jun 06 '17 at 21:28

1 Answers1

0

I figured out a solution: using XMLSerializer.

XML Serializer can actually serialize dom elements into strings. Postmessage can send strings to another window safely.