<div class="navbar"> <!-- ... --> </div> <x-ms-webview id="WebView"></x-ms-webview>
// Update the navigation state this.updateNavState = () => { this.backButton.disabled = !this.webview.canGoBack; this.forwardButton.disabled = !this.webview.canGoForward; }; // Listen for the back button to navigate backwards this.backButton.addEventListener("click", () => this.webview.goBack()); // Listen for the forward button to navigate forwards this.forwardButton.addEventListener("click", () => this.webview.goForward());
// Listen for the stop/refresh button to stop navigation/refresh the page this.stopButton.addEventListener("click", () => { if (this.loading) { this.webview.stop(); this.showProgressRing(false); this.showRefresh(); } else { this.webview.refresh(); } });
const RE_VALIDATE_URL = /^[-:.&#+()[\]$'*;@~!,?%=\/\w]+$/; // Attempt a function function attempt(func) { try { return func(); } catch (e) { return e; } } // Navigate to the specified absolute URL function navigate(webview, url, silent) { let resp = attempt(() => webview.navigate(url)); // ... } // Navigate to the specified location this.navigateTo = loc => { // ... // Check if the input value contains illegal characters let isUrl = RE_VALIDATE_URL.test(loc); if (isUrl && navigate(this.webview, loc, true)) { return; } // ... Fallback logic (eg prepending http(s) to the URL, querying Bing.com, etc.) }; // Listen for the Enter key in the address bar to navigate to the specified URL this.urlInput.addEventListener("keypress", e => { if (e.keyCode === 13) { this.navigateTo(urlInput.value); } });
// Check if a file exists at the specified URL function fileExists(url) { return new Promise(resolve => Windows.Web.Http.HttpClient() .getAsync(new URI(url), Windows.Web.Http.HttpCompletionOption.responseHeadersRead) .done(e => resolve(e.isSuccessStatusCode), () => resolve(false)) ); } // Show the favicon if available this.getFavicon = loc => { let host = new URI(loc).host; // Exit for cached ico location // ... let protocol = loc.split(":")[0]; // Hide favicon when the host cannot be resolved or the protocol is not http(s) // ... loc = `${protocol}://${host}/favicon.ico`; // Check if there is a favicon in the root directory fileExists(loc).then(exists => { if (exists) { console.log(`Favicon found: ${loc}`); this.favicon.src = loc; return; } // Asynchronously check for a favicon in the web page markup console.log("Favicon not found in root. Checking the markup..."); let script = "Object(Array.from(document.getElementsByTagName('link')).find(link => link.rel.includes('icon'))).href"; let asyncOp = this.webview.invokeScriptAsync("eval", script); asyncOp.oncomplete = e => { loc = e.target.result || ""; if (loc) { console.log(`Found favicon in markup: ${loc}`); this.favicon.src = loc; } else { this.hideFavicon(); } }; asyncOp.onerror = e => { console.error(`Unable to find favicon in markup: ${e.message}`); }; asyncOp.start(); }); };
// Before (ES < 6): "(function () {var n = document.getElementsByTagName('link'); for (var i = 0; i < n.length; i++) { if (n[i].rel.indexOf('icon') > -1) { return n[i].href; }}})();" // After (ES6): "Object(Array.from(document.getElementsByTagName('link')).find(link => link.rel.includes('icon'))).href"
KeyHandler::KeyHandler() { // Must run on App UI thread m_dispatcher = Windows::UI::Core::CoreWindow::GetForCurrentThread()->Dispatcher; } void KeyHandler::setKeyCombination(int keyPress) { m_dispatcher->RunAsync( CoreDispatcherPriority::Normal, ref new DispatchedHandler([this, keyPress] { NotifyAppEvent(keyPress); })); }
// Create the C++ Windows Runtime Component let winRTObject = new NativeListener.KeyHandler(); // Listen for an app notification from the WinRT object winRTObject.onnotifyappevent = e => this.handleShortcuts(e.target); // Expose the native WinRT object on the page's global object this.webview.addWebAllowedObject("NotifyApp", winRTObject); // ... // Inject fullscreen mode hot key listener into the WebView with every page load this.webview.addEventListener("MSWebViewDOMContentLoaded", () => { let asyncOp = this.webview.invokeScriptAsync("eval", ` addEventListener("keydown", e => { let k = e.keyCode; if (k === ${this.KEYS.ESC} || k === ${this.KEYS.F11} || (e.ctrlKey && k === ${this.KEYS.L})) { NotifyApp.setKeyCombination(k); } }); `); asyncOp.onerror = e => console.error(`Unable to listen for fullscreen hot keys: ${e.message}`); asyncOp.start(); });
//// browser.js // Use a proxy to workaround a WinRT issue with Object.assign this.titleBar = new Proxy(Windows.UI.ViewManagement.ApplicationView.getForCurrentView().titleBar, { "get": (target, key) => target[key], "set": (target, key, value) => (target[key] = value, true) }); //// title-bar.js // Set your default colors const BRAND = hexStrToRGBA("#3B3B3B"); const GRAY = hexStrToRGBA("#666"); const WHITE = hexStrToRGBA("#FFF"); // Set the default title bar colors this.setDefaultAppBarColors = () => { Object.assign(this.titleBar, { "foregroundColor": BRAND, "backgroundColor": BRAND, "buttonForegroundColor": WHITE, "buttonBackgroundColor": BRAND, "buttonHoverForegroundColor": WHITE, "buttonHoverBackgroundColor": GRAY, "buttonPressedForegroundColor": BRAND, "buttonPressedBackgroundColor": WHITE, "inactiveForegroundColor": BRAND, "inactiveBackgroundColor": BRAND, "buttonInactiveForegroundColor": GRAY, "buttonInactiveBackgroundColor": BRAND, "buttonInactiveHoverForegroundColor": WHITE, "buttonInactiveHoverBackgroundColor": BRAND, "buttonPressedForegroundColor": BRAND, "buttonPressedBackgroundColor": BRAND }); };
Source: https://habr.com/ru/post/269143/
All Articles