mirror of
https://github.com/NohamR/safari-internal-js.git
synced 2026-11-25 23:08:08 +00:00
Add Safari 27.2 JavaScript dump
This commit is contained in:
1 parent
897293bc5f
commit
f752a0a82c
40 files changed
+24914
-1
No files matched your search
@@ -0,0 +1,109 @@
|
||||
//# sourceURL=__InjectedScript_CNNCaptionQuirk.js
|
||||
(function () {
|
||||
"use strict";
|
||||
|
||||
class CaptionMirror {
|
||||
static _instances = new WeakMap();
|
||||
constructor(video) {
|
||||
let oldMirror = CaptionMirror._instances.get(video);
|
||||
if (oldMirror) {
|
||||
oldMirror.invalidate();
|
||||
CaptionMirror._instances.delete(video);
|
||||
}
|
||||
CaptionMirror._instances.set(video, this);
|
||||
this._video = video;
|
||||
this._playerSelector = '[data-component-name="video-player"]';
|
||||
this._overlayRootSelector = '[data-testid="overlay-root"]';
|
||||
this._captionRowSelector = '[data-testid="cueBoxRowTextCue"]';
|
||||
this._player = this._findPlayerRoot(video);
|
||||
this._mirrorTrack = video.addTextTrack("forced", "CNN Captions");
|
||||
this._mirrorTrack.mode = "hidden";
|
||||
this._captionObserver = null;
|
||||
this._waitObserver = null;
|
||||
this._onPresentationModeChanged = () =>
|
||||
this._handlePresentationModeChanged();
|
||||
this._video.addEventListener(
|
||||
"webkitpresentationmodechanged",
|
||||
this._onPresentationModeChanged,
|
||||
);
|
||||
if (this._player) this._attachOverlayObserver();
|
||||
}
|
||||
invalidate() {
|
||||
if (this._captionObserver) {
|
||||
this._captionObserver.disconnect();
|
||||
this._captionObserver = null;
|
||||
}
|
||||
if (this._waitObserver) {
|
||||
this._waitObserver.disconnect();
|
||||
this._waitObserver = null;
|
||||
}
|
||||
this._mirrorTrack.mode = "disabled";
|
||||
this._video.removeEventListener(
|
||||
"webkitpresentationmodechanged",
|
||||
this._onPresentationModeChanged,
|
||||
);
|
||||
}
|
||||
_findPlayerRoot(video) {
|
||||
return video.closest(this._playerSelector);
|
||||
}
|
||||
_syncCues(overlayRoot) {
|
||||
while (this._mirrorTrack.cues?.length)
|
||||
this._mirrorTrack.removeCue(this._mirrorTrack.cues[0]);
|
||||
var rows = Array.from(
|
||||
overlayRoot.querySelectorAll(this._captionRowSelector),
|
||||
);
|
||||
if (!rows.length) return;
|
||||
var text = rows.map((row) => row.textContent).join("\n");
|
||||
if (!text) return;
|
||||
var cue = new VTTCue(0, 10e7, text);
|
||||
cue.snapToLines = true;
|
||||
cue.line = -4;
|
||||
cue.position = "auto";
|
||||
cue.positionAlign = "center";
|
||||
this._mirrorTrack.addCue(cue);
|
||||
}
|
||||
|
||||
_attachOverlayObserver() {
|
||||
var overlayRoot = this._player.querySelector(this._overlayRootSelector);
|
||||
if (overlayRoot) {
|
||||
this._attachCaptionObserver(overlayRoot);
|
||||
return;
|
||||
}
|
||||
this._waitObserver = new MutationObserver(() => {
|
||||
var overlayRoot = this._player.querySelector(this._overlayRootSelector);
|
||||
if (overlayRoot) {
|
||||
this._attachCaptionObserver(overlayRoot);
|
||||
this._waitObserver.disconnect();
|
||||
this._waitObserver = null;
|
||||
}
|
||||
});
|
||||
this._waitObserver.observe(this._player, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
}
|
||||
_attachCaptionObserver(overlayRoot) {
|
||||
if (this._captionObserver) this._captionObserver.disconnect();
|
||||
this._captionObserver = new MutationObserver(() => {
|
||||
var current = this._player.querySelector(this._overlayRootSelector);
|
||||
if (current) this._syncCues(current);
|
||||
});
|
||||
this._captionObserver.observe(overlayRoot, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
characterData: true,
|
||||
});
|
||||
this._syncCues(overlayRoot);
|
||||
}
|
||||
_handlePresentationModeChanged() {
|
||||
if (this._video.webkitPresentationMode == "inline") {
|
||||
this._mirrorTrack.mode = "hidden";
|
||||
return;
|
||||
}
|
||||
this._mirrorTrack.mode = "showing";
|
||||
}
|
||||
}
|
||||
window.setupCaptionMirroring = function (video) {
|
||||
new CaptionMirror(video);
|
||||
};
|
||||
})();
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,236 @@
|
||||
//# sourceURL=__InjectedScript_YouTubeCaptionQuirk.js
|
||||
(function () {
|
||||
"use strict";
|
||||
class CaptionMirror {
|
||||
static _instances = new WeakMap();
|
||||
constructor(video) {
|
||||
let oldMirror = CaptionMirror._instances.get(video);
|
||||
if (oldMirror) {
|
||||
oldMirror.invalidate();
|
||||
CaptionMirror._instances.delete(video);
|
||||
}
|
||||
CaptionMirror._instances.set(video, this);
|
||||
this._video = video;
|
||||
this._player = video.parentElement.parentElement;
|
||||
this._captionWindowSelector = ".ytp-caption-window-container";
|
||||
this._mirrorTrack = video.addTextTrack("forced", "YouTube Captions");
|
||||
this._mirrorTrack.mode = "hidden";
|
||||
this._captionObserver = null;
|
||||
this._waitObserver = null;
|
||||
this._observedCaptionContainer = null;
|
||||
this._onCaptionsTrackListChanged = () =>
|
||||
this._handleCaptionStateChanged();
|
||||
this._onCaptionsChanged = () => this._handleCaptionStateChanged();
|
||||
this._player.addEventListener(
|
||||
"onCaptionsTrackListChanged",
|
||||
this._onCaptionsTrackListChanged,
|
||||
);
|
||||
this._player.addEventListener("captionschanged", this._onCaptionsChanged);
|
||||
navigator.mediaSession.setActionHandler("togglecaptions", (details) =>
|
||||
this._handleMediaSessionAction(details),
|
||||
);
|
||||
navigator.mediaSession.setActionHandler("selectcaptiontrack", (details) =>
|
||||
this._handleMediaSessionAction(details),
|
||||
);
|
||||
this._onPresentationModeChanged = () =>
|
||||
this._handlePresentationModeChanged();
|
||||
this._video.addEventListener(
|
||||
"webkitpresentationmodechanged",
|
||||
this._onPresentationModeChanged,
|
||||
);
|
||||
this._onEmptied = () => this._handleResourceChanged();
|
||||
this._video.addEventListener("emptied", this._onEmptied);
|
||||
this._attachCaptionWindowObserver();
|
||||
this._syncTrackList();
|
||||
}
|
||||
invalidate() {
|
||||
if (this._captionObserver) {
|
||||
this._captionObserver.disconnect();
|
||||
this._captionObserver = null;
|
||||
}
|
||||
if (this._waitObserver) {
|
||||
this._waitObserver.disconnect();
|
||||
this._waitObserver = null;
|
||||
}
|
||||
this._observedCaptionContainer = null;
|
||||
this._mirrorTrack.mode = "disabled";
|
||||
this._player.removeEventListener(
|
||||
"onCaptionsTrackListChanged",
|
||||
this._onCaptionsTrackListChanged,
|
||||
);
|
||||
this._player.removeEventListener(
|
||||
"captionschanged",
|
||||
this._onCaptionsChanged,
|
||||
);
|
||||
this._video.removeEventListener(
|
||||
"webkitpresentationmodechanged",
|
||||
this._onPresentationModeChanged,
|
||||
);
|
||||
this._video.removeEventListener("emptied", this._onEmptied);
|
||||
}
|
||||
_handleCaptionStateChanged() {
|
||||
this._syncTrackList();
|
||||
this._updateCues();
|
||||
}
|
||||
_handleResourceChanged() {
|
||||
this._removeAllCues();
|
||||
this._detachCaptionObserver();
|
||||
this._attachCaptionWindowObserver();
|
||||
}
|
||||
_syncTrackList() {
|
||||
var trackList = this._getTrackList();
|
||||
var activeTrack = this._getActiveTrack();
|
||||
navigator.mediaSession.captionTracks =
|
||||
trackList?.map((track) => {
|
||||
return {
|
||||
label: track.displayName,
|
||||
language: track.languageCode,
|
||||
enabled: this._areTracksEqual(track, activeTrack),
|
||||
};
|
||||
}) ?? [];
|
||||
navigator.mediaSession.captionsEnabled = this._player.isSubtitlesOn();
|
||||
}
|
||||
_updateCues() {
|
||||
var container = this._player.querySelector(this._captionWindowSelector);
|
||||
if (!container) {
|
||||
this._removeAllCues();
|
||||
this._detachCaptionObserver();
|
||||
this._attachCaptionWindowObserver();
|
||||
return;
|
||||
}
|
||||
if (container !== this._observedCaptionContainer)
|
||||
this._attachCaptionObserver(container);
|
||||
this._syncCues(container);
|
||||
}
|
||||
_removeAllCues() {
|
||||
while (this._mirrorTrack.cues?.length)
|
||||
this._mirrorTrack.removeCue(this._mirrorTrack.cues[0]);
|
||||
}
|
||||
_syncCues(captionWindowContainer) {
|
||||
this._removeAllCues();
|
||||
for (var captionWindow of captionWindowContainer.querySelectorAll(
|
||||
".caption-window",
|
||||
)) {
|
||||
var text = Array.from(
|
||||
captionWindow.querySelectorAll(".captions-text > span"),
|
||||
)
|
||||
.map((span) => span.textContent)
|
||||
.join("\n");
|
||||
if (!text) continue;
|
||||
var cue = new VTTCue(0, 10e7, text);
|
||||
switch (captionWindow.style.textAlign) {
|
||||
case "left":
|
||||
case "start":
|
||||
cue.align = "start";
|
||||
cue.positionAlign = "line-left";
|
||||
break;
|
||||
case "end":
|
||||
case "right":
|
||||
cue.align = "end";
|
||||
cue.positionAlign = "line-right";
|
||||
break;
|
||||
default:
|
||||
cue.align = "center";
|
||||
cue.positionAlign = "center";
|
||||
}
|
||||
var topPercent = parseFloat(captionWindow.style.top);
|
||||
var bottomPercent = parseFloat(captionWindow.style.bottom);
|
||||
var leftPercent = parseFloat(captionWindow.style.left);
|
||||
var rightPercent = parseFloat(captionWindow.style.right);
|
||||
if (!isNaN(topPercent)) {
|
||||
cue.snapToLines = false;
|
||||
cue.line = topPercent;
|
||||
} else if (!isNaN(bottomPercent)) {
|
||||
cue.snapToLines = false;
|
||||
cue.line = 100 - bottomPercent;
|
||||
}
|
||||
if (!isNaN(leftPercent)) cue.position = leftPercent;
|
||||
else if (!isNaN(rightPercent)) cue.position = 100 - rightPercent;
|
||||
this._mirrorTrack.addCue(cue);
|
||||
}
|
||||
}
|
||||
_attachCaptionWindowObserver() {
|
||||
var container = this._player.querySelector(this._captionWindowSelector);
|
||||
if (container) {
|
||||
this._attachCaptionObserver(container);
|
||||
return;
|
||||
}
|
||||
if (this._waitObserver) return;
|
||||
this._waitObserver = new MutationObserver(() => {
|
||||
var container = this._player.querySelector(this._captionWindowSelector);
|
||||
if (container) {
|
||||
this._attachCaptionObserver(container);
|
||||
this._waitObserver.disconnect();
|
||||
this._waitObserver = null;
|
||||
this._syncCues(container);
|
||||
}
|
||||
});
|
||||
this._waitObserver.observe(this._player, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
});
|
||||
}
|
||||
_detachCaptionObserver() {
|
||||
if (this._captionObserver) {
|
||||
this._captionObserver.disconnect();
|
||||
this._captionObserver = null;
|
||||
}
|
||||
this._observedCaptionContainer = null;
|
||||
}
|
||||
_attachCaptionObserver(container) {
|
||||
this._detachCaptionObserver();
|
||||
this._observedCaptionContainer = container;
|
||||
this._captionObserver = new MutationObserver(() => this._updateCues());
|
||||
this._captionObserver.observe(container, {
|
||||
childList: true,
|
||||
subtree: true,
|
||||
characterData: true,
|
||||
});
|
||||
}
|
||||
_getIsInline() {
|
||||
if (typeof this._video.webkitPresentationMode === "undefined")
|
||||
return !this._video.webkitDisplayingFullscreen;
|
||||
return this._video.webkitPresentationMode == "inline";
|
||||
}
|
||||
_handlePresentationModeChanged() {
|
||||
var isInline = this._getIsInline();
|
||||
if (!isInline) this._player.loadModule("captions");
|
||||
this._updateCues();
|
||||
this._mirrorTrack.mode = isInline ? "hidden" : "showing";
|
||||
var captionContainer = this._player.querySelector(
|
||||
this._captionWindowSelector,
|
||||
);
|
||||
if (!captionContainer) return;
|
||||
if (isInline) captionContainer.style.removeProperty("visibility");
|
||||
else
|
||||
captionContainer.style.setProperty("visibility", "hidden", "important");
|
||||
}
|
||||
_handleMediaSessionAction(details) {
|
||||
switch (details.action) {
|
||||
case "togglecaptions":
|
||||
this._player.toggleSubtitles();
|
||||
break;
|
||||
case "selectcaptiontrack": {
|
||||
var list = this._getTrackList();
|
||||
var index = details.trackIndex;
|
||||
if (index >= 0 && index < list.length)
|
||||
this._player.setOption("captions", "track", list[index]);
|
||||
else this._player.setOption("captions", "track", {});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
_getTrackList() {
|
||||
return this._player.getOption("captions", "tracklist") || [];
|
||||
}
|
||||
_getActiveTrack() {
|
||||
return this._player.getOption("captions", "track");
|
||||
}
|
||||
_areTracksEqual(a, b) {
|
||||
return a && b && a.vss_id === b.vss_id;
|
||||
}
|
||||
}
|
||||
window.setupCaptionMirroring = function (video) {
|
||||
new CaptionMirror(video);
|
||||
};
|
||||
})();
|
||||
Reference in new issue
Block a user