mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-26 16:18:51 +00:00
feat: add settings page parser (#154)
* feat: add settings page parsers * fix(AccountManager): small ts error * feat: add `CopyLink` & `SettingsCheckbox` * deps: remove “flat” dependency
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import Flatten from 'flat';
|
||||
import package_json from '../../package.json';
|
||||
import { FetchFunction } from './HTTPClient';
|
||||
import userAgents from './user-agents.json';
|
||||
@@ -31,24 +30,6 @@ export class OAuthError extends InnertubeError { }
|
||||
export class PlayerError extends Error { }
|
||||
export class SessionError extends Error { }
|
||||
|
||||
/**
|
||||
* Utility to help access deep properties of an object.
|
||||
* @param obj - the object.
|
||||
* @param key - key of the property being accessed.
|
||||
* @param target - anything that might be inside of the property.
|
||||
* @param depth - maximum number of nested objects to flatten.
|
||||
* @param safe - if set to true arrays will be preserved.
|
||||
*/
|
||||
export function findNode(obj: any, key: string, target: string, depth: number, safe = true): any | any[] {
|
||||
const flat_obj = Flatten(obj, { safe, maxDepth: depth || 2 }) as any;
|
||||
const result = Object.keys(flat_obj).find((entry) => entry.includes(key) && JSON.stringify(flat_obj[entry] || '{}').includes(target));
|
||||
if (!result)
|
||||
throw new ParsingError(`Expected to find "${key}" with content "${target}" but got ${result}`, {
|
||||
key, target, data_snippet: `${JSON.stringify(flat_obj, null, 4).slice(0, 300)}..`
|
||||
});
|
||||
return flat_obj[result];
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares given objects. May not work correctly for
|
||||
* objects with methods.
|
||||
@@ -295,4 +276,4 @@ export const debugFetch: FetchFunction = (input, init) => {
|
||||
|
||||
export function u8ToBase64(u8: Uint8Array) {
|
||||
return btoa(String.fromCharCode.apply(null, Array.from(u8)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user