Created
November 7, 2024 13:32
-
-
Save JulienRAVIA/26b238dfb70d8684347cc62cf6795230 to your computer and use it in GitHub Desktop.
Senscritique userscript google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Ajouter film à Google Calendar sur SensCritique | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Ajoute un bouton pour ajouter un film à Google Calendar depuis SensCritique | |
// @author Xylis | |
// @match https://www.senscritique.com/films/sorties-cinema/*/semaine/* | |
// @match https://www.senscritique.com/films/sorties-cinema/*/* | |
// @match https://www.senscritique.com/films/sorties-cinema/* | |
// @match https://www.senscritique.com/films/cette-semaine | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=senscritique.com | |
// @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) { | |
if (str === null) { | |
return null; | |
} | |
const regex = /(\d{1,2}) (\w+) (\d{4})/; | |
const replaced = str.replace('décembre', 'decembre').replace('février', 'fevrier'); | |
const m = replaced.match(regex); | |
if (!m) { | |
console.log(str, m); | |
return null; | |
} | |
const jour = m[1]; | |
const moisTexte = m[2]; | |
const annee = m[3]; | |
const moisNumeros = { | |
"janvier": 1, | |
"fevrier": 2, | |
"mars": 3, | |
"avril": 4, | |
"mai": 5, | |
"juin": 6, | |
"juillet": 7, | |
"août": 8, | |
"septembre": 9, | |
"octobre": 10, | |
"novembre": 11, | |
"decembre": 12 | |
}; | |
const mois = moisNumeros[moisTexte.toLowerCase()] || null; | |
return (m) ? new Date(annee, mois - 1, jour) : null; | |
} | |
function updateElements() { | |
const elements = document.querySelectorAll('[data-testid="product-list-item"]'); | |
elements.forEach(element => { | |
const title = element.querySelector('[data-testid="product-title"]')?.textContent; | |
const parsedTitle = title.replace(/\s*\(\d{4}\)/, ''); | |
const releaseDate = element.querySelector('[data-testid="date-release"]')?.textContent; | |
const duration = element.querySelector('[data-testid="duration"]')?.textContent; | |
const genres = element.querySelector('[data-testid="genres"]')?.textContent; | |
const otherInfos = element.querySelector('[data-testid="synopsis"]'); | |
const parsedDate = parseDate(releaseDate); | |
const calendarElement = element.querySelector('[data-testid="calendar-link"]'); | |
if (!calendarElement && parsedDate) { | |
createElement(parsedDate, parsedTitle, otherInfos); | |
} | |
}); | |
} | |
function createElement(parsedDate, parsedTitle, otherInfos) | |
{ | |
if (parsedDate && otherInfos) { | |
const calendarLink = document.createElement('a'); | |
const calendarSpan = document.createElement('p'); | |
calendarSpan.setAttribute('data-testid', 'calendar-link'); | |
calendarSpan.style.marginBottom = '3px'; | |
calendarSpan.style.fontSize = '14px'; | |
calendarSpan.style.color = 'rgb(32, 131, 246)'; | |
calendarSpan.style.fontFamily = '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif'; | |
const calendarUrl = createLink(parsedTitle, parsedDate, ''); | |
calendarLink.href = calendarUrl; | |
calendarLink.target = '_blank'; | |
calendarLink.textContent = "Ajouter au calendrier"; | |
calendarSpan.appendChild(calendarLink); | |
otherInfos.after(calendarSpan); | |
} | |
} | |
function executerPeriodiquement() { | |
updateElements(); // Exécuter le script immédiatement | |
setTimeout(executerPeriodiquement, 1500); | |
} | |
// Lancer l'exécution périodique | |
executerPeriodiquement(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment