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.