`
));
}
function isCalendarPageCurrentMonth() {
let today = new Date();
// Extract the calendar date from the URL
let calendarDate = new Date(window.location.href.substring(window.location.href.lastIndexOf('/') + 1));
// If there's no date (current month) or it's the current month then return true
return (isNaN(calendarDate) || (calendarDate.getMonth() === today.getMonth() && calendarDate.getYear() === today.getYear()))
}
// Autoscroll to current date
function scrollCurrentDate() {
if(isCalendarPageCurrentMonth()) {
let todayCard = [...$$('.date-separator:not(.filler) .date')].filter((el) => {
return el.textContent == (new Date()).getDate();
})[0];
if (todayCard) {
todayCard.scrollIntoView(true);
// Scroll up to compensate top navbar
let topNav = $('#top-nav');
let offset = -window.getComputedStyle(topNav).getPropertyValue('height').slice(0, -2);
window.scrollBy(0, offset);
}
}
}
// Process calendar page
function processCalendarPage() {
// Torrent links
for (const el of [...$$('.grid-item[data-type="episode"]')]) {
addLinkToGridItem(el, 'episode');
}
if (GM_config.get('autoscrollToday')) {
scrollCurrentDate();
}
// Settings menu icon
let menuIcon = createElement(
`
`
);
menuIcon = $('.sidenav-inner').appendChild(menuIcon);
addEventListener(menuIcon, 'click', () => GM_config.open());
// Jump icon
if(isCalendarPageCurrentMonth()) {
let jumpIcon = createElement(
`
`
);
jumpIcon = $('body').appendChild(jumpIcon);
addEventListener(jumpIcon, 'click', () => scrollCurrentDate());
}
}
// Process show page
function processShowsPage() {
for (const el of [...$$('.grid-item[data-type="show"]')]) { addLinkToGridItem(el, 'show'); }
}
// Process show page
function processShowPage() {
for (const el of [...$$('.grid-item[data-type="season"]')]) { addLinkToGridItem(el, 'season'); }
addLinkToActionList($('.action-buttons'), 'show');
}
// Process season page
function processSeasonPage() {
for (const el of [...$$('.grid-item[data-type="episode"]')]) { addLinkToGridItem(el, 'episode'); }
addLinkToActionList($('.action-buttons'), 'season');
}
// Process episode page
function processEpisodePage() {
addLinkToActionList($('.action-buttons'), 'episode');
}
// Process movies page
function processMoviesPage() {
for (const el of [...$$('.grid-item[data-type="movie"]')]) { addLinkToGridItem(el, 'movie'); }
}
// Process movie page
function processMoviePage() {
addLinkToActionList($('.action-buttons'), 'movie');
}
// Main function
function processPage() {
if (regex.calendar.test(location.pathname)) {
processCalendarPage();
}
else if (regex.shows.test(location.pathname)) {
processShowsPage();
}
else if (regex.show.test(location.pathname)) {
processShowPage();
}
else if (regex.season.test(location.pathname)) {
processSeasonPage();
}
else if (regex.episode.test(location.pathname)) {
processEpisodePage();
}
else if (regex.movies.test(location.pathname)) {
processMoviesPage();
}
else if (regex.movie.test(location.pathname)) {
processMoviePage();
}
}
// Executes the callback after the page finishes loading
// Using a MutationObserver, a timout is set every time a new mutation happens,
// if either the elapsed time bewteen mutations is greater than intervalTime or
// the complete elapsed time is greater than maxWaitTime the callback is executed
function whenPageReady(callback, intervalTime, maxWaitTime = 2500) {
pageReady.startTimer = Date.now();
console.log('The Pirate Calendar: waiting for page to load');
let observerCallback = (mutationList, observer) => {
if (pageReady.timeout) {
let delay = (Date.now() - pageReady.startTimer) > maxWaitTime ? 0 : intervalTime;
clearTimeout(pageReady.timeout);
pageReady.timeout = setTimeout(() => {
console.log(`The Pirate Calendar: page ready in ${Date.now() - pageReady.startTimer}ms!`);
clearTimeout(pageReady.timeout);
pageReady.timeout = null;
observer.disconnect();
callback();
}, intervalTime)
} else {
observer.disconnect();
}
};
let observer = new MutationObserver(observerCallback);
observer.observe($('body'), { attributes: true, childList: true, subtree: true });
}
function init() {
whenPageReady(() => {
// Apply styles
$('head').append(createElement(style));
validateSettings();
processPage();
applySettings();
}, 250);
}
})();