2022-10-22 01:17:06 +02:00
/ *
* Vencord , a modification for Discord ' s desktop app
* Copyright ( c ) 2022 Vendicated and contributors
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < https : //www.gnu.org/licenses/>.
* /
2023-05-02 02:50:51 +02:00
/// <reference path="../src/modules.d.ts" />
/// <reference path="../src/globals.d.ts" />
2025-11-16 15:17:35 +01:00
// Be very careful with imports in this file to avoid circular dependency issues.
// Only import pure modules that don't import other parts of Vencord.
2024-05-26 19:12:18 +02:00
import monacoHtmlLocal from "file://monacoWin.html?minify" ;
2025-11-16 15:17:35 +01:00
import * as DataStore from "@api/DataStore" ;
import type { Settings } from "@api/Settings" ;
2026-07-13 21:00:59 -04:00
import { getThemeInfo } from "@main/themes" ;
2025-11-16 15:17:35 +01:00
import { debounce } from "@shared/debounce" ;
import { localStorage } from "@utils/localStorage" ;
2025-06-07 00:06:01 +02:00
import { getStylusWebStoreUrl } from "@utils/web" ;
2025-12-20 01:02:43 +01:00
import { EXTENSION_BASE_URL , metaReady , RENDERER_CSS_URL } from "@utils/web-metadata" ;
2022-10-21 23:58:41 +02:00
// listeners for ipc.on
2023-05-02 02:50:51 +02:00
const cssListeners = new Set < ( css : string ) = > void > ( ) ;
const NOOP = ( ) = > { } ;
const NOOP_ASYNC = async ( ) = > { } ;
2022-10-04 00:52:42 +02:00
2023-05-02 02:50:51 +02:00
const setCssDebounced = debounce ( ( css : string ) = > VencordNative . quickCss . set ( css ) ) ;
2022-10-04 00:52:42 +02:00
2024-07-13 00:25:18 -04:00
const themeStore = DataStore . createStore ( "VencordThemes" , "VencordThemeData" ) ;
2023-08-04 18:52:20 +01:00
2023-05-02 02:50:51 +02:00
// probably should make this less cursed at some point
window . VencordNative = {
2023-08-04 18:52:20 +01:00
themes : {
uploadTheme : ( fileName : string , fileData : string ) = > DataStore . set ( fileName , fileData , themeStore ) ,
deleteTheme : ( fileName : string ) = > DataStore . del ( fileName , themeStore ) ,
getThemesList : ( ) = > DataStore . entries ( themeStore ) . then ( entries = >
2026-07-13 20:56:29 -04:00
entries . map ( ( [ name , css ] ) = > getThemeInfo ( css , name . toString ( ) ) )
2023-08-04 18:52:20 +01:00
) ,
2023-09-25 18:53:10 +02:00
getThemeData : ( fileName : string ) = > DataStore . get ( fileName , themeStore ) ,
getSystemValues : async ( ) = > ( { } ) ,
2025-06-14 18:51:40 +02:00
openFolder : async ( ) = > Promise . reject ( "themes:openFolder is not supported on web" ) ,
2023-08-04 18:52:20 +01:00
} ,
2023-05-02 02:50:51 +02:00
native : {
getVersions : ( ) = > ( { } ) ,
2026-05-18 04:28:46 +02:00
supportsWindowsMaterial : ( ) = > false ,
2025-12-20 01:02:43 +01:00
openExternal : async ( url ) = > void open ( url , "_blank" ) ,
getRendererCss : async ( ) = > {
if ( IS_USERSCRIPT )
// need to wait for next tick for _vcUserScriptRendererCss to be set
return Promise . resolve ( ) . then ( ( ) = > window . _vcUserScriptRendererCss ) ;
await metaReady ;
return fetch ( RENDERER_CSS_URL )
. then ( res = > res . text ( ) ) ;
} ,
onRendererCssUpdate : NOOP ,
2022-10-21 23:58:41 +02:00
} ,
2023-05-02 02:50:51 +02:00
updater : {
2024-04-17 14:29:47 -04:00
getRepo : async ( ) = > ( { ok : true , value : "https://github.com/Equicord/Equicord" } ) ,
2023-05-02 02:50:51 +02:00
getUpdates : async ( ) = > ( { ok : true , value : [ ] } ) ,
2026-10-03 16:35:13 -04:00
fetchUpdate : async ( ) = > ( { ok : true , value : false } ) ,
applyUpdate : async ( ) = > ( { ok : true , value : true } ) ,
2023-05-02 02:50:51 +02:00
} ,
2022-10-04 00:52:42 +02:00
2023-05-02 02:50:51 +02:00
quickCss : {
get : ( ) = > DataStore . get ( "VencordQuickCss" ) . then ( s = > s ? ? "" ) ,
set : async ( css : string ) = > {
await DataStore . set ( "VencordQuickCss" , css ) ;
cssListeners . forEach ( l = > l ( css ) ) ;
} ,
addChangeListener ( cb ) {
cssListeners . add ( cb ) ;
} ,
2023-08-04 18:52:20 +01:00
addThemeChangeListener : NOOP ,
2023-05-02 02:50:51 +02:00
openFile : NOOP_ASYNC ,
async openEditor() {
2025-06-07 00:06:01 +02:00
if ( IS_USERSCRIPT ) {
const shouldOpenWebStore = confirm ( "QuickCSS is not supported on the Userscript. You can instead use the Stylus extension.\n\nDo you want to open the Stylus web store page?" ) ;
if ( shouldOpenWebStore ) {
window . open ( getStylusWebStoreUrl ( ) , "_blank" ) ;
}
return ;
}
2023-05-02 02:50:51 +02:00
const features = ` popup,width= ${ Math . min ( window . innerWidth , 1000 ) } ,height= ${ Math . min ( window . innerHeight , 1000 ) } ` ;
const win = open ( "about:blank" , "VencordQuickCss" , features ) ;
if ( ! win ) {
alert ( "Failed to open QuickCSS popup. Make sure to allow popups!" ) ;
return ;
}
2022-10-04 00:52:42 +02:00
2023-09-19 04:07:24 +02:00
win . baseUrl = EXTENSION_BASE_URL ;
2023-05-02 02:50:51 +02:00
win . setCss = setCssDebounced ;
win . getCurrentCss = ( ) = > VencordNative . quickCss . get ( ) ;
2025-12-20 01:02:43 +01:00
win . getTheme = this . getEditorTheme ;
2022-10-04 00:52:42 +02:00
2025-06-07 00:06:01 +02:00
win . document . write ( monacoHtmlLocal ) ;
2022-10-04 00:52:42 +02:00
} ,
2025-12-20 01:02:43 +01:00
getEditorTheme : ( ) = > {
const { getTheme , Theme } = require ( "@utils/discord" ) ;
return getTheme ( ) === Theme . Light
? "vs-light"
: "vs-dark" ;
}
2022-10-04 00:52:42 +02:00
} ,
2023-05-02 02:50:51 +02:00
settings : {
2024-03-13 21:45:45 +01:00
get : ( ) = > {
try {
2024-07-13 00:06:01 -04:00
return JSON . parse ( localStorage . getItem ( "EquicordSettings" ) || "{}" ) ;
2024-03-13 21:45:45 +01:00
} catch ( e ) {
console . error ( "Failed to parse settings from localStorage: " , e ) ;
return { } ;
}
} ,
2024-07-13 00:06:01 -04:00
set : async ( s : Settings ) = > localStorage . setItem ( "EquicordSettings" , JSON . stringify ( s ) ) ,
2025-06-16 16:17:52 -04:00
getSettingsDir : async ( ) = > "LocalStorage" ,
2025-06-14 18:51:40 +02:00
openFolder : async ( ) = > Promise . reject ( "settings:openFolder is not supported on web" ) ,
2023-08-04 18:52:20 +01:00
} ,
pluginHelpers : { } as any ,
2025-06-12 02:19:45 +02:00
csp : { } as any ,
2022-10-04 00:52:42 +02:00
} ;