WebView Javascript error: "Can't find variable: external"
gered opened this issue · 2 comments
The HTML pages loaded into rpatchur's WebView will of course need to at some point use Javascript in button click handlers or whatever else to perform actions, and that means calling external.invoke()
as per your own examples (e.g. /examples/basic_launcher/index.html
found in this repository).
I believe this WebView integration is broken on non-Windows OS's.
On my Gentoo Linux PC, I was having issues running an rpatchur installation with no visible error. Debugging it via CLion, I saw this error pop up:
https://koko.z13.web.core.windows.net/js/jquery-3.4.1.slim.min.js:2:31216: CONSOLE WARN jQuery.Deferred exception: Can't find variable: external @https://koko.z13.web.core.windows.net/live/index.html:35:21
e@https://koko.z13.web.core.windows.net/js/jquery-3.4.1.slim.min.js:2:29662
@https://koko.z13.web.core.windows.net/js/jquery-3.4.1.slim.min.js:2:29960 undefined
With the most important bit being obviously the Can't find variable: external
bit which it encountered when running this line found in the mentioned index.html
external.invoke('start_update');
I was able to fix this by replacing all calls to external.invoke()
with window.webkit.messageHandlers.external.postMessage()
instead. This obviously will not work when the WebView is being backed by IE on Windows, so a suitable cross-platform solution would be nice here!
Also maybe useful, this issue has been logged against the Rust web-view dependency used by rpatchur: Boscop/web-view#289
To anyone else noticing this issue on a RO server using rpatchur, the suggestion found in the above linked issue on the web-view project to use this Javascript function to work around the cross-platform differences would seem to be the best bet.
// include this function somewhere in the page source you load into rpatchur
function sendMessageToServer(cmd) {
if (window.external !== undefined) {
return window.external.invoke(cmd);
} else if (window.webkit.messageHandlers.external !== undefined) {
return window.webkit.messageHandlers.external.postMessage(cmd);
}
throw new Error('Failed to locate webkit external handler')
}
Then instead of doing stuff like:
external.invoke('start_update');
Instead do:
sendMessageToServer('start_update');