Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebView Javascript error: "Can't find variable: external" #39

Open
gered opened this issue Dec 30, 2021 · 2 comments
Open

WebView Javascript error: "Can't find variable: external" #39

gered opened this issue Dec 30, 2021 · 2 comments

Comments

@gered
Copy link

gered commented Dec 30, 2021

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!

@gered
Copy link
Author

gered commented Dec 30, 2021

Also maybe useful, this issue has been logged against the Rust web-view dependency used by rpatchur: Boscop/web-view#289

@gered
Copy link
Author

gered commented Dec 30, 2021

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');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant