MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 12: | Line 12: | ||
} | } | ||
/* Watch for TMH removing | /* Watch for TMH removing controls and adding disabled */ | ||
var observer = new MutationObserver(function (mutations) { | var observer = new MutationObserver(function (mutations) { | ||
mutations.forEach(function (mutation) { | mutations.forEach(function (mutation) { | ||
if (mutation.attributeName === 'controls') { | if (mutation.attributeName === 'controls') { | ||
if (video.getAttribute('controls') === null) { | |||
video.setAttribute('controls', ''); | video.setAttribute('controls', ''); | ||
} | |||
} | |||
if (mutation.attributeName === 'disabled') { | |||
if (video.hasAttribute('disabled')) { | |||
video.removeAttribute('disabled'); | |||
} | } | ||
} | } | ||
| Line 26: | Line 30: | ||
observer.observe(video, { | observer.observe(video, { | ||
attributes: true, | attributes: true, | ||
attributeFilter: ['controls'] | attributeFilter: ['controls', 'disabled'] | ||
}); | }); | ||
/* | /* Set initial state */ | ||
video.setAttribute('controls', ''); | video.setAttribute('controls', ''); | ||
video.removeAttribute('disabled'); | |||
} | } | ||
| Line 40: | Line 45: | ||
============================================================ */ | ============================================================ */ | ||
$(function () { | $(function () { | ||
if ($('.training-video-wrap').length === 0) return; | if ($('.training-video-wrap').length === 0) return; | ||
var $video = $('.training-video-wrap video'); | var $video = $('.training-video-wrap video'); | ||
var $entries = $('.ts-entry'); | var $entries = $('.ts-entry'); | ||
| Line 51: | Line 54: | ||
var $timeDisp = $('.ts-time-display'); | var $timeDisp = $('.ts-time-display'); | ||
if ($video.length === 0) return; | if ($video.length === 0) return; | ||
var videoEl = $video[0]; | var videoEl = $video[0]; | ||
function fmt(s) { | function fmt(s) { | ||
s = Math.floor(s); | s = Math.floor(s); | ||
| Line 61: | Line 63: | ||
} | } | ||
var timestamps = []; | var timestamps = []; | ||
$entries.each(function () { | $entries.each(function () { | ||
| Line 67: | Line 68: | ||
}); | }); | ||
function highlightActive() { | function highlightActive() { | ||
var cur = videoEl.currentTime; | var cur = videoEl.currentTime; | ||
| Line 77: | Line 77: | ||
$(this).toggleClass('ts-active', i === activeIdx); | $(this).toggleClass('ts-active', i === activeIdx); | ||
}); | }); | ||
var $active = $entries.eq(activeIdx); | var $active = $entries.eq(activeIdx); | ||
if ($active.length && $body.length) { | if ($active.length && $body.length) { | ||
| Line 89: | Line 88: | ||
} | } | ||
function updateProgress() { | function updateProgress() { | ||
if (!videoEl.duration) return; | if (!videoEl.duration) return; | ||
| Line 97: | Line 95: | ||
} | } | ||
$video.on('timeupdate', function () { | $video.on('timeupdate', function () { | ||
highlightActive(); | highlightActive(); | ||
| Line 105: | Line 102: | ||
$video.on('loadedmetadata', function () { | $video.on('loadedmetadata', function () { | ||
$timeDisp.text('0:00 / ' + fmt(videoEl.duration)); | $timeDisp.text('0:00 / ' + fmt(videoEl.duration)); | ||
$markers.each(function () { | $markers.each(function () { | ||
var t = parseInt($(this).data('time'), 10); | var t = parseInt($(this).data('time'), 10); | ||
| Line 112: | Line 108: | ||
}); | }); | ||
$entries.on('click', function () { | $entries.on('click', function () { | ||
var t = parseInt($(this).data('time'), 10); | var t = parseInt($(this).data('time'), 10); | ||
| Line 120: | Line 115: | ||
}); | }); | ||
$('.ts-progress-track').on('click', function (e) { | $('.ts-progress-track').on('click', function (e) { | ||
var rect = this.getBoundingClientRect(); | var rect = this.getBoundingClientRect(); | ||
| Line 127: | Line 121: | ||
}); | }); | ||
$markers.on('click', function (e) { | $markers.on('click', function (e) { | ||
e.stopPropagation(); | e.stopPropagation(); | ||
Revision as of 08:24, 22 April 2026
/* Any JavaScript here will be loaded for all users on every page load. */
/* ── Prevent TMH from removing native video controls ── */
$(function () {
if ($('.training-video-wrap').length === 0) return;
function startGuarding() {
var video = document.querySelector('.mw-file-element');
if (!video) {
setTimeout(startGuarding, 100);
return;
}
/* Watch for TMH removing controls and adding disabled */
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.attributeName === 'controls') {
if (video.getAttribute('controls') === null) {
video.setAttribute('controls', '');
}
}
if (mutation.attributeName === 'disabled') {
if (video.hasAttribute('disabled')) {
video.removeAttribute('disabled');
}
}
});
});
observer.observe(video, {
attributes: true,
attributeFilter: ['controls', 'disabled']
});
/* Set initial state */
video.setAttribute('controls', '');
video.removeAttribute('disabled');
}
startGuarding();
});
/* ============================================================
Training Video – Synced Transcript Sidebar
============================================================ */
$(function () {
if ($('.training-video-wrap').length === 0) return;
var $video = $('.training-video-wrap video');
var $entries = $('.ts-entry');
var $body = $('.ts-scroll-body');
var $markers = $('.ts-progress-marker');
var $fill = $('.ts-progress-fill');
var $timeDisp = $('.ts-time-display');
if ($video.length === 0) return;
var videoEl = $video[0];
function fmt(s) {
s = Math.floor(s);
return Math.floor(s / 60) + ':' + ('0' + (s % 60)).slice(-2);
}
var timestamps = [];
$entries.each(function () {
timestamps.push(parseInt($(this).data('time'), 10));
});
function highlightActive() {
var cur = videoEl.currentTime;
var activeIdx = 0;
for (var i = 0; i < timestamps.length; i++) {
if (cur >= timestamps[i]) activeIdx = i;
}
$entries.each(function (i) {
$(this).toggleClass('ts-active', i === activeIdx);
});
var $active = $entries.eq(activeIdx);
if ($active.length && $body.length) {
var entryTop = $active.position().top;
var bodyScrollTop = $body.scrollTop();
var bodyHeight = $body.height();
if (entryTop < 0 || entryTop > bodyHeight - 60) {
$body.animate({ scrollTop: bodyScrollTop + entryTop - 60 }, 200);
}
}
}
function updateProgress() {
if (!videoEl.duration) return;
var pct = (videoEl.currentTime / videoEl.duration) * 100;
$fill.css('width', pct + '%');
$timeDisp.text(fmt(videoEl.currentTime) + ' / ' + fmt(videoEl.duration));
}
$video.on('timeupdate', function () {
highlightActive();
updateProgress();
});
$video.on('loadedmetadata', function () {
$timeDisp.text('0:00 / ' + fmt(videoEl.duration));
$markers.each(function () {
var t = parseInt($(this).data('time'), 10);
$(this).css('left', ((t / videoEl.duration) * 100) + '%');
});
});
$entries.on('click', function () {
var t = parseInt($(this).data('time'), 10);
videoEl.currentTime = t;
if (videoEl.paused) videoEl.play();
highlightActive();
});
$('.ts-progress-track').on('click', function (e) {
var rect = this.getBoundingClientRect();
var ratio = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
videoEl.currentTime = ratio * videoEl.duration;
});
$markers.on('click', function (e) {
e.stopPropagation();
var t = parseInt($(this).data('time'), 10);
videoEl.currentTime = t;
if (videoEl.paused) videoEl.play();
});
});
/* ============================================================ */