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.