3D WebView doesn't yet have an API specifically for this, but I have a feature request for it, and I'll update this article if I add support for it in the future. In the meantime, here are some options:
- One option is to add JavaScript to IWebView.PageLoadScripts that disables links to sites that aren't whitelisted, like this:
await webViewPrefab.WaitUntilInitialized();
webViewPrefab.WebView.PageLoadScripts.Add(@"
const whitelistedDomains = [
'developer.vuplex.com',
'google.com'
];
// Every 500ms, check for links to non-whitelisted domains.
setInterval(() => {
const anchorTags = Array.from(document.getElementsByTagName('a'));
for (const anchorTag of anchorTags) {
const url = anchorTag.getAttribute('href');
if (shouldDisableLink(url)) {
anchorTag.removeAttribute('href');
}
}
}, 500);
function shouldDisableLink(url) {
if (!url) {
return false;
}
if (url.indexOf('http') !== 0) {
return false;
}
for (const domain of whitelistedDomains) {
if (url.indexOf(`http://${domain}`) === 0 || url.indexOf(`https://${domain}`) === 0) {
return false;
}
}
return true;
}
");
- Even with disabling links like shown in option #1, a page can still navigate to a different URL via JavaScript. So, I also recommend attaching a handler to IWebView.UrlChanged that detects if the webview navigated to a site that isn't whitelisted and, if so, either use IWebView.LoadUrl() to navigate to the last acceptable site or use IWebView.GoBack() to go back.