Add language detection from Discord

This commit is contained in:
smartfrigde 2022-06-11 22:14:16 +02:00
parent e518c0f04e
commit fa24b7996f
4 changed files with 33 additions and 8 deletions

View file

@ -1,7 +1,7 @@
//ipc stuff
import {app, ipcMain, shell, desktopCapturer} from "electron";
import {createTabsGuest, mainWindow} from "./window";
import {setConfigBulk, getVersion, getConfig} from "./utils";
import {setConfigBulk, getVersion, getConfig, setLang} from "./utils";
import {customTitlebar, tabs} from "./main";
import {createSettingsWindow} from "./settings/main";
export function registerIpc() {
@ -11,6 +11,9 @@ export function registerIpc() {
ipcMain.on("openTab", (event, number: number) => {
createTabsGuest(number);
});
ipcMain.on("setLang", (event, lang: string) => {
setLang(lang);
});
ipcMain.on("open-external-link", (event, href: string) => {
shell.openExternal(href);
});

View file

@ -16,6 +16,7 @@ contextBridge.exposeInMainWorld("armcord", {
electron: process.versions.electron,
channel: ipcRenderer.sendSync("channel"),
openTab: (number: number) => ipcRenderer.sendSync("openTab", number),
setLang: (lang: string) => ipcRenderer.send("setLang", lang),
version: ipcRenderer.sendSync("get-app-version", "app-version"),
getDisplayMediaSelector: getDisplayMediaSelector,
openSettingsWindow: () => ipcRenderer.send("openSettingsWindow")

View file

@ -4,11 +4,15 @@ import "./patch";
import * as fs from "fs";
import * as path from "path";
import {injectTitlebar} from "./titlebar";
import {sleep, addStyle, injectJS} from "../utils";
import {sleep, addStyle, injectJS, addScript} from "../utils";
import {ipcRenderer} from "electron";
import {injectTabs} from "./tabs";
var version = ipcRenderer.sendSync("get-app-version", "app-version");
async function updateLang() {
addScript(`function getDiscordLang() {
{const _w=webpackChunkdiscord_app;let lang;_w.push([[Symbol()],{},e=>{for(const k in e.c){const m=e.c[k].exports;const mDef=m?.default&&m.__esModule?m.default:m;if(mDef?._chosenLocale&&!lang)lang=mDef}}]);_w.pop();window.armcord.setLang(lang._chosenLocale);return lang._chosenLocale;void 0}}
getDiscordLang();`);
}
declare global {
interface Window {
armcord: any;
@ -30,7 +34,7 @@ if (window.location.href.indexOf("splash.html") > -1) {
if (ipcRenderer.sendSync("tabs")) {
injectTabs();
}
sleep(5000).then(() => {
sleep(5000).then(async () => {
const cssPath = path.join(__dirname, "../", "/content/css/discord.css");
addStyle(fs.readFileSync(cssPath, "utf8"));
@ -38,14 +42,17 @@ if (window.location.href.indexOf("splash.html") > -1) {
case "goosemod":
injectJS(clientMods.goosemod);
console.log("Loading GooseMod...");
await updateLang();
break;
case "cumcord":
injectJS(clientMods.cumcord);
console.log("Loading Cumcord...");
await updateLang();
break;
case "flicker":
injectJS(clientMods.flicker);
console.log("Loading FlickerMod...");
await updateLang();
break;
}
});

View file

@ -1,7 +1,7 @@
import * as fs from "fs";
import {app, dialog} from "electron";
import path from "path";
import { defaultMaxListeners } from "events";
import {defaultMaxListeners} from "events";
export var firstRun: boolean;
export var isSetup: boolean;
export var contentPath: string;
@ -88,8 +88,8 @@ export async function injectElectronFlags() {
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
const presets = {
'performance': `--enable-gpu-rasterization --enable-zero-copy --ignore-gpu-blocklist --enable-hardware-overlays=single-fullscreen,single-on-top,underlay --enable-features=EnableDrDc,CanvasOopRasterization,BackForwardCache:TimeToLiveInBackForwardCacheInSeconds/300/should_ignore_blocklists/true/enable_same_site/true,ThrottleDisplayNoneAndVisibilityHiddenCrossOriginIframes,UseSkiaRenderer,WebAssemblyLazyCompilation --disable-features=Vulkan --force_high_performance_gpu`, // Performance
'battery': '--enable-features=TurnOffStreamingMediaCachingOnBattery --force_low_power_gpu' // Known to have better battery life for Chromium?
performance: `--enable-gpu-rasterization --enable-zero-copy --ignore-gpu-blocklist --enable-hardware-overlays=single-fullscreen,single-on-top,underlay --enable-features=EnableDrDc,CanvasOopRasterization,BackForwardCache:TimeToLiveInBackForwardCacheInSeconds/300/should_ignore_blocklists/true/enable_same_site/true,ThrottleDisplayNoneAndVisibilityHiddenCrossOriginIframes,UseSkiaRenderer,WebAssemblyLazyCompilation --disable-features=Vulkan --force_high_performance_gpu`, // Performance
battery: "--enable-features=TurnOffStreamingMediaCachingOnBattery --force_low_power_gpu" // Known to have better battery life for Chromium?
};
switch (await getConfig("performanceMode")) {
case "performance":
@ -104,6 +104,20 @@ export async function injectElectronFlags() {
console.log("No performance modes set");
}
}
export async function setLang(language: string) {
const userDataPath = app.getPath("userData");
const storagePath = path.join(userDataPath, "/storage/");
const langConfigFile = storagePath + "lang.json";
if (!fs.existsSync(langConfigFile)) {
fs.writeFileSync(langConfigFile, "{}", "utf-8");
}
let rawdata = fs.readFileSync(langConfigFile, "utf-8");
let parsed = JSON.parse(rawdata);
parsed["lang"] = language;
let toSave = JSON.stringify(parsed);
fs.writeFileSync(langConfigFile, toSave, "utf-8");
}
//ArmCord Settings/Storage manager
export interface Settings {
@ -113,7 +127,7 @@ export interface Settings {
minimizeToTray: boolean;
automaticPatches: boolean;
mods: string;
performanceMode: string,
performanceMode: string;
blurType: string;
inviteWebsocket: boolean;
doneSetup: boolean;