1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2026-02-04 13:07:00 +00:00
This commit is contained in:
c-basalt
2025-03-01 17:00:05 -05:00
parent d0956644d8
commit 5d36f8789b
5 changed files with 33 additions and 20 deletions

View File

@@ -2,6 +2,7 @@
from .native import JSInterpreter
from .common import _JSI_PREFERENCES, _JSI_HANDLERS, JSIWrapper
from ._phantomjs import PhantomJSwrapper
from . import _deno # ensure jsi registration
__all__ = [

View File

@@ -74,6 +74,7 @@ def random_string(length: int = 10) -> str:
def override_navigator_js(user_agent: str) -> str:
"""Generate js snippet to override navigator properties based on user_agent string"""
return '\n'.join([
'Object.defineProperty(navigator, "%s", { value: %s, configurable: true });' % (k, json.dumps(v))
for k, v in {
@@ -113,7 +114,7 @@ def extract_script_tags(html: str) -> tuple[str, list[str]]:
def prepare_wasm_jsmodule(js_mod: str, wasm: bytes) -> str:
"""
Prepare wasm init for js wrapper module generated by rust wasm-pack
Sanitize js wrapper module generated by rust wasm-pack for wasm init
removes export and import.meta and inlines wasm binary as Uint8Array
See test/test_data/jsi_external/hello_wasm.js for example

View File

@@ -31,6 +31,18 @@ def get_jsi_keys(jsi_or_keys: typing.Iterable[str | type[JSI] | JSI]) -> list[st
return [jok if isinstance(jok, str) else jok.JSI_KEY for jok in jsi_or_keys]
def filter_jsi_keys(features=None, only_include=None, exclude=None):
keys = list(_JSI_HANDLERS)
if features:
keys = [key for key in keys if key in _JSI_HANDLERS
and _JSI_HANDLERS[key]._SUPPORTED_FEATURES.issuperset(features)]
if only_include:
keys = [key for key in keys if key in get_jsi_keys(only_include)]
if exclude:
keys = [key for key in keys if key not in get_jsi_keys(exclude)]
return keys
def filter_jsi_include(only_include: typing.Iterable[str] | None, exclude: typing.Iterable[str] | None):
keys = get_jsi_keys(only_include) if only_include else _JSI_HANDLERS.keys()
return [key for key in keys if key not in (exclude or [])]
@@ -123,9 +135,7 @@ class JSIWrapper:
self.report_warning(f'`{invalid_key}` is not a valid JSI, ignoring preference setting')
user_prefs.remove(invalid_key)
jsi_keys = filter_jsi_include(only_include, exclude)
self.write_debug(f'Allowed JSI keys: {jsi_keys}')
handler_classes = [_JSI_HANDLERS[key] for key in filter_jsi_feature(self._features, jsi_keys)]
handler_classes = [_JSI_HANDLERS[key] for key in filter_jsi_keys(self._features, only_include, exclude)]
self.write_debug(f'Select JSI for features={self._features}: {get_jsi_keys(handler_classes)}, '
f'included: {get_jsi_keys(only_include) or "all"}, excluded: {get_jsi_keys(exclude)}')
if not handler_classes:
@@ -208,12 +218,8 @@ class JSIWrapper:
@param html: html to load as document, requires `dom` feature
@param cookiejar: cookiejar to read and set cookies, requires `cookies` feature, pass `InfoExtractor.cookiejar` if you want to read and write cookies
"""
kwargs = filter_dict({
'note': note,
'html': html,
'cookiejar': cookiejar,
})
return self._dispatch_request('execute', jscode, video_id, **kwargs)
return self._dispatch_request('execute', jscode, video_id, **filter_dict({
'note': note, 'html': html, 'cookiejar': cookiejar}))
class JSI(abc.ABC):