dzunlock/dzunlock.user.js

238 lines
7.4 KiB
JavaScript
Raw Normal View History

2021-07-11 11:24:25 +00:00
// ==UserScript==
// @name dzunlock
// @namespace io.github.uhwot.dzunlock
2021-09-15 14:13:49 +00:00
// @description enables deezer hifi features lol
2021-07-11 11:24:25 +00:00
// @author uh wot
2021-09-15 15:43:53 +00:00
// @version 1.3.1
2021-07-13 11:30:22 +00:00
// @license GPL-3.0-only
2021-07-11 16:19:38 +00:00
// @homepageURL https://git.freezer.life/uhwot/dzunlock
// @downloadURL https://git.freezer.life/uhwot/dzunlock/raw/branch/master/dzunlock.user.js
2021-07-11 11:24:25 +00:00
// @icon https://cdns-files.dzcdn.net/cache/images/common/favicon/favicon-96x96.852baf648e79894b668670e115e4a375.png
2021-07-13 12:43:33 +00:00
// @include /^https:\/\/www\.deezer\.com\/[a-z]{2}\/($|track|album|artist|playlist|episode|show|profile|channels|podcasts|radio)/
2021-07-11 11:24:25 +00:00
// @match https://www.deezer.com/search/*
// @match https://www.deezer.com/account/*
2021-07-13 12:43:33 +00:00
// @match https://www.deezer.com/smarttracklist/*
2021-08-10 13:21:57 +00:00
// @require https://cdnjs.cloudflare.com/ajax/libs/aes-js/3.1.2/index.min.js
2021-07-13 12:41:25 +00:00
// @grant GM_getValue
2021-07-13 23:20:50 +00:00
// @run-at document-start
2021-07-11 11:24:25 +00:00
// ==/UserScript==
2021-07-11 16:26:04 +00:00
const debug = false
2021-07-11 11:24:25 +00:00
function log(...args) {
if (debug) {
return console.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
2021-08-10 13:21:57 +00:00
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)
2021-09-15 14:13:49 +00:00
const quality_to_format = {
"standard": "MP3_128",
"high": "MP3_320",
"lossless": "FLAC"
}
2021-08-10 13:21:57 +00:00
function str2bin(str) {
return Array.from(str).map(function (item) {
return item.charCodeAt(0);
})
}
function bin2str(bin) {
return String.fromCharCode.apply(String, bin);
}
function decryptHex(hex) {
hex = aesjs.utils.hex.toBytes(hex)
return bin2str(cipher.decrypt(hex)).replace(/\0+$/, '') // removes zero-padding
}
function encryptHex(str) {
// zero-padding
if (str.length % 16) {
str += '\x00'.repeat(16 - str.length % 16)
}
return aesjs.utils.hex.fromBytes(cipher.encrypt(str2bin(str)))
}
function playerTokenPatch(playerToken) {
playerToken = JSON.parse(decryptHex(playerToken))
// enables 320/flac quality selection
2021-09-15 14:13:49 +00:00
playerToken.audio_qualities.wifi_streaming = ['low', 'standard', 'high', 'lossless']
2021-08-10 13:21:57 +00:00
// disables previews
playerToken.streaming = true
playerToken.limited = false
log(playerToken)
return encryptHex(JSON.stringify(playerToken))
}
2021-07-11 11:24:25 +00:00
2021-07-13 23:20:50 +00:00
window.addEventListener('DOMContentLoaded', (_) => {
unsafeWindow.dzPlayer.setTrackList = (function (old) {
2021-09-15 14:13:49 +00:00
return function (data, ...args) {
// needed for deezer's player to accept 320/flac responses
2021-07-13 23:20:50 +00:00
2021-07-16 11:13:43 +00:00
for (let i = 0; i < data.data.length; i++) {
2021-09-15 14:13:49 +00:00
const id = parseInt(data.data[i].SNG_ID)
if (id >= 0) { // don't change filesizes on user-upped tracks
data.data[i].FILESIZE_MP3_320 = '1'
data.data[i].FILESIZE_FLAC = '1'
2021-07-13 23:20:50 +00:00
}
}
2021-07-11 11:24:25 +00:00
2021-07-16 11:13:43 +00:00
log(data)
2021-07-11 11:24:25 +00:00
2021-07-16 11:13:43 +00:00
return old(data, ...args)
2021-07-13 23:20:50 +00:00
};
})(unsafeWindow.dzPlayer.setTrackList);
});
2021-07-11 11:24:25 +00:00
fetchIntercept.register({
request: function (url, config) {
// Modify the url or config here
2021-09-15 14:13:49 +00:00
if (url === 'https://media.deezer.com/v1/get_url') {
const quality = unsafeWindow.dzPlayer.control.getAudioQuality()
const track = unsafeWindow.dzPlayer.getCurrentSong()
const id = parseInt(track.SNG_ID)
let is_subbed = !unsafeWindow.dzPlayer.user_status.can_subscribe
2021-09-15 15:43:53 +00:00
let is_quality_available = unsafeWindow.dzPlayer.user_status.audio_qualities.wifi_download.includes(quality)
2021-09-15 14:13:49 +00:00
// STREAM_ADS_AVAILABLE is used to check if track is restricted to premium/hifi
2021-09-15 15:43:53 +00:00
if (track.RIGHTS.STREAM_ADS_AVAILABLE !== true && !is_subbed) {
is_quality_available = false
2021-09-15 14:13:49 +00:00
}
if (id >= 0 && !is_quality_available) {
const media_server = GM_getValue('media_server', 'https://dzmedia.herokuapp.com')
url = `${media_server}/get_url`
const body = {
formats: ['FLAC', 'MP3_320', 'MP3_128', 'MP3_64', 'MP3_MISC'],
ids: [id]
}
for (let i = 0; i < body.formats.length; i++) {
if (body.formats[0] !== quality_to_format[quality]) {
body.formats.shift()
} else {
break
}
}
config.body = JSON.stringify(body)
}
}
2021-07-11 11:24:25 +00:00
return [url, config];
},
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')) {
2021-07-19 09:37:31 +00:00
let json = await response.json()
2021-07-11 11:24:25 +00:00
// removes upgrade popup stuff
json.results.USER.ENTRYPOINTS = {}
// needed to play premium-restricted albums like https://www.deezer.com/album/801279
json.results.OFFER_ID = 600
// disables ads
json.results.USER.OPTIONS.ads_display = false
json.results.USER.OPTIONS.ads_audio = false
2021-08-10 13:21:57 +00:00
json.results.PLAYER_TOKEN = playerTokenPatch(json.results.PLAYER_TOKEN)
2021-07-11 11:24:25 +00:00
log(json)
return new Response(JSON.stringify(json))
}
2021-08-10 13:21:57 +00:00
if (response.url.startsWith('https://www.deezer.com/ajax/gw-light.php?method=log.listen')) {
const json = await response.json()
if (typeof json.results === 'string') {
json.results = playerTokenPatch(json.results)
}
return new Response(JSON.stringify(json))
}
2021-07-11 11:24:25 +00:00
return response;
},
responseError: function (error) {
// Handle an fetch error
return Promise.reject(error);
}
2021-08-10 13:21:57 +00:00
});