Search code examples
javascriptunreal-engine4

Including external JS script without the HTML script tag


Pardon my ignorance, as my knowledge of JS is very shallow.

I'm currently using UnrealJS to connect to a Google Javascript API. UnrealJS basically runs a V8 instance inside the Unreal 4 engine. Considering that there's no conception of a DOM or HTML really inside UnrealJS, I need to be able to link to the external Google JS API script without the script tag:

<script src="some-external-script-url" />

Is there a way to do this? UnrealJS also supports Node.js - so if there's a way to do it with native Node.js, that would work as well. I'v looked into using require() from Node, but it seems to only be available for local scripts.

Apologies if this question is too vague or hard to understand, I will edit it if need be.


Solution

  • I'm not familiar with UnrealJS, but you've mentioned you can run Node code, and require modules. If that's the case, you can use an HTTP/S request to get the remote code, and then use vm.runInThisContext to inject the script into the current context. A sample code can be found in this answer.