Skip to content

Instantly share code, notes, and snippets.

@JulienRAVIA
Created November 6, 2024 15:06
Show Gist options
  • Save JulienRAVIA/347257f27217aa19e0855c391283ccef to your computer and use it in GitHub Desktop.
Save JulienRAVIA/347257f27217aa19e0855c391283ccef to your computer and use it in GitHub Desktop.
TMDB Calendar
// ==UserScript==
// @name Ajouter film à Google Calendar sur TheMovieDB
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Ajoute un bouton pour ajouter un film à Google Calendar depuis TheMovieDB
// @author Xylis
// @match https://www.themoviedb.org/movie/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=themoviedb.org
// @grant none
// ==/UserScript==
(function() {
'use strict';
const TimeFormats = {
dateTimeLocal: "YYYY-MM-DD[T]HH:mm:ss",
dateTimeUTC: "YYYYMMDD[T]HHmmss[Z]",
allDay: "YYYYMMDD",
};
function formatTimes(date) {
let formatted = null;
console.log({ date });
if (date) {
let month = date.getUTCMonth() + 1;
month = month.toString().padStart(2, '0');
let day = date.getDate();
day = day.toString().padStart(2, '0');
formatted = `${date.getFullYear()}${month}${day}`
console.log({ formatted });
}
return formatted;
}
function createLink(title, date, overview) {
const startDate = formatTimes(date);
const endDate = formatTimes(date);
const details = {
action: "TEMPLATE",
text: title,
dates: `${startDate}/${endDate}`,
details: overview
};
const params = new URLSearchParams(details);
return `https://calendar.google.com/calendar/render?${params.toString()}`;
}
function parseDate(str) {
let result = str.replace(/\s?\(.*?\)/, '');
var m = result.match(/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/);
return (m) ? new Date(m[3], m[2]-1, m[1]) : null;
}
// Récupérer le titre du film
const movieTitle = document.querySelector('.title a').textContent;
console.log({ movieTitle });
// Récupérer la date de sortie du film
const releaseDateElement = document.querySelector('.release');
let releaseDate = releaseDateElement ? parseDate(releaseDateElement.textContent.trim()) : null;
let overview = document.querySelector('.overview > p');
overview = overview ? overview.textContent.trim() : null;
// Créer un bouton "Ajouter au calendrier"
const calendarButton = document.createElement('li');
calendarButton.title = 'Ajouter au calendrier';
const calendarLink = document.createElement('a');
const calendarIcon = document.createElement('span');
calendarIcon.textContent = "Ajouter au calendrier";
if (releaseDate) {
const calendarUrl = createLink(movieTitle, releaseDate, overview);
calendarLink.href = calendarUrl;
calendarLink.target = '_blank';
calendarLink.appendChild(calendarIcon);
calendarButton.appendChild(calendarLink);
// Ajouter le bouton à la page, juste après le titre du film
const titleElement = document.querySelector('ul.auto.actions');
if (titleElement) {
titleElement.appendChild(calendarButton);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment