0

I'm trying to inject javascript code below into iframe element but I don't understand how I can achieve this. because since the code is written in ES6, I can't find other way I can put the code into iframe element.

I'm aware that I can add javascript into iframe this way,

var scriptTag = "<script>alert(1)</script>";
$("iframe").contents().find("body").append(scriptTag);

but I don't understand how I can do that with code below.

import finder from '@medv/finder'
document.addEventListener('mouseover', event => {
    const selector = finder(event.target)
    console.log(selector) 
}, true)

any help would be so appreciated :)

Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
Phillip YS
  • 784
  • 3
  • 10
  • 33
  • @JacobGoh I mean, you have to compile the javascript code that will be injected before It injects into the iframe. – Phillip YS Jun 01 '18 at 07:13
  • You're not going to be able to do that without transpiling with Babel or something first. You need to find a way to bundle `@medv/finder` within the code you want to inject – CodingIntrigue Jun 01 '18 at 07:38
  • @CodingIntrigue It's saying `exports is not defined` when I bundle it and inject the code. how can I fix it? – Phillip YS Jun 01 '18 at 07:55
  • maybe you should see https://stackoverflow.com/questions/33516906/which-browsers-support-import-and-export-syntax-for-ecmascript-6, finally you´ll find there other ways to do it. – lucchi Jun 02 '18 at 10:26
  • Possible duplicate of [Which browsers support import and export syntax for ECMAScript 6?](https://stackoverflow.com/questions/33516906/which-browsers-support-import-and-export-syntax-for-ecmascript-6) – lucchi Jun 02 '18 at 10:29

0 Answers0