jadefin/mods/jade/PatchFirefoxAnamorphic.js

91 lines
2.8 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";
// Thanks, Mozilla, for killing Firefox... https://bugzilla.mozilla.org/show_bug.cgi?id=1331110
export default JadefinIntegrity("PatchFirefoxAnamorphic", import.meta.url, () => new (class PatchFirefoxAnamorphic extends JadefinMod {
constructor() {
super();
}
async init(name, url) {
await super.init(name, url);
document.addEventListener("viewshow", () => {
if (JadefinUtils.routePathIsVideo) {
if (this._tryUpdateRepeat) {
clearInterval(this._tryUpdateRepeat);
}
clearInterval(this._tryUpdateRepeat);
this._tryUpdateRepeat = setInterval(() => this.tryUpdate(), 100);
}
this.update();
});
}
tryUpdate() {
const video = JadefinUtils.video;
if (!video) {
return;
}
this.update();
if (this._tryUpdateRepeat) {
clearInterval(this._tryUpdateRepeat);
}
}
update() {
const video = JadefinUtils.video;
if (!video) {
this.video = null;
return this._dispatch(0);
}
if (this.video == video) {
return this._dispatch(0);
}
this.video = video;
const ratioStr = JadefinUtils.currentPlayer.streamInfo.mediaSource.MediaStreams.find(s => s.Type == "Video").AspectRatio;
const ratioStrSplit = ratioStr && ratioStr.split(":");
if (!ratioStrSplit || !ratioStrSplit[1]) {
this.log.i(`Aspect ratio couldn't be determined`);
return this._dispatch(0);
}
const wantedRatio = parseFloat(ratioStrSplit[0]) / parseFloat(ratioStrSplit[1]);
const wantedWidth = this.video.videoHeight * wantedRatio;
if (Math.abs(this.video.videoWidth - wantedWidth) < 32) {
this.log.i(`Aspect ratio is already correct (video width: ${this.video.videoWidth}; wanted width: ${wantedWidth})`);
return this._dispatch(0);
}
this.log.i(`Aspect ratio is wrong, correcting (video width: ${this.video.videoWidth}; wanted width: ${wantedWidth})`);
this.video.style.setProperty("transform", `scaleX(${wantedWidth / this.video.videoWidth})`);
this.video.style.setProperty("z-index", "-1");
this._dispatch(wantedWidth);
}
/**
* @param {number} width
*/
_dispatch(width) {
JadefinUtils.events.dispatchEvent(new CustomEvent("PatchFirefoxAnamorphic", {
detail: {
width
}
}));
}
})());