-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixins.ts
161 lines (136 loc) · 3.72 KB
/
mixins.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (C) 2024 Delusoire
* SPDX-License-Identifier: GPL-3.0-or-later
*/
// @deno-types="./protocol.ts"
import { handleProtocol } from "./protocol.js";
// @deno-types="./transform.ts"
import { SourceFile, type Transformer } from "./transform.js";
// @deno-types="./util.ts"
import { matchLast } from "./util.js";
// @deno-types="./module.ts"
import { RootModule } from "./module.js";
type CHUNKS = Record<string, PromiseWithResolvers<void>>;
declare global {
var __applyTransforms: typeof applyTransforms;
var __interceptNavigationControlMessage: typeof interceptNavigationControlMessage;
var __onScriptLoaded: (path: string) => void;
var CHUNKS: CHUNKS;
}
export const applyTransforms = (path: string) => {
const source = SourceFile.from(path);
console.info("loadResource", source);
return source.getObjectURL();
};
globalThis.__applyTransforms = applyTransforms;
const spotifyAppScheme = "spotify:app:";
function interceptNavigationControlMessage(e: Event): boolean {
const uri: string = (e as any).data.data;
if (!uri.startsWith(spotifyAppScheme)) {
return false;
}
const url = new URL(uri.slice(spotifyAppScheme.length));
if (url.protocol !== "rpc:") {
return false;
}
if (url.pathname.startsWith("reload")) {
const modules = url.searchParams.getAll("module");
if (modules.length === 0) {
document.location.reload();
} else {
(async function () {
for (const identifier of modules) {
const instance = RootModule.INSTANCE.getDescendant(identifier)?.getEnabledInstance();
if (!instance) {
continue;
}
if (await instance.unload()) {
await instance.load();
}
}
})();
}
} else if (url.pathname.startsWith("spicetify")) {
handleProtocol(url.pathname);
}
return true;
}
globalThis.__interceptNavigationControlMessage = interceptNavigationControlMessage;
export const CHUNKS: CHUNKS = globalThis.CHUNKS = {};
globalThis.__onScriptLoaded = (path: string) => {
CHUNKS[path] ??= Promise.withResolvers();
setTimeout(CHUNKS[path].resolve);
};
export default async function (transformer: Transformer) {
transformer(
(emit) => (str) => {
emit();
str = str.replace(
/(([a-zA-Z_\$][\w\$]*)=([a-zA-Z_\$][\w\$]*)\.p\+\3\.u\([a-zA-Z_\$][\w\$]*\))/,
"$1,$2=await __applyTransforms($2)",
);
const i = str.search('"Loading chunk "');
const { index } = matchLast(
str.slice(0, i),
/=\([a-zA-Z_\$][\w\$]*,[a-zA-Z_\$][\w\$]*\)=>\{/g,
);
str = `${str.slice(0, index! + 1)}async${str.slice(index! + 1)}`;
str = str.replace(
/(new Promise\(\()(\([a-zA-Z_\$][\w\$]*,[a-zA-Z_\$][\w\$]*\)=>\{var ([a-zA-Z_\$][\w\$]*)=([a-zA-Z_\$][\w\$]*)\.miniCssF\([a-zA-Z_\$][\w\$]*\),([a-zA-Z_\$][\w\$]*)=\4\.p\+\3)/,
"$1async$2,$5=await __applyTransforms($5)",
);
return str;
},
{
glob: /^\/xpui\.js/,
},
);
transformer(
(emit) => (str) => {
emit();
str = str.replace("/864e5<30", "<0");
return str;
},
{
glob: /^\/xpui\.js/,
},
);
transformer(
(emit) => (str) => {
emit();
str = str.replace(/("incognito-enabled":[a-zA-Z_\$][\w\$]*)/, '$1,employee:"1"');
str = str.replace(
/([a-zA-Z_\$][\w\$]*)\("app\.enable-developer-mode",([a-zA-Z_\$][\w\$]*)\)/,
'$1("app.enable-developer-mode",$2);$1("app-developer",$2?2:0)',
);
return str;
},
{
glob: /^\/xpui\.js/,
},
);
transformer(
(emit) => (str) => {
emit();
str = str.replace(
/(([a-zA-Z_\$][\w\$]*)\.data\.type===(?:[a-zA-Z_\$][\w\$]*\.){2}NAVIGATION)&&/,
"$1&&!__interceptNavigationControlMessage($2)&&",
);
return str;
},
{
glob: /^\/xpui\.js/,
},
);
transformer(
(emit) => (str, path) => {
emit();
str += `;\n__onScriptLoaded("${path}");`;
return str;
},
{
glob: /\.js$/,
wait: false,
},
);
}