Skip to content

Commit

Permalink
feat: simplify platform exposition (#2753)
Browse files Browse the repository at this point in the history
  • Loading branch information
SunsetTechuila authored Jan 10, 2024
1 parent a3444f8 commit 0665058
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CustomApps/lyrics-plus/PlaybarButton.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(function PlaybarButton() {
if (!Spicetify.Platform) {
if (!Spicetify.Platform.History) {
setTimeout(PlaybarButton, 300);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Extensions/webnowplaying.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <reference path="../globals.d.ts" />

(function WebNowPlaying() {
if (!Spicetify.CosmosAsync || !Spicetify.Platform) {
if (!Spicetify.CosmosAsync || !Spicetify.Platform.LibraryAPI) {
setTimeout(WebNowPlaying, 500);
return;
}
Expand Down
21 changes: 19 additions & 2 deletions jsHelper/spicetifyWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ window.Spicetify = {
"colorExtractor",
"test",
"Platform",
"_platform",
"getFontStyle",
"_fontStyle",
"Config",
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 5 additions & 14 deletions src/preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 0665058

Please sign in to comment.