From 3dfcabf7609728d27728c92b2e93e589dea08927 Mon Sep 17 00:00:00 2001 From: Jon <> Date: Sat, 7 Nov 2026 21:52:28 +0000 Subject: [PATCH] Show the Discord title bar on WebView2 WebView2 has no window.webkit.messageHandlers, so the page's host messages were dropped on Windows and the page never reported ready. The bridge now uses window.chrome.webview there, and keeps webkit on Linux. The page script could also run before the document had a body, when the navigation completed, and then failed on documentElement. It now waits for DOMContentLoaded in that case. The window opens on a loading page, which reports ready at once and then moves on to Discord, so the window appears straight away. Co-Authored-By: Claude Haiku 5.5 --- src/LoadingPage.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/PageScript.h | 9 ++++++++- src/main.cpp | 4 +++- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/LoadingPage.h diff --git a/src/LoadingPage.h b/src/LoadingPage.h new file mode 100644 index 0000000..685a3b4 --- /dev/null +++ b/src/LoadingPage.h @@ -0,0 +1,47 @@ +// The page shown while Discord loads. It reports ready at once, so the window appears straight away, then replaces itself with +// the Discord page. The address is passed as a JavaScript string literal, already quoted. +#pragma once + +#include + +inline std::string loadingPage(const std::string& quotedUrl) { + return R"HTML( + + + + +Discord + + + +
+
Loading Discord…
+ + + +)HTML"; +} diff --git a/src/PageScript.h b/src/PageScript.h index 06695e7..abad263 100644 --- a/src/PageScript.h +++ b/src/PageScript.h @@ -4,14 +4,17 @@ inline constexpr const char* kPageScript = R"JS( (() => { + const install = () => { // The window starts on about:blank, which must not count as the page being ready. if (!/^https?:$/.test(location.protocol) || window.__gwDiscordFrame) return; window.__gwDiscordFrame = true; const BAR = 28; + // WebView2 (Windows) and WebKitGTK (Linux) expose the host under different names. + const bridge = () => (window.chrome && window.chrome.webview) || window.webkit.messageHandlers.host; const post = (method, args) => { try { - window.webkit.messageHandlers.host.postMessage(JSON.stringify({id: Date.now(), method, args})); + bridge().postMessage(JSON.stringify({id: Date.now(), method, args})); } catch (e) {} }; window.hostReceive = window.hostReceive || (() => {}); @@ -143,5 +146,9 @@ inline constexpr const char* kPageScript = R"JS( // Tells the host the frame is in place: the window's onPageReady runs for it. post('ready', []); + }; + // The page is injected as its navigation completes, and that can come before the document has a body to build the bar in. + if (document.body) install(); + else document.addEventListener('DOMContentLoaded', install, {once: true}); })(); )JS"; diff --git a/src/main.cpp b/src/main.cpp index f1c404b..f781ad5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,6 +11,7 @@ #include #include +#include "LoadingPage.h" #include "PageScript.h" #include "UpdateCheck.h" #include "WebViewWindow.h" @@ -111,7 +112,8 @@ class DiscordWindow : public WebViewWindow { }); } - std::string devUrl() const override { return kDiscordUrl; } + // The window opens on the loading page, which moves on to Discord. A reload after the page's process died starts over the same way. + std::string html() const override { return loadingPage(Json(kDiscordUrl).dump()); } std::string title() const override { return "Discord"; } // Shows the update button on the bar, now and after each page load. Called on the window's thread.