mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-07-04 03:51:00 +00:00
refactor: simplify the player class
This commit is contained in:
@@ -52,14 +52,17 @@ class NToken {
|
||||
transformations[data.index](transformations[param_index[0]], transformations[param_index[1]], base64_dia);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(`Could not transform n-token (${this.n}), download may be throttled:`, err.message);
|
||||
console.error(new Utils.ParsingError('Could not transform n-token, download may be throttled.', {
|
||||
original_token: this.n,
|
||||
stack: err.stack
|
||||
}));
|
||||
return this.n;
|
||||
}
|
||||
return n_token.join('');
|
||||
}
|
||||
|
||||
#getFunc(el) {
|
||||
return el.match(Constants.FUNCS_REGEX);
|
||||
return el.match(Constants.NTOKEN_REGEX.FUNCTIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,75 +1,90 @@
|
||||
'use strict';
|
||||
|
||||
const Constants = require('../utils/Constants');
|
||||
const QueryString = require('querystring');
|
||||
|
||||
class Signature {
|
||||
constructor(url, player) {
|
||||
constructor(url, sig_decipher_sc) {
|
||||
this.url = url;
|
||||
this.player = player;
|
||||
this.func_regex = /(.{2}):function\(.*?\){(.*?)}/g;
|
||||
this.actions_regex = /;.{2}\.(.{2})\(.*?,(.*?)\)/g;
|
||||
this.sig_decipher_sc = sig_decipher_sc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deciphers signature.
|
||||
* @returns {string}
|
||||
*/
|
||||
decipher() {
|
||||
let actions;
|
||||
|
||||
const args = QueryString.parse(this.url);
|
||||
const signature = args.s.split('');
|
||||
|
||||
const functions = this.#getFunctions();
|
||||
|
||||
function splice(arr, end) {
|
||||
arr.splice(0, end);
|
||||
}
|
||||
|
||||
function swap(arr, index) {
|
||||
let origArrI = arr[0];
|
||||
arr[0] = arr[index % arr.length];
|
||||
arr[index % arr.length] = origArrI;
|
||||
}
|
||||
|
||||
function reverse(arr) {
|
||||
arr.reverse();
|
||||
}
|
||||
|
||||
let actions;
|
||||
let signature = args.s.split('');
|
||||
|
||||
while ((actions = this.actions_regex.exec(this.player.sig_decipher_sc)) !== null) {
|
||||
switch (actions[1]) {
|
||||
/**
|
||||
* Decides what function should be used to modify the
|
||||
* the signature.
|
||||
*/
|
||||
while ((actions = Constants.SIG_REGEX.ACTIONS.exec(this.sig_decipher_sc)) !== null) {
|
||||
const action = actions.groups;
|
||||
switch (action.name) {
|
||||
case functions[0]:
|
||||
reverse(signature, actions[2]);
|
||||
this.#reverse(signature);
|
||||
break;
|
||||
case functions[1]:
|
||||
splice(signature, actions[2]);
|
||||
this.#splice(signature, action.param);
|
||||
break;
|
||||
case functions[2]:
|
||||
swap(signature, actions[2]);
|
||||
this.#swap(signature, action.param);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
const url_components = new URL(args.url);
|
||||
args.sp ? url_components.searchParams.set(args.sp, signature.join('')) : url_components.searchParams.set('signature', signature.join(''));
|
||||
|
||||
args.sp ?
|
||||
url_components.searchParams.set(args.sp, signature.join('')) :
|
||||
url_components.searchParams.set('signature', signature.join(''));
|
||||
|
||||
return url_components.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extracts the functions used to modify the signature
|
||||
* and returns them in the correct order.
|
||||
*
|
||||
* @returns {Array.<string>}
|
||||
*/
|
||||
#getFunctions() {
|
||||
let func;
|
||||
let func_name = [];
|
||||
|
||||
while ((func = this.func_regex.exec(this.player.sig_decipher_sc)) !== null) {
|
||||
if (func[0].includes('reverse()')) {
|
||||
func_name[0] = func[1];
|
||||
let functions = [];
|
||||
|
||||
while ((func = Constants.SIG_REGEX.FUNCTIONS.exec(this.sig_decipher_sc)) !== null) {
|
||||
if (func[0].includes('reverse')) {
|
||||
functions[0] = func[1];
|
||||
} else if (func[0].includes('splice')) {
|
||||
func_name[1] = func[1];
|
||||
functions[1] = func[1];
|
||||
} else {
|
||||
func_name[2] = func[1];
|
||||
functions[2] = func[1];
|
||||
}
|
||||
}
|
||||
|
||||
return func_name;
|
||||
|
||||
return functions;
|
||||
}
|
||||
|
||||
#swap(arr, index) {
|
||||
let origArrI = arr[0];
|
||||
arr[0] = arr[index % arr.length];
|
||||
arr[index % arr.length] = origArrI;
|
||||
}
|
||||
|
||||
#splice(arr, end) {
|
||||
arr.splice(0, end);
|
||||
}
|
||||
|
||||
#reverse(arr) {
|
||||
arr.reverse();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user