replaced fetch library crap

This commit is contained in:
uh wot 2022-03-18 20:40:02 +01:00
parent 795b7fd1bb
commit 7891c8a093
Signed by: uhwot
GPG Key ID: CB2454984587B781
1 changed files with 14 additions and 92 deletions

View File

@ -3,7 +3,7 @@
// @namespace io.github.uhwot.dzunlock
// @description enables deezer hifi features lol
// @author uh wot
// @version 1.3.5
// @version 1.3.6
// @license GPL-3.0-only
// @homepageURL https://git.freezerapp.xyz/uhwot/dzunlock
// @downloadURL https://git.freezerapp.xyz/uhwot/dzunlock/raw/branch/master/dzunlock.user.js
@ -26,65 +26,6 @@ function log(...args) {
}
}
// https://github.com/werk85/fetch-intercept/blob/develop/src/attach.js modified for browser support
let interceptors = [];
function interceptor(fetch, ...args) {
const reversedInterceptors = interceptors.reduce((array, interceptor) => [interceptor].concat(array), []);
let promise = Promise.resolve(args);
// Register request interceptors
reversedInterceptors.forEach(({ request, requestError }) => {
if (request || requestError) {
promise = promise.then(args => request(...args), requestError);
}
});
// Register fetch call
promise = promise.then(args => {
const request = new Request(...args);
return fetch(request).then(response => {
response.request = request;
return response;
}).catch(error => {
error.request = request;
return Promise.reject(error);
});
});
// Register response interceptors
reversedInterceptors.forEach(({ response, responseError }) => {
if (response || responseError) {
promise = promise.then(response, responseError);
}
});
return promise;
}
unsafeWindow.fetch = (function (fetch) {
return function (...args) {
return interceptor(fetch, ...args);
};
})(unsafeWindow.fetch);
fetchIntercept = {
register: function (interceptor) {
interceptors.push(interceptor);
return () => {
const index = interceptors.indexOf(interceptor);
if (index >= 0) {
interceptors.splice(index, 1);
}
};
},
clear: function () {
interceptors = [];
}
};
// main code starts here
const playerTokenKey = [102, 228, 95, 242, 215, 50, 122, 26, 57, 216, 206, 38, 164, 237, 200, 85]
const cipher = new aesjs.ModeOfOperation.ecb(playerTokenKey)
@ -154,9 +95,8 @@ window.addEventListener('DOMContentLoaded', (_) => {
})(unsafeWindow.dzPlayer.setTrackList);
});
fetchIntercept.register({
request: function (url, config) {
// Modify the url or config here
unsafeWindow.fetch = (function (fetch) {
return async function (url, init) {
if (url === 'https://media.deezer.com/v1/get_url') {
const quality = unsafeWindow.dzPlayer.control.getAudioQuality()
const track = unsafeWindow.dzPlayer.getCurrentSong()
@ -186,24 +126,14 @@ fetchIntercept.register({
}
}
config.body = JSON.stringify(body)
init.body = JSON.stringify(body)
}
}
return [url, config];
},
let resp = await fetch(url, init)
requestError: function (error) {
// Called when an error occured during another 'request' interceptor call
return Promise.reject(error);
},
response: async function (response) {
// Modify the response object
if (response.url.startsWith('https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData')) {
let json = await response.json()
if (url.startsWith('https://www.deezer.com/ajax/gw-light.php?method=deezer.getUserData')) {
let json = await resp.json()
// removes upgrade popup stuff
json.results.USER.ENTRYPOINTS = {}
@ -217,25 +147,17 @@ fetchIntercept.register({
log(json)
return new Response(JSON.stringify(json))
}
if (response.url.startsWith('https://www.deezer.com/ajax/gw-light.php?method=log.listen')) {
const json = await response.json()
resp = new Response(JSON.stringify(json), resp)
} else if (url.startsWith('https://www.deezer.com/ajax/gw-light.php?method=log.listen')) {
const json = await resp.json()
if (typeof json.results === 'string') {
json.results = playerTokenPatch(json.results)
}
return new Response(JSON.stringify(json))
resp = new Response(JSON.stringify(json), resp)
}
return response;
},
responseError: function (error) {
// Handle an fetch error
return Promise.reject(error);
}
});
return resp
};
})(unsafeWindow.fetch);