0

So the problem seems pretty simple. I want to import an external JS file, convert it to a string and use it for injectedJavascript in WebView.

For example I tried:

    <WebView
      source={{ uri: 'https://www.someurl.com/' }}
      ref={(wView) => (setWebView(wView))}
      javaScriptEnabled
      domStorageEnabled
      injectedJavaScript={
        `
          ${String(contentScript)};
          const ContentScript = contentScript();
        `
      }
      onMessage={receiveMessage}
    />

Where the function contentScript looks like so:

const contentScript = () => {
  const exec = {};

  // ... More code here

  exec.test = () => {
    window.ReactNativeWebView.postMessage('test invoked.');
  };

  return {
    exec,
  };
};

Sadly that does not work most of the times as it returns an empty exec. and sometimes results in unexpected code that can even lead to errors.

So I was thinking of maybe creating some additional compile that converts the file into text and exports the string. What do you think? What would be the best solution for this?

I tried to search for a solution on this, but didn't manage to find any clear solution to this problem.

FYI I'm using EXPO.

TheE
  • 328
  • 4
  • 13
  • Try this https://stackoverflow.com/questions/57170036/import-js-file-as-text-and-use-it-inside-webview-injectedjavascript-in-react-na – aytek Mar 21 '20 at 18:54
  • @aytek Hi, funny enough that is my question from a year ago. Sadly it wasn't solved and the answer there doesn't solve the issue. Currently getting `Unable to resolve "!!raw-loader!./content-script/content-script.js" from "src\pages\ZalandoLounge\ZalandoLounge.jsx"`. I guess I would need to eject from expo?? – TheE Mar 21 '20 at 19:52
  • Sorry about that. I don't have an answer for you right now if you've already tried those 2 ways. – aytek Mar 21 '20 at 21:36
  • @aytek I wonder why is this case not answered for so long :( – TheE Mar 24 '20 at 18:19

0 Answers0