Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): WebPWA #2994

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
104 changes: 104 additions & 0 deletions src/plugins/webPWA.web/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/

import { disableStyle, enableStyle } from "@api/Styles";
import { Devs } from "@utils/constants";
import definePlugin from "@utils/types";
import { findStoreLazy } from "@webpack";
import { RelationshipStore } from "@webpack/common";
import type { FluxStore } from "@webpack/types";

import style from "./style.css?managed";
ThaUnknown marked this conversation as resolved.
Show resolved Hide resolved

const MANIFEST = {
name: "Discord",
short_name: "Discord",
start_url: location.origin + "/app", // URL when PWA launches
display: "fullscreen",
display_override: ["window-controls-overlay"],
lang: "en-US",
background_color: "#2a2a2f",
theme_color: "#2a2a2f",
scope: "/", // scope of all possible URL"s
description: "Imagine a place...",
orientation: "landscape",
icons: [
{
src: document.querySelector<HTMLLinkElement>('link[rel="icon"]')!.href,
sizes: "256x256",
type: "image/png"
}
]
};

const isMac = navigator.platform.startsWith("Mac");

const GuildReadStateStore: FluxStore & { getTotalMentionCount: () => number; hasAnyUnread: () => boolean; } = findStoreLazy("GuildReadStateStore");
const NotificationSettingsStore: FluxStore & { getDisableUnreadBadge: () => boolean; } = findStoreLazy("NotificationSettingsStore");

export default definePlugin({
name: "WebPWA",
description: "Allows Discord to be installable and usable as a PWA.",
authors: [Devs.ThaUnknown],
manifest: null,
setBadge: () => {
try {
const mentionCount = GuildReadStateStore.getTotalMentionCount();
const pendingRequests = RelationshipStore.getPendingCount();
const hasUnread = GuildReadStateStore.hasAnyUnread();
const disableUnreadBadge = NotificationSettingsStore.getDisableUnreadBadge();

let totalCount = mentionCount + pendingRequests;
if (!totalCount && hasUnread && !disableUnreadBadge) totalCount = -1;

navigator.setAppBadge(totalCount);
} catch (e) {
console.error(e);
}
},
start() {
enableStyle(style);
const url = URL.createObjectURL(new Blob([JSON.stringify(MANIFEST)], { type: "application/json" }));
this.linkEl = document.createElement("link");
this.linkEl.rel = "manifest";
this.linkEl.href = url;

NotificationSettingsStore.addChangeListener(this.setBadge);
GuildReadStateStore.addChangeListener(this.setBadge);
RelationshipStore.addChangeListener(this.setBadge);
},
stop() {
disableStyle(style);
this.linkEl?.remove();
navigator.setAppBadge(0);
NotificationSettingsStore.removeChangeListener(this.setBadge);
GuildReadStateStore.removeChangeListener(this.setBadge);
RelationshipStore.removeChangeListener(this.setBadge);
},
patches: [
{
find: "platform-web",
replacement: {
// eslint-disable-next-line no-useless-escape
match: /(?<=" platform-overlay"\):)\i/,
replace: "$self.getPlatformClass()"
}
},
{
find: "\"NotificationSettingsStore",
replacement: {
// eslint-disable-next-line no-useless-escape
match: /\.isPlatformEmbedded(?=\?\i\.\i\.ALL)/g,
replace: "$&||true"
}
}
],

getPlatformClass() {
if (isMac) return "platform-osx";
return "platform-win";
}
});
11 changes: 11 additions & 0 deletions src/plugins/webPWA.web/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Download Desktop button in guilds list */
[class^="listItem_"]:has([data-list-item-id="guildsnav___app-download-button"]),
[class^="listItem_"]:has(+ [class^="listItem_"] [data-list-item-id="guildsnav___app-download-button"]) {
display: none;
}

/* Workaround for making things in the draggable area clickable again on macOS */
.platform-osx [class*="topic_"],
.platform-osx [class*="notice_"] button {
-webkit-app-region: no-drag;
}
6 changes: 5 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,10 +575,14 @@ export const Devs = /* #__PURE__*/ Object.freeze({
name: "RamziAH",
id: 1279957227612147747n,
},
SomeAspy: {
SomeAspy: {
name: "SomeAspy",
id: 516750892372852754n,
},
ThaUnknown: {
name: "ThaUnknown_",
id: 252390917665718273n
},
} satisfies Record<string, Dev>);

// iife so #__PURE__ works correctly
Expand Down