`,
));
}
}
function isCalendarPageCurrentMonth() {
const today = new Date();
// Extract the calendar date from the URL
const 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()) {
const 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
const topNav = $('#top-nav');
const offset = -window.getComputedStyle(topNav).getPropertyValue('height').slice(0, -2);
window.scrollBy(0, offset);
}
}
}
// Generic actions for every page
function processGenericPage() {
addLinksToActionList();
addLinksToGrid();
}
// Special actions for calendar pages
function processCalendarPage() {
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());
}
}
// Special actions for list pages
function processListPage() {
if (GM_config.get('listPageIconsMode') === 'Squeeze icons!') {
for (const el of [...$$('.quick-icons')]) {
el.classList.add('smallest-tcp');
}
}
if (GM_config.get('listPageIconsMode') === 'Bigger posters') {
for (const el of [...$$('.grid-item')]) {
el.classList.replace('col-md-2', 'col-md-4');
el.classList.replace('col-sm-3', 'col-sm-4');
}
}
if (GM_config.get('listPageIconsMode') === 'Hide heart icon and squeeze') {
for (const el of [...$$('.quick-icons .fa.fa-heart')]) {
toggle(el, false);
}
for (const el of [...$$('.quick-icons')]) {
el.classList.add('smaller-tcp');
}
}
if (GM_config.get('listPageIconsMode') === 'Hide collect and "watch on" icons') {
for (const el of [...$$('.quick-icons .collect')]) {
toggle(el, false);
}
for (const el of [...$$('.quick-icons .watch-now')]) {
toggle(el, false);
}
}
}
// Main function
function processPage() {
processGenericPage();
if (REGEX.calendar.test(location.pathname)) {
processCalendarPage();
} else if (REGEX.list.test(location.pathname)) {
processListPage();
}
}
// 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 full elapsed time is greater than maxWaitTime the callback is executed
function whenPageReady(callback, intervalTime, maxWaitTime = 3000) {
PAGE_READY.startTimer = Date.now();
console.debug('[The Pirate Calendar] Waiting for page to load');
const observerCallback = (mutationList, observer) => {
if (PAGE_READY.timeout) {
clearTimeout(PAGE_READY.timeout);
if ((Date.now() - PAGE_READY.startTimer) > maxWaitTime) {
console.debug(`[The Pirate Calendar] Max wait time exceded, loading script anyway!`);
clearTimeout(PAGE_READY.timeout);
PAGE_READY.timeout = null;
observer.disconnect();
callback();
} else {
PAGE_READY.timeout = setTimeout(() => {
console.debug(`[The Pirate Calendar] Page ready in ${Date.now() - PAGE_READY.startTimer}ms!`);
clearTimeout(PAGE_READY.timeout);
PAGE_READY.timeout = null;
observer.disconnect();
callback();
}, intervalTime);
}
} else {
observer.disconnect();
}
};
const 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);
}
})();