Skip to content

Commit

Permalink
Merge branch 'spicetify:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Lseoksee authored Feb 21, 2024
2 parents 0befc9a + 5f77de5 commit 559b123
Showing 1 changed file with 100 additions and 2 deletions.
102 changes: 100 additions & 2 deletions jsHelper/spicetifyWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,99 @@ window.Spicetify = {
Platform: {}
};

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

const corsProxyURL = "https://cors-proxy.spicetify.app/";
const allowedMethodsMap = {
get: "get",
post: "post",
del: "delete",
put: "put",
patch: "patch"
};
const allowedMethodsSet = new Set(Object.keys(allowedMethodsMap));
const internalEndpoints = new Set(["sp:", "wg:"]);

const handler = {
// biome-ignore lint/complexity/useArrowFunction: <explanation>

Check warning on line 324 in jsHelper/spicetifyWrapper.js

View workflow job for this annotation

GitHub Actions / linter

Suppression comment is not being used

Check warning on line 324 in jsHelper/spicetifyWrapper.js

View workflow job for this annotation

GitHub Actions / linter

Suppression comment is not being used
get: function (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;

// biome-ignore lint/complexity/useArrowFunction: <explanation>
return async function (url, body) {
const urlObj = new URL(url);

const isWebAPI = urlObj.hostname === "api.spotify.com";
const isSpClientAPI = urlObj.hostname.includes("spotify.com") && urlObj.hostname.includes("spclient");
const isInternalURL = internalEndpoints.has(urlObj.protocol);
// biome-ignore lint/style/noArguments: <explanation>
if (isInternalURL) return internalFetch.apply(this, arguments);

const shouldUseCORSProxy = !isWebAPI && !isSpClientAPI && !isInternalURL;

const method = allowedMethodsMap[prop.toLowerCase()];
const headers = {
"Content-Type": "application/json"
};

const options = {
method,
headers,
timeout: 1000 * 15
};

function isJson(str) {
try {
JSON.parse(str);
} catch {
return false;
}
return true;
}

let finalURL = urlObj.toString();
if (body) {
if (method === "get") {
const params = new URLSearchParams(body);
finalURL += `?${params.toString()}`;
} else options.body = isJson ? JSON.stringify(body) : body;
}
if (shouldUseCORSProxy) finalURL = `${corsProxyURL}${finalURL}`;

const Authorization = `Bearer ${Spicetify.Platform.AuthorizationAPI.getState().token.accessToken}`;
let injectedHeaders = {};
if (isWebAPI) injectedHeaders = { Authorization };
if (isSpClientAPI) {
injectedHeaders = {
Authorization,
"Spotify-App-Version": Spicetify.Platform.version,
"App-Platform": Spicetify.Platform.PlatformData.app_platform
};
}
Object.assign(options.headers, injectedHeaders);

try {
return fetch(finalURL, options).then(res =>
res.json().catch(() => {
return { status: res.status };
})
);
} catch (e) {
console.error(e);
}
};
}
};

Spicetify.Player.origin._cosmos = new Proxy(Spicetify.Player.origin._cosmos, handler);
})();

(function waitForPlatform() {
if (!Spicetify._platform) {
setTimeout(waitForPlatform, 50);
Expand All @@ -326,7 +419,11 @@ window.Spicetify = {
}
// Force all webpack modules to load
const require = webpackChunkopen.push([[Symbol()], {}, re => re]);
const chunks = Object.entries(require.m);
const chunks = require.m ? Object.entries(require.m) : [];
if (!chunks) {
setTimeout(hotloadWebpackModules, 50);
return;
}
const cache = Object.keys(require.m).map(id => require(id));
const modules = cache
.filter(module => typeof module === "object")
Expand Down Expand Up @@ -1552,7 +1649,8 @@ Spicetify.ContextMenuV2 = (() => {
})();

Spicetify.Menu = (() => {
const shouldAdd = (_, trigger, target) => trigger === "click" && target.closest(".main-topBar-container");
const shouldAdd = (_, trigger, target) =>
trigger === "click" && (target.classList.contains("main-userWidget-boxCondensed") || target.classList.contains("main-userWidget-box"));

class Item extends Spicetify.ContextMenuV2.Item {
constructor(children, isEnabled, onClick, leadingIcon) {
Expand Down

0 comments on commit 559b123

Please sign in to comment.