From 0665058173eb0209595c408c563e9586167c129c Mon Sep 17 00:00:00 2001 From: Grigory Date: Wed, 10 Jan 2024 18:13:55 +0500 Subject: [PATCH] feat: simplify platform exposition (#2753) --- CustomApps/lyrics-plus/PlaybarButton.js | 2 +- Extensions/webnowplaying.js | 2 +- jsHelper/spicetifyWrapper.js | 21 +++++++++++++++++++-- src/preprocess/preprocess.go | 19 +++++-------------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/CustomApps/lyrics-plus/PlaybarButton.js b/CustomApps/lyrics-plus/PlaybarButton.js index 52aede851c..1d3465a648 100644 --- a/CustomApps/lyrics-plus/PlaybarButton.js +++ b/CustomApps/lyrics-plus/PlaybarButton.js @@ -1,5 +1,5 @@ (function PlaybarButton() { - if (!Spicetify.Platform) { + if (!Spicetify.Platform.History) { setTimeout(PlaybarButton, 300); return; } diff --git a/Extensions/webnowplaying.js b/Extensions/webnowplaying.js index d15f0144bc..2d310e8794 100644 --- a/Extensions/webnowplaying.js +++ b/Extensions/webnowplaying.js @@ -5,7 +5,7 @@ /// (function WebNowPlaying() { - if (!Spicetify.CosmosAsync || !Spicetify.Platform) { + if (!Spicetify.CosmosAsync || !Spicetify.Platform.LibraryAPI) { setTimeout(WebNowPlaying, 500); return; } diff --git a/jsHelper/spicetifyWrapper.js b/jsHelper/spicetifyWrapper.js index 8b93e0a593..e16d8cb033 100644 --- a/jsHelper/spicetifyWrapper.js +++ b/jsHelper/spicetifyWrapper.js @@ -173,6 +173,7 @@ window.Spicetify = { "colorExtractor", "test", "Platform", + "_platform", "getFontStyle", "_fontStyle", "Config", @@ -292,10 +293,26 @@ window.Spicetify = { ReactComponent: {}, ReactHook: {}, ReactFlipToolkit: {}, - Snackbar: {} + Snackbar: {}, + Platform: {} }; -(async function hotloadWebpackModules() { +(function waitForPlatform() { + if (!Spicetify._platform) { + setTimeout(() => waitForPlatform(), 50); + return; + } + const { _platform } = Spicetify; + for (const key of Object.keys(_platform)) { + if (key.startsWith("get") && typeof _platform[key] === "function") { + Spicetify.Platform[key.slice(3)] = _platform[key](); + } else { + Spicetify.Platform[key] = _platform[key]; + } + } +})(); + +(function hotloadWebpackModules() { if (!window?.webpackChunkopen) { setTimeout(hotloadWebpackModules, 50); return; diff --git a/src/preprocess/preprocess.go b/src/preprocess/preprocess.go index b7d6b24e4c..fd8e6faeda 100644 --- a/src/preprocess/preprocess.go +++ b/src/preprocess/preprocess.go @@ -310,20 +310,11 @@ func exposeAPIs_main(input string) string { `"data-testid":`, `"":`) - reAllAPIPromises := regexp.MustCompile(`return ?(?:function\(\))?(?:[\w$_\.&!=]+[\w$_\.()=!]+.)*\{(?:[ \w.$,(){}]+:[\w\d!$_.()]+,)*(?:return [\w.\(,\)}]+)?(?:get\w+:(?:[()=>{}\w]+new Promise[()=>{}]+),)?((?:get\w+:(?:\(\)=>|function\(\)\{return ?)(?:[\w$]+|[(){}]+)\}?,?)+?)[})]+;?`) - allAPIPromises := reAllAPIPromises.FindAllStringSubmatch(input, -1) - for _, found := range allAPIPromises { - splitted := strings.Split(found[1], ",") - if len(splitted) > 6 { - matchMap := regexp.MustCompile(`get(\w+):(?:\(\)=>|function\(\)\{return ?)([\w$]+|\(?\{\}\)?)\}?,?`) - code := "Spicetify.Platform={};" - for _, apiFunc := range splitted { - matches := matchMap.FindStringSubmatch(apiFunc) - code += "Spicetify.Platform[\"" + fmt.Sprint(matches[1]) + "\"]=" + fmt.Sprint(matches[2]) + ";" - } - input = strings.Replace(input, found[0], code+found[0], 1) - } - } + // Spicetify._platform + utils.Replace( + &input, + `(setTitlebarHeight[\w(){}.,&$!=;"" ]+)(\{version:\w+,)`, + `${1}Spicetify._platform=${2}`) // Profile Menu hook v1.1.56 utils.Replace(