Skip to content

Commit

Permalink
fix(wrapper): resolve missing APIs from Registry on 1.2.42 (spice…
Browse files Browse the repository at this point in the history
  • Loading branch information
rxri authored Jul 14, 2024
1 parent 3356b85 commit 372dded
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions jsHelper/spicetifyWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,55 @@ window.Spicetify = {
Platform: {}
};

(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];
}
}

if (!Spicetify.Platform.Registry) Spicetify.Events.platformLoaded.fire();
})();

(function addMissingPlatformAPIs() {
if (!Spicetify.Platform?.version && !Spicetify.Platform?.Registry) {
setTimeout(addMissingPlatformAPIs, 50);
return;
}
const version = Spicetify.Platform.version.split(".").map(i => Number.parseInt(i));
if (version[0] === 1 && version[1] === 2 && version[2] < 38) return;

for (const [key, _] of Spicetify.Platform.Registry._map.entries()) {
if (typeof key?.description !== "string" || !key?.description.endsWith("API")) continue;
const symbolName = key.description;
if (Object.hasOwn(Spicetify.Platform, symbolName)) continue;
const resolvedAPI = Spicetify.Platform.Registry.resolve(key);
if (!resolvedAPI) {
console.warn(`[spicetifyWrapper] Failed to resolve PlatformAPI from Registry: ${symbolName}`);
continue;
}

Spicetify.Platform[symbolName] = resolvedAPI;
console.debug(`[spicetifyWrapper] Resolved PlatformAPI from Registry: ${symbolName}`);
}

if (Spicetify.Events.platformLoaded.callbacks.length) Spicetify.Events.platformLoaded.fire();
})();

(function addProxyCosmos() {
if (!Spicetify.Player.origin?._cosmos && !Spicetify.Platform?.Registry) {
setTimeout(addProxyCosmos, 50);
return;
}

const _cosmos = Spicetify.Player.origin?._cosmos ?? Spicetify.Platform?.Registry._map.get(Symbol.for("Cosmos")).instance;
const _cosmos = Spicetify.Player.origin?._cosmos ?? Spicetify.Platform?.Registry.resolve(Symbol.for("Cosmos"));

const corsProxyURL = "https://cors-proxy.spicetify.app";
const allowedMethodsMap = {
Expand All @@ -332,7 +374,9 @@ window.Spicetify = {
get: (target, prop, receiver) => {
const internalFetch = Reflect.get(target, prop, receiver);

if (typeof internalFetch !== "function" || !allowedMethodsSet.has(prop) || Spicetify.Platform.version < "1.2.31") return internalFetch;
if (typeof internalFetch !== "function" || !allowedMethodsSet.has(prop)) return internalFetch;
const version = Spicetify.Platform.version.split(".").map(i => Number.parseInt(i));
if (version[0] === 1 && version[1] === 2 && version[2] < 31) return internalFetch;

return async function (url, body) {
const urlObj = new URL(url);
Expand Down Expand Up @@ -404,22 +448,6 @@ window.Spicetify = {
});
})();

(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];
}
}
Spicetify.Events.platformLoaded.fire();
})();

(function hotloadWebpackModules() {
if (!window?.webpackChunkclient_web) {
setTimeout(hotloadWebpackModules, 50);
Expand Down

0 comments on commit 372dded

Please sign in to comment.