User:Most2dot0/common.js
From Angelina Jordan Wiki
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
//syntax highlighter
mw.loader.load('//www.mediawiki.org/w/index.php?title=MediaWiki:Gadget-DotsSyntaxHighlighter.js&action=raw&ctype=text/javascript');
syntaxHighlighterConfig = { parameterColor: "#FFDD99" }
// embedded YouTube player implementation
// Use like this, where + designates an optional start, and "-" an optional stop time:
// <div class="youtube-player-placeholder" data-videos="VIDEO_ID_1|+45-135,VIDEO_ID_2,VIDEO_ID_3|+10-60"></div>
function insertYouTubePlayers() {
var placeholders = document.querySelectorAll('.youtube-player-placeholder');
// Load the YouTube IFrame API if not already loaded
if (!document.getElementById('youtube-iframe-api')) {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
tag.id = 'youtube-iframe-api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
var videoLists = [];
var currentVideoIndices = [];
var checkIntervals = [];
function parseVideosFromPlaceholder(placeholder) {
var videoDataList = [];
// Check for YouTube URLs in the placeholder's text content
var textContent = placeholder.dataset.originalText;
var youtubeUrlPattern = /https?:\/\/(www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)([^\s]*)/g;
var match;
while ((match = youtubeUrlPattern.exec(textContent)) !== null) {
var videoId = match[2];
var params = new URLSearchParams(match[3]);
var start = params.get('t') ? parseInt(params.get('t')) : 0;
var end = params.get('end') ? parseInt(params.get('end')) : null;
videoDataList.push({ videoId: videoId, start: start, end: end });
}
// If no URLs found, fall back to data-videos attribute
if (videoDataList.length === 0) {
videoDataList = placeholder.getAttribute('data-videos').split(',').map(function(videoData) {
var parts = videoData.split('|');
var videoId = parts[0];
var start = 0;
var end = null;
if (parts.length > 1) {
var timeParts = parts[1].split('+');
if (timeParts.length > 1) {
start = parseInt(timeParts[0]) || 0;
end = timeParts[1] ? parseInt(timeParts[1]) : null;
} else if (parts[1].includes('-')) {
start = parseInt(parts[1].split('-')[0]) || 0;
end = parseInt(parts[1].split('-')[1]) || null;
}
}
return { videoId: videoId, start: start, end: end };
});
}
return videoDataList;
}
function onYouTubeIframeAPIReady() {
var observer = new IntersectionObserver(function(entries) {
entries.forEach(function(entry) {
if (entry.isIntersecting) {
var index = entry.target.getAttribute('data-index');
if (!entry.target.getAttribute('data-loaded')) {
entry.target.setAttribute('data-loaded', 'true');
loadYouTubePlayer(entry.target, index);
}
}
});
});
placeholders.forEach(function(placeholder, index) {
placeholder.setAttribute('data-index', index);
// Save the original text content if not already saved
if (!placeholder.dataset.originalText) {
placeholder.dataset.originalText = placeholder.innerHTML.trim();
}
observer.observe(placeholder);
});
}
window.onYouTubeIframeAPIReady = onYouTubeIframeAPIReady;
function loadYouTubePlayer(placeholder, index) {
var videoDataList = parseVideosFromPlaceholder(placeholder);
videoLists[index] = videoDataList;
currentVideoIndices[index] = 0;
checkIntervals[index] = null;
// Preserve the placeholder's original text content
var originalHTML = document.createElement('div');
originalHTML.className = 'youtube-placeholder-text';
originalHTML.innerHTML = placeholder.dataset.originalText;
placeholder.innerHTML = ''; // Clear the original content of the placeholder
placeholder.appendChild(originalHTML);
var playerDiv = document.createElement('div');
playerDiv.id = 'youtube-player-' + index;
placeholder.appendChild(playerDiv);
var controlsDiv = document.createElement('div');
controlsDiv.className = 'youtube-player-controls';
controlsDiv.innerHTML = '<button class="prev-button" data-index="' + index + '">Previous</button>' +
'<button class="next-button" data-index="' + index + '">Next</button>';
placeholder.appendChild(controlsDiv);
window['youtube-player-' + index] = new YT.Player('youtube-player-' + index, {
height: '270',
width: '480',
videoId: videoLists[index][currentVideoIndices[index]].videoId,
playerVars: {
start: videoLists[index][currentVideoIndices[index]].start
},
events: {
'onStateChange': onPlayerStateChange(index),
'onReady': onPlayerReady(index)
}
});
// Add click event listeners to the YouTube links in the placeholder's text
var links = originalHTML.querySelectorAll('a');
links.forEach(function(link) {
var href = link.getAttribute('href');
var match = href.match(/https?:\/\/(www\.)?youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)([^\s]*)/);
if (match) {
link.addEventListener('click', function(event) {
event.preventDefault();
var videoId = match[2];
var params = new URLSearchParams(match[3]);
var start = params.get('t') ? parseInt(params.get('t')) : 0;
var end = params.get('end') ? parseInt(params.get('end')) : null;
var videoIndex = videoLists[index].findIndex(function(video) {
return video.videoId === videoId &&
video.start === start &&
(video.end === end || video.end === null);
});
if (videoIndex !== -1) {
currentVideoIndices[index] = videoIndex;
window['youtube-player-' + index].loadVideoById({
videoId: videoLists[index][currentVideoIndices[index]].videoId,
startSeconds: videoLists[index][currentVideoIndices[index]].start
});
}
});
}
});
}
function onPlayerReady(index) {
return function(event) {
var placeholder = placeholders[index];
var originalHTML = placeholder.querySelector('.youtube-placeholder-text');
originalHTML.style.display = ''; // Ensure the original text is displayed correctly
};
}
function onPlayerStateChange(index) {
return function(event) {
clearInterval(checkIntervals[index]);
if (event.data == YT.PlayerState.PLAYING && videoLists[index][currentVideoIndices[index]].end) {
checkIntervals[index] = setInterval(function() {
var currentTime = window['youtube-player-' + index].getCurrentTime();
if (currentTime >= videoLists[index][currentVideoIndices[index]].end) {
playNextVideo(index);
}
}, 1000); // Check every second
} else if (event.data == YT.PlayerState.ENDED) {
playNextVideo(index);
}
};
}
function playNextVideo(index) {
clearInterval(checkIntervals[index]);
currentVideoIndices[index]++;
if (currentVideoIndices[index] < videoLists[index].length) {
window['youtube-player-' + index].loadVideoById({
videoId: videoLists[index][currentVideoIndices[index]].videoId,
startSeconds: videoLists[index][currentVideoIndices[index]].start
});
} else {
currentVideoIndices[index] = 0;
window['youtube-player-' + index].loadVideoById({
videoId: videoLists[index][currentVideoIndices[index]].videoId,
startSeconds: videoLists[index][currentVideoIndices[index]].start
});
}
}
function playPreviousVideo(index) {
clearInterval(checkIntervals[index]);
currentVideoIndices[index]--;
if (currentVideoIndices[index] >= 0) {
window['youtube-player-' + index].loadVideoById({
videoId: videoLists[index][currentVideoIndices[index]].videoId,
startSeconds: videoLists[index][currentVideoIndices[index]].start
});
} else {
currentVideoIndices[index] = videoLists[index].length - 1;
window['youtube-player-' + index].loadVideoById({
videoId: videoLists[index][currentVideoIndices[index]].videoId,
startSeconds: videoLists[index][currentVideoIndices[index]].start
});
}
}
document.addEventListener('click', function(event) {
if (event.target.classList.contains('next-button')) {
var index = event.target.getAttribute('data-index');
playNextVideo(index);
} else if (event.target.classList.contains('prev-button')) {
var index = event.target.getAttribute('data-index');
playPreviousVideo(index);
}
});
}
insertYouTubePlayers();