fix for tampermonkey/greasemonkey

This commit is contained in:
uh wot 2021-07-14 01:20:50 +02:00
parent a46482057a
commit de585b2ff8
Signed by: uhwot
GPG Key ID: CB2454984587B781
1 changed files with 54 additions and 51 deletions

View File

@ -3,7 +3,7 @@
// @namespace io.github.uhwot.dzunlock // @namespace io.github.uhwot.dzunlock
// @description enables deezer hifi features lol // @description enables deezer hifi features lol
// @author uh wot // @author uh wot
// @version 1.1.4 // @version 1.1.5
// @license GPL-3.0-only // @license GPL-3.0-only
// @homepageURL https://git.freezer.life/uhwot/dzunlock // @homepageURL https://git.freezer.life/uhwot/dzunlock
// @downloadURL https://git.freezer.life/uhwot/dzunlock/raw/branch/master/dzunlock.user.js // @downloadURL https://git.freezer.life/uhwot/dzunlock/raw/branch/master/dzunlock.user.js
@ -15,6 +15,7 @@
// @require https://cdnjs.cloudflare.com/ajax/libs/aes-js/3.1.2/index.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/aes-js/3.1.2/index.min.js
// @grant GM_getValue // @grant GM_getValue
// @grant GM_setValue // @grant GM_setValue
// @run-at document-start
// ==/UserScript== // ==/UserScript==
const debug = false const debug = false
@ -89,8 +90,8 @@ const clientSecret = '2f5b4c9785ddc367975b83d90dc46f5c'
const playerTokenKey = [102, 228, 95, 242, 215, 50, 122, 26, 57, 216, 206, 38, 164, 237, 200, 85] const playerTokenKey = [102, 228, 95, 242, 215, 50, 122, 26, 57, 216, 206, 38, 164, 237, 200, 85]
const formats = [ const formats = [
{api: '320', gw: 'MP3_320'}, { api: '320', gw: 'MP3_320' },
{api: 'flac', gw: 'FLAC'} { api: 'flac', gw: 'FLAC' }
] ]
function str2bin(str) { function str2bin(str) {
@ -140,66 +141,68 @@ async function renewAccessToken() {
return access_token return access_token
} }
unsafeWindow.dzPlayer.setTrackList = (function (old) { window.addEventListener('DOMContentLoaded', (_) => {
return async function (...args) { unsafeWindow.dzPlayer.setTrackList = (function (old) {
// don't get filesizes if account is hifi return async function (...args) {
if (unsafeWindow.dzPlayer.user_status.lossless) { // don't get filesizes if account is hifi
return old(...args) if (unsafeWindow.dzPlayer.user_status.lossless) {
} return old(...args)
let batchList = []
for (let i = 0; i < args[0].data.length; i++) {
const id = Number(args[0].data[i].SNG_ID)
if (id >= 0) { // we don't need filesizes for user-upped tracks
batchList.push({ 'relative_url': `/track/${id}` })
} }
}
// return if all the tracks are user-upped let batchList = []
if (batchList.length === 0) {
return old(...args)
}
batchList = JSON.stringify(batchList) for (let i = 0; i < args[0].data.length; i++) {
const id = Number(args[0].data[i].SNG_ID)
if (id >= 0) { // we don't need filesizes for user-upped tracks
batchList.push({ 'relative_url': `/track/${id}` })
}
}
let access_token = GM_getValue('access_token', false) // return if all the tracks are user-upped
const expiry = GM_getValue('access_token_expiry', 0) if (batchList.length === 0) {
if (!access_token || Math.floor(Date.now() / 1000) >= expiry) { return old(...args)
access_token = await renewAccessToken() }
}
url = `https://api.deezer.com/batch?methods=${batchList}&access_token=${access_token}` batchList = JSON.stringify(batchList)
let resp = await fetch(url)
let json = await resp.json() let access_token = GM_getValue('access_token', false)
const expiry = GM_getValue('access_token_expiry', 0)
if (!access_token || Math.floor(Date.now() / 1000) >= expiry) {
access_token = await renewAccessToken()
}
// renew access token if token is invalid
const error = json.batch_result[0].error
if (error && error.code === 300) {
access_token = await renewAccessToken()
url = `https://api.deezer.com/batch?methods=${batchList}&access_token=${access_token}` url = `https://api.deezer.com/batch?methods=${batchList}&access_token=${access_token}`
resp = await fetch(url) let resp = await fetch(url)
json = await resp.json() let json = await resp.json()
}
let userUppedSoFar = 0 // renew access token if token is invalid
const error = json.batch_result[0].error
for (let i = 0; i < args[0].data.length; i++) { if (error && error.code === 300) {
const id = Number(args[0].data[i].SNG_ID) access_token = await renewAccessToken()
if (id < 0) { // user-uploaded track url = `https://api.deezer.com/batch?methods=${batchList}&access_token=${access_token}`
userUppedSoFar++ resp = await fetch(url)
continue json = await resp.json()
} }
for (let j = 0; j < formats.length; j++) {
args[0].data[i]['FILESIZE_' + formats[j].gw] = json.batch_result[i - userUppedSoFar]['filesize_' + formats[j].api] let userUppedSoFar = 0
for (let i = 0; i < args[0].data.length; i++) {
const id = Number(args[0].data[i].SNG_ID)
if (id < 0) { // user-uploaded track
userUppedSoFar++
continue
}
for (let j = 0; j < formats.length; j++) {
args[0].data[i]['FILESIZE_' + formats[j].gw] = json.batch_result[i - userUppedSoFar]['filesize_' + formats[j].api]
}
} }
}
log(args) log(args)
return old(...args) return old(...args)
}; };
})(unsafeWindow.dzPlayer.setTrackList); })(unsafeWindow.dzPlayer.setTrackList);
});
fetchIntercept.register({ fetchIntercept.register({
request: function (url, config) { request: function (url, config) {