jadefin/mods/PatchForceHLSJS.js

79 lines
2.6 KiB
JavaScript

//@ts-check
import JadefinIntegrity from '../JadefinIntegrity.js';
import Jadefin from "../Jadefin.js";
import JadefinMod from "../JadefinMod.js";
import JadefinModules from "../JadefinModules.js";
import JadefinUtils from "../JadefinUtils.js";
// Mainly spawned from https://github.com/jellyfin/jellyfin-android/issues/1031
export default JadefinIntegrity("PatchAndroidHLSJS", import.meta.url, () => new (class PatchAndroidHLSJS extends JadefinMod {
constructor() {
super();
this.enableHlsJsPlayer = this.enableHlsJsPlayer.bind(this);
}
get isEnabled() {
return this.storage.get("isEnabled2", true);
}
set isEnabled(value) {
this.storage.set("isEnabled2", value);
}
async init(name, url) {
await super.init(name, url);
let enableHlsJsPlayerKey = [];
const htmlMediaHelper = this.htmlMediaPlayer = Jadefin.findWebpackRawLoad(
e => JadefinUtils.findDeep(
e,
1,
(i, o) => o.toString().indexOf(`.canPlayType("application/vnd.apple.mpegURL").replace(/no/,"")`) != -1,
(i, v, k) => enableHlsJsPlayerKey.push(k)
) && 1
)[0];
this._enableHlsJsPlayer = htmlMediaHelper[enableHlsJsPlayerKey[0]].bind(htmlMediaHelper);
Object.defineProperty(htmlMediaHelper, enableHlsJsPlayerKey[0], { get: () => this.enableHlsJsPlayer });
const ExtrasMenu = /** @type {import("./ExtrasMenu.js").default} */ (Jadefin.getMod("ExtrasMenu"));
ExtrasMenu.items.push({
name: "Enable HLS.js",
secondaryText: "Sometimes helps with playback in some browsers",
icon: "auto_fix_normal",
in: ExtrasMenu.IN_CUSTOM,
inCustom: (current, item) => {
if (!this.isEnabled) {
item.name = "Enable HLS.js";
item.icon = "auto_fix_normal";
} else {
item.name = "Disable HLS.js";
item.icon = "auto_fix_off";
}
return (current & (ExtrasMenu.IN_LIBRARY | ExtrasMenu.IN_MOVIE)) == current;
},
cb: () => {
this.isEnabled = !this.isEnabled;
}
});
}
enableHlsJsPlayer(runTimeTicks, mediaType) {
if (this.isEnabled) {
return true;
}
// https://github.com/jellyfin/jellyfin-web/commit/baf1b55a0cb83d4cb63a18968fc93493ce785498
if (JadefinModules.browser.android && mediaType === "Video") {
mediaType = "Audio";
}
return this._enableHlsJsPlayer?.(runTimeTicks, mediaType);
}
})());