To communicate from JavaScript to C++, use the method window.external.invoke
:
// JavaScript
window.exteral.invoke('hello world!');
// C++
void callback(WebView &w, wv::String &arg) {
// arg = "hello world!"
}
This method can only accept strings, so objects must be serialized in some way:
// JavaScript
window.external.invoke(JSON.stringify({ foo: 'bar' }));
Note: On Windows, all strings are std::wstring
. On MacOS and Linux, they are std::string
.
If you need to support Windows, make sure to use Win32 WinMain
entry point:
#ifdef WIN32
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
#else
int main() {
#endif
// ...
}
WebView(int width, int height, bool resizable, bool debug, string title,
string url = DEFAULT_URL)
The default url (DEFAULT_URL
) will render a blank page with a single root div:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body>
<div id="app"></div>
<script type="text/javascript"></script>
</body>
</html>
int init();
Initializes the webview window.
0 if successful, otherwise -1.
#include "webview.h"
int main() {
wv::WebView w{800, 600, true, true, "My WebView"};
// Initialize the webview
if (w.init() == -1) {
return 1;
}
// Do stuff with the webview
return 0;
}
void setCallback(std::function<void(WebView &, std::string &)> callback);
Attaches a function that will run when receiving messages from JavaScript.
- callback: A callback function
#include "webview.h"
void callback(WebView &w, std::string &arg) {
if (arg == "color") {
w.css("body { background-color: red; }");
}
}
// ...
w.setCallback(callback);
void setTitle(string title);
Sets the title of the webview window.
- title: The new title of the webview
void setFullscreen(bool fs);
Sets the webview window to fullscreen or windowed mode.
- fs: True if setting to fullscreen, false if windowed
void setFullscreenFromJS(bool allow);
Sets whether JavaScript can directly switch between fullscreen and windowed mode. By default, JavaScript cannot change the window using requestFullscreen()
and exitFullscreen()
. Any fullscreen element will take up the size of the window itself rather than the screen.
Note: on Mac (WEBVIEW_MAC
), this method will only apply when called before init
. After init
, this will be a no-op. For fine-grained control, consider polyfilling the fullscreen API with a call to C++ and manually call setFullscreen
. For example,
Element.prototype.requestFullscreen = () => window.external.invoke('full');
Document.prototype.exitFullscreen = () => window.external.invoke('exit');
WEBVIEW_MAIN {
bool allow = false;
wv::WebView w{};
if (w.init() == -1) return 1;
w.setCallback([](wv::WebView& w, wv::String& arg) {
if (allow) {
if (arg == "full") w.setFullscreen(true);
else if (arg == "exit") w.setFullscreen(false);
}
});
while (w.run() == 0);
return 0;
}
Sets the background color of the webview. If the webpage has a background color, it will take precedence over this color.
void setBgColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
- r: Red component of the color (0 to 255)
- g: Green component of the color (0 to 255)
- b: Blue component of the color (0 to 255)
- a: Alpha component of the color (0 to 255)
bool run();
Runs one iteration of the main loop. This method is blocking if no events are pending.
True if the webview window will be closed, otherwise false.
#include "webview.h"
int main() {
wv::WebView w{800, 600, true, true, "My WebView"};
if (w.init() == -1) {
return 1;
}
// Keep running until the window will close
while (w.run() == 0);
return 0;
}
void navigate(string uri);
Navigates the webview to the specified URI.
- uri: URI to the webpage
void preEval(string js);
Injects the JavaScript string into the webpage before it loads.
Note: in some cases, the JavaScript will be run before the DOM loads. If you need access to the DOM, make sure to wait for it to load:
window.onload = () => {
const el = document.getElementById('id');
};
// or
window.addEventListener('DOMContentLoaded', () => {
// or the 'load' event
const el = document.getElementById('id');
});
void eval(string js);
Executes the JavaScript string in the current webpage.
- js: JavaScript string to execute
#include "webview.h"
void callback(wv::WebView &w, std::string &arg) {
if (arg == "eval") {
w.eval("alert('boo!')");
}
}
int main() {
wv::WebView w{800, 600, true, true, "My WebView"};
if (w.init() == -1) {
return 1;
}
w.setCallback(callback);
while (w.run() == 0);
return 0;
}
The alert
will display when this is executed in the webpage:
window.external.invoke('eval');
void css(string css);
Applies the CSS string to the current webpage.
- css: CSS string to apply
This method works similar to WebView::eval
.
#include "webview.h"
void callback(wv::WebView &w, std::string &arg) {
if (arg == "style") {
w.css("p { color: red; }");
}
}
int main() {
wv::WebView w{800, 600, true, true, "My WebView"};
if (w.init() == -1) {
return 1;
}
w.setCallback(callback);
while (w.run() == 0);
return 0;
}
All p
tags will be colored red when this is executed in the webpage:
window.external.invoke('style');
void exit();
Closes the webview window. This will cause the next invocation of WebView::run
to return true
.