-1

I have this for example, which is a file html:

<html>

My html

<script>
    function Test() {
          alert("yaaaah");
    }
</script>

</html>

I want that nodeJS server execute the function Test() in a file which is at /path/here for example.

Use require doesn't work because the function is nested in the html.

How can I do to access to this function and execute it from server side?

Erased
  • 571
  • 4
  • 10
  • 22
  • 1
    Why are you writing functions in the frontend that must be executed by the backend? – Diego May 11 '17 at 11:10
  • 1
    If you were to extract the JS and run it, then it still wouldn't work because `alert` is a browser API and isn't available to Node. This reads like an http://xyproblem.info/ and you should probably explain what you really want to achieve. – Quentin May 11 '17 at 11:15
  • @Diego The html file containing css and js is wrote by an user, to respect the norms (defined by me), he need to create function getHeight(), getWidth() of his iframe for example. (Each html file is iframe containing only input). And I need to get the value of the return function. I don't use document.windowContent.myFunction() because it needed that I allow "allow-same-origin" but i can't, for the security. So i wanted to used socket to execute this function, get the return value and send it to my general page (the parent). I don't know if i'm clear, ask questions and don't hesitate! – Erased May 11 '17 at 11:30
  • @Diego Or maybe if I can require the function nested in html It can work but how do that? Normally it's require(blablablah.js) because it's a js file. But if my desired function is in html file? Can't do let test = require(blablablah.html). So I can do test.getHeight(), and so on... – Erased May 11 '17 at 11:38
  • Why not doing everything on the frontend? – Diego May 11 '17 at 11:57
  • Possible duplicate: http://stackoverflow.com/questions/22086722/resize-cross-domain-iframe-height – Quentin May 11 '17 at 11:58
  • @Diego to let the developer of the iframe to specified its min-height, height and width of its iframes when it's displayed on the parent. – Erased May 11 '17 at 12:07
  • @Quentin I'm checking that – Erased May 11 '17 at 12:08
  • Suposing that parent is controlled by you, can't you create a function resizeIframe(), which resizes via jQuery the iframe, and force developers to call your function with the correct parameters. All in frontend. – Diego May 11 '17 at 12:11
  • @Diego Oh, so you say, in parent there is a function resizeIframe(size). And for developpers, in their html (iframe), they will call the parent function resizeIframe with completing parameters? I think it's not possible to an iframe to call to a parent function (WITHOUT allow-same-origin) – Erased May 11 '17 at 12:18
  • Sorry, I forgot the Iframe issue. I think now I understand your problem. – Diego May 11 '17 at 12:20
  • @Diego Cool for your understanding! :) If I could allow-same-origin, it will be so easier, but I can't let developpers acces to coockie, DOM, and so on.. – Erased May 11 '17 at 12:34
  • @Erased — Did you read the the duplicate question? You can use message events to pass data between origins with JS without giving full access to cookies and the DOM. – Quentin May 11 '17 at 14:39

1 Answers1

0

My approach would be force them to write an specific comment inside the HTML something like:

<!--key 400,500-->

Then you can read the HTML file and extract the values with a RegExp:

let html = '<html><!--key 500,23--></html>'

let matches = /<!--key (\d+),(\d+)-->/g.exec(html);
matches.shift();

console.log(matches);

Although, if width and height are not static then it's not a valid answer.

Diego
  • 816
  • 7
  • 17
  • And what is the utility of this specific comment? I don't understand the utility. Yeah, it's not static. – Erased May 11 '17 at 12:36
  • "if width and height are not static" — It's a web page. They won't be static. They'll vary from system to system. – Quentin May 11 '17 at 12:36
  • @Quentin It's not really the height and width for my project. It will be precisely min-height, max-height, min-width and max-width – Erased May 11 '17 at 12:39
  • @Erased Have you thought about implementing two socket.io connections, one from the iframe to the server and other from the server to the parent? I cannot imagine any other solution. – Diego May 11 '17 at 12:44
  • @Diego Then, in developer html file, they need to connect to socket IO, and so on.. It will be complicated and not sure that will working. :// Thanks a lot for your time, I really appreciate it! – Erased May 11 '17 at 12:46
  • @Quentin Is it impossible to execute this function inside html by server nodejs? – Erased May 11 '17 at 13:07