Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/LoadingPage.h
Original file line number Diff line number Diff line change
@@ -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 <string>

inline std::string loadingPage(const std::string& quotedUrl) {
return R"HTML(<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Discord</title>
<style>
html, body { height: 100%; margin: 0; background: #313338; color: #dbdee1; font: 14px sans-serif; overflow: hidden;
user-select: none; cursor: default; }
#drag { position: fixed; top: 0; left: 0; right: 0; height: 28px; }
#wrap { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; }
.spinner { width: 32px; height: 32px; border: 3px solid #4e5058; border-top-color: #5865f2; border-radius: 50%;
animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
</style>
</head>
<body>
<div id="drag"></div>
<div id="wrap"><div class="spinner"></div><div>Loading Discord&hellip;</div></div>
<script>
(() => {
const bridge = () => (window.chrome && window.chrome.webview) || window.webkit.messageHandlers.host;
const post = (method, args) => {
try {
bridge().postMessage(JSON.stringify({id: Date.now(), method, args}));
} catch (e) {}
};
document.getElementById('drag').addEventListener('mousedown', e => {
if (e.button === 0) post('moveDrag', []);
});
// The host shows the window once a page reports ready, so the loading screen appears at once.
post('ready', []);
// Then the Discord page takes this one's place. A failure to load it is reported by the host.
location.replace()HTML" + quotedUrl + R"HTML();
})();
</script>
</body>
</html>
)HTML";
}
9 changes: 8 additions & 1 deletion src/PageScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 || (() => {});
Expand Down Expand Up @@ -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";
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <thread>

#include "LoadingPage.h"
#include "PageScript.h"
#include "UpdateCheck.h"
#include "WebViewWindow.h"
Expand Down Expand Up @@ -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.
Expand Down