mediaserver-nginx-autoindexer/autoindex.js

344 lines
11 KiB
JavaScript
Raw Normal View History

2021-05-23 19:24:04 +00:00
const allLinks = [...document.getElementsByTagName('a')];
const bodyInner = document.querySelector('.inner');
const tableElem = document.querySelector('table');
const infoRegex = /(-thumb)?\.(jpg|nfo|srt)$/;
2021-05-30 14:52:17 +00:00
let infoFiles = 0;
let infoFilesAccounted = 0;
2021-05-23 19:24:04 +00:00
const arrow = `
<svg version="1.1" id="back-arrow" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
2021-05-23 19:47:14 +00:00
viewBox="0 0 492 492" style="enable-background:new 0 0 492 492;" xml:space="preserve">
<g>
2021-05-24 19:05:42 +00:00
<g>
<path fill="#fff" d="M464.344,207.418l0.768,0.168H135.888l103.496-103.724c5.068-5.064,7.848-11.924,7.848-19.124
c0-7.2-2.78-14.012-7.848-19.088L223.28,49.538c-5.064-5.064-11.812-7.864-19.008-7.864c-7.2,0-13.952,2.78-19.016,7.844
L7.844,226.914C2.76,231.998-0.02,238.77,0,245.974c-0.02,7.244,2.76,14.02,7.844,19.096l177.412,177.412
c5.064,5.06,11.812,7.844,19.016,7.844c7.196,0,13.944-2.788,19.008-7.844l16.104-16.112c5.068-5.056,7.848-11.808,7.848-19.008
c0-7.196-2.78-13.592-7.848-18.652L134.72,284.406h329.992c14.828,0,27.288-12.78,27.288-27.6v-22.788
C492,219.198,479.172,207.418,464.344,207.418z"/>
</g>
2021-05-23 19:47:14 +00:00
</g>
2021-05-23 19:24:04 +00:00
</svg>
2021-10-11 18:39:36 +00:00
`;
2021-05-23 19:24:04 +00:00
2021-05-24 19:05:42 +00:00
function reencodeURI(uri) {
return encodeURIComponent(decodeURIComponent(uri));
}
function getHref(tag) {
return tag.getAttribute('href');
}
2021-05-23 19:24:04 +00:00
function findByHref(href) {
2021-05-24 19:05:42 +00:00
return allLinks.filter((link) => {
return reencodeURI(getHref(link)) === reencodeURI(href)
});
2021-05-23 19:24:04 +00:00
}
2021-10-11 18:39:36 +00:00
function findWholeNumberInText(text, matchNumber) {
const numbers = text.match(/\d+/g);
return numbers
&& numbers.length
&& numbers.map((x) => parseInt(x, 10)).some((number) => number === matchNumber);
}
2021-05-23 19:24:04 +00:00
function findByInfoFile(infoFile) {
2021-10-11 18:39:36 +00:00
const found = ['mkv', 'mp4', 'avi'].reduce((last, current) => {
2021-05-23 19:24:04 +00:00
const element = findByHref(infoFile.replace(infoRegex, '.' + current))[0];
2021-10-11 18:39:36 +00:00
return element || last;
2021-05-23 19:24:04 +00:00
}, null);
return found;
}
function deduplicateJointEpisode(textContent) {
return new Promise((resolve, reject) => {
const lns = textContent.split('\n');
const result = [];
let passed = 0;
for (const line of lns) {
if (line.startsWith('<episodedetails>')) {
if (passed === 1) {
break;
}
passed = 1;
}
result.push(line);
}
resolve(result.join('\n'));
2021-05-23 19:47:14 +00:00
});
2021-05-23 19:24:04 +00:00
}
const parser = new window.DOMParser();
async function createXMLRequest(nfo) {
return fetch(nfo)
.then((response) => response.text())
.then((text) => deduplicateJointEpisode(text))
.then((str) => parser.parseFromString(str, 'text/xml'));
}
2021-10-11 18:39:36 +00:00
// Fill metadata boxes, generate buttons
function fillMeta(
content, original, nfo, movieTitle, movieDescription, movieButtons
) {
infoFilesAccounted += 1;
if (!content) {
return;
}
const titleEl = content.querySelector('title');
const plotEl = content.querySelector('plot');
const airedEl = content.querySelector('aired');
const premieredEl = content.querySelector('premiered');
const imdbEl = content.querySelector('uniqueid[type="imdb"]');
const tvdbEl = content.querySelector('uniqueid[type="tvdb"]');
if (!titleEl || !plotEl) {
return;
}
if (airedEl || premieredEl) {
const timestamp = original.parentElement.parentElement.querySelector('.timestamp');
if (timestamp) {
timestamp.innerText = (airedEl || premieredEl).textContent;
}
}
const episode = content.querySelector('episode');
const season = content.querySelector('season');
let title = titleEl.textContent
if (episode && season) {
title = `(S${season.textContent} E${episode.textContent}) ` + title;
original.parentElement.setAttribute('data-episode', episode.textContent);
}
movieTitle.innerText = title;
movieDescription.innerText = plotEl.textContent;
if (imdbEl) {
const imlink = document.createElement('a');
imlink.target = '_blank';
imlink.innerText = 'IMDb';
imlink.href = `https://www.imdb.com/title/${imdbEl.textContent}`;
movieButtons.appendChild(imlink);
}
if (tvdbEl) {
const tvlink = document.createElement('a');
tvlink.target = '_blank';
tvlink.innerText = 'The TVDB';
tvlink.href = `http://www.thetvdb.com/?tab=series&id=${tvdbEl.textContent}`;
movieButtons.appendChild(tvlink);
}
// Add "copy direct url" button
if (
getHref(nfo) !== 'tvshow.nfo'
&& navigator
&& navigator.clipboard
&& navigator.clipboard.writeText
) {
const copybutton = document.createElement('button');
copybutton.innerText = 'Copy direct URL';
movieButtons.appendChild(copybutton);
copybutton.addEventListener('click', (e) => {
e.preventDefault();
const popup = document.createElement('div');
popup.className = 'tooltip';
popup.innerText = 'Copied!';
const url = window.location.href + reencodeURI(getHref(original));
navigator.clipboard.writeText(url).then(() => {
copybutton.appendChild(popup);
setTimeout(() => copybutton.removeChild(popup), 1000);
});
});
}
}
// Replace list entry with a metadata box
// Either create or populate
2021-05-23 19:24:04 +00:00
function createOrImproveMovieMeta(original, thumbnail, nfo) {
2021-05-24 19:05:42 +00:00
let imgTag = `<img class="thumbnail" src="${thumbnail ? reencodeURI(getHref(thumbnail)) : ''}"/>`;
2021-05-23 19:24:04 +00:00
let movieTitle = `<div class="movie-title">${original.innerText}</div>`;
let movieDescription = `<div class="movie-description"></div>`;
2021-05-24 19:05:42 +00:00
let movieButtons = `<div class="movie-buttons"></div>`;
2021-05-23 19:24:04 +00:00
if (!thumbnail && !nfo) {
return;
}
if (original.parentElement.classList.contains('enhanced')) {
imgTag = original.querySelector('.thumbnail');
movieTitle = original.querySelector('.movie-title');
movieDescription = original.querySelector('.movie-description');
2021-05-24 19:05:42 +00:00
movieButtons = original.querySelector('.movie-buttons');
2021-05-23 19:24:04 +00:00
if (thumbnail) {
2021-05-24 19:05:42 +00:00
imgTag.src = reencodeURI(getHref(thumbnail));
2021-05-23 19:24:04 +00:00
}
} else {
original.classList.add('movie');
2021-05-24 19:05:42 +00:00
original.innerHTML = imgTag + '<div class="meta-wrap">' + movieTitle + movieDescription + movieButtons + '</div>';
2021-05-23 19:24:04 +00:00
original.parentElement.classList.add('enhanced');
2021-05-23 19:47:14 +00:00
// quick innerHTML hack to create elements here lol
2021-05-23 19:24:04 +00:00
imgTag = original.querySelector('.thumbnail');
movieTitle = original.querySelector('.movie-title');
movieDescription = original.querySelector('.movie-description');
2021-05-24 19:05:42 +00:00
movieButtons = original.querySelector('.movie-buttons');
2021-05-23 19:24:04 +00:00
}
2021-10-11 18:39:36 +00:00
// Fetch metadata from the nfo file and populate the info box
2021-05-23 19:24:04 +00:00
if (nfo) {
2021-05-24 19:05:42 +00:00
createXMLRequest(reencodeURI(getHref(nfo)))
2021-10-11 18:39:36 +00:00
.then((content) => fillMeta(content, original, nfo, movieTitle, movieDescription, movieButtons))
.catch((e) => {
2021-05-30 14:52:17 +00:00
infoFilesAccounted += 1;
2021-10-11 18:39:36 +00:00
console.error(e);
});
2021-05-23 19:24:04 +00:00
}
}
2021-10-11 18:39:36 +00:00
// Promise that resolves when all info files that were found have been parsed
2021-05-30 14:52:17 +00:00
function waitUntilInfoComplete() {
return new Promise((resolve, reject) => {
if (infoFilesAccounted >= infoFiles) {
resolve(true);
return;
}
let seconds = 0;
const accountWait = setInterval(() => {
2021-10-11 18:39:36 +00:00
if (infoFilesAccounted >= infoFiles || seconds > 20) {
2021-05-30 14:52:17 +00:00
clearInterval(accountWait);
resolve(true);
return;
}
2021-10-11 18:39:36 +00:00
seconds += 1;
2021-05-30 14:52:17 +00:00
}, 500);
});
}
2021-10-11 18:39:36 +00:00
// Get episode name or name of file
2021-05-30 14:52:17 +00:00
const getCellValue = (tr, idx) =>
tr.children[idx].getAttribute('data-episode')
|| tr.children[idx].innerText
|| tr.children[idx].textContent;
2021-10-11 18:39:36 +00:00
// Compare numeric or (locale-aware, numeric-aware) by string values
2021-05-30 14:52:17 +00:00
const comparer = (idx, asc) => (a, b) => ((v1, v2) =>
2021-10-11 18:39:36 +00:00
v1 !== '' && v2 !== '' && !isNaN(v1) && !isNaN(v2) ? v1 - v2 : v1.toString().localeCompare(v2, 'et', { numeric: true })
2021-05-30 14:52:17 +00:00
)(getCellValue(asc ? a : b, idx), getCellValue(asc ? b : a, idx));
2021-10-11 18:39:36 +00:00
// Table sort by column index
2021-05-30 14:52:17 +00:00
const sortTableValues = (index) =>
Array.from(tableElem.querySelectorAll('tr:nth-child(n+2):not(.btn-back)'))
.sort(comparer(index, this.asc = !this.asc))
.forEach((tr) => tableElem.appendChild(tr));
2021-05-23 19:24:04 +00:00
let tvShow;
function accountForMetadata() {
allLinks.forEach((link) => {
2021-05-24 19:05:42 +00:00
const url = getHref(link);
2021-10-11 18:39:36 +00:00
// If nfo or jpg (or subtitle, for hiding it)..
2021-05-23 19:24:04 +00:00
if (url.match(infoRegex)) {
2021-10-11 18:39:36 +00:00
// Hide metadata file
2021-05-23 19:24:04 +00:00
link.parentElement.parentElement.style.display = 'none';
let findOriginal
2021-10-11 18:39:36 +00:00
// Place TV show information in the beginning of the table
2021-05-23 19:24:04 +00:00
if (url === 'tvshow.nfo' || url === 'poster.jpg') {
2021-05-24 19:05:42 +00:00
if (url === 'poster.jpg' && !allLinks.find((found) => getHref(found) === 'tvshow.nfo')) {
2021-05-23 19:24:04 +00:00
findOriginal = document.querySelector('a:not([href=".."])');
} else {
if (tvShow) {
findOriginal = tvShow;
} else {
const wrapper = document.createElement('div');
wrapper.className = 'highlight';
tvShow = document.createElement('a');
tvShow.className = 'movie';
wrapper.appendChild(tvShow);
bodyInner.insertBefore(wrapper, tableElem);
findOriginal = tvShow;
}
}
2021-10-11 18:39:36 +00:00
// Give posters to seasons
2021-05-23 19:24:04 +00:00
} else if (url.startsWith('season') && url.includes('poster')) {
2021-10-11 18:39:36 +00:00
// Find season by number
2021-05-23 19:24:04 +00:00
const sn = url.match(/season(\d+)/);
if (sn) {
2021-10-11 18:39:36 +00:00
findOriginal = allLinks.find(
({ innerText }) => {
return innerText.endsWith('/')
&& findWholeNumberInText(innerText, parseInt(sn[1], 10));
});
// Find specials folder
2021-05-24 19:05:42 +00:00
} else if (url.includes('specials')) {
2021-10-11 18:39:36 +00:00
findOriginal = allLinks.find(({ innerText }) => {
const matchAgainst = innerText.toLowerCase();
return (matchAgainst.startsWith('specials') || matchAgainst.startsWith('extras'))
&& innerText.endsWith('/');
});
2021-05-23 19:24:04 +00:00
}
2021-10-11 18:39:36 +00:00
// Find the video file associated with this info file
2021-05-23 19:24:04 +00:00
} else {
2021-05-23 19:47:14 +00:00
findOriginal = findByInfoFile(url);
2021-05-23 19:24:04 +00:00
}
2021-10-11 18:39:36 +00:00
// Populate video file/season folder/tv show info box
2021-05-23 19:24:04 +00:00
if (findOriginal) {
2021-05-30 14:52:17 +00:00
if (url.match(/.nfo$/)) {
infoFiles += 1;
}
2021-05-23 19:24:04 +00:00
createOrImproveMovieMeta(findOriginal,
url.match(/.jpg$/) ? link : undefined,
url.match(/.nfo$/) ? link : undefined,
);
}
}
2021-10-11 18:39:36 +00:00
// Replace the back button with a more descriptive one
2021-05-23 19:24:04 +00:00
if (url === '..') {
link.parentElement.parentElement.classList.add('btn-back');
link.innerHTML = arrow + 'Back (..)';
}
});
2021-05-30 14:52:17 +00:00
waitUntilInfoComplete().then(() => sortTableValues(0));
2021-05-23 19:24:04 +00:00
}
2021-05-24 19:05:42 +00:00
function createToggleCheckbox(field, value, description) {
2021-05-23 19:24:04 +00:00
const form = document.createElement('div');
const checkbox = document.createElement('input');
const label = document.createElement('label');
2021-05-24 19:05:42 +00:00
form.className = 'form ' + field;
2021-05-23 19:24:04 +00:00
checkbox.type = 'checkbox';
2021-05-24 19:05:42 +00:00
checkbox.id = field
label.innerText = description;
label.setAttribute('for', field);
2021-05-23 19:24:04 +00:00
form.appendChild(checkbox);
form.appendChild(label)
bodyInner.appendChild(form);
checkbox.addEventListener('change', (e) => {
if (checkbox.checked) {
2021-05-24 19:05:42 +00:00
window.localStorage.setItem(field, 'true');
2021-05-23 19:24:04 +00:00
} else {
2021-05-24 19:05:42 +00:00
window.localStorage.removeItem(field);
2021-05-23 19:24:04 +00:00
}
window.location.reload();
});
2021-05-24 19:05:42 +00:00
checkbox.checked = value === 'true';
2021-05-23 19:24:04 +00:00
}
if ('localStorage' in window) {
const plainMode = window.localStorage.getItem('plainMode');
if (plainMode !== 'true') {
accountForMetadata();
}
2021-05-24 19:05:42 +00:00
createToggleCheckbox('plainMode', plainMode, 'Disable metadata / display plain index listing (less bandwidth required)');
2021-05-23 19:24:04 +00:00
} else {
accountForMetadata();
}
2021-05-30 14:52:17 +00:00
document.querySelectorAll('th').forEach((th) => th.addEventListener('click', (() => {
sortTableValues(Array.from(th.parentNode.children).indexOf(th));
})));