diff --git a/README.md b/README.md index c7df4ab8..9b69f209 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ Voici la liste triée par ordre alphabétique : - [Le Point](https://www.lepoint.fr) - [Libération](https://www.liberation.fr/) - [Les Échos](https://www.lesechos.fr) +- [Télérama (Magazine en PDF)](https://www.telerama.fr/kiosque/telerama) ### Presse régionale - [Corse Matin](https://www.corsematin.com/) diff --git a/ophirofox/content_scripts/config.js b/ophirofox/content_scripts/config.js index 121f0a6a..60470b45 100644 --- a/ophirofox/content_scripts/config.js +++ b/ophirofox/content_scripts/config.js @@ -85,15 +85,64 @@ async function ophirofoxEuropresseLink(keywords, { publishedTime } = {}) { a.textContent = "Lire sur Europresse"; a.className = "ophirofox-europresse"; - const setKeywords = () => new Promise(accept => - chrome.storage.local.set({ - "ophirofox_read_request": - { - 'search_terms': keywords, - 'published_time': publishedTime - } - }, - accept)); + const setKeywords = () => new Promise(accept => { + Promise.all([ + // set request type (read, or readPDF) + chrome.storage.local.set({ + "ophirofox_request_type": + { + 'type': 'read' + } + }), + chrome.storage.local.set({ + "ophirofox_read_request": + { + 'search_terms': keywords, + 'published_time': publishedTime + } + }), + ]).then(() => accept()); + }); + a.onmousedown = setKeywords; + a.onclick = async function (evt) { + evt.preventDefault(); + const [{ AUTH_URL }] = await Promise.all([ophirofox_config, setKeywords()]); + window.location = AUTH_URL + } + ophirofox_config.then(({ AUTH_URL }) => { a.href = AUTH_URL }); + return a; +} + +/** + * Crée un lien vers Europresse pour la consultation de PDF, avec l'id du media, et la date de parution + * @param {string} media_id + * @param {string} publishedTime + * @returns {Promise} + */ +async function ophirofoxEuropressePDFLink(media_id, publishedTime) { + // Creating HTML anchor element + const a = document.createElement("a"); + a.textContent = "Lire sur Europresse"; + a.className = "ophirofox-europresse"; + + const setKeywords = () => new Promise(accept => { + Promise.all([ + // set request type (read, or readPDF) + chrome.storage.local.set({ + "ophirofox_request_type": + { + 'type': 'readPDF' + } + }), + chrome.storage.local.set({ + "ophirofox_readPDF_request": + { + 'media_id': media_id, + 'published_time': publishedTime + } + }), + ]).then(() => accept()); + }); a.onmousedown = setKeywords; a.onclick = async function (evt) { evt.preventDefault(); @@ -192,4 +241,4 @@ async function requiredAdditionalPermissions() { return missing_permissions; } -requiredAdditionalPermissions(); \ No newline at end of file +requiredAdditionalPermissions(); diff --git a/ophirofox/content_scripts/europresse_search.js b/ophirofox/content_scripts/europresse_search.js index bf79e379..b3929f57 100644 --- a/ophirofox/content_scripts/europresse_search.js +++ b/ophirofox/content_scripts/europresse_search.js @@ -1,3 +1,13 @@ +async function consumeRequestType() { + return new Promise((accept, reject) => { + chrome.storage.local.get("ophirofox_request_type", + (r) => { + accept(r.ophirofox_request_type); + chrome.storage.local.remove("ophirofox_request_type"); + }); + }) +} + async function consumeReadRequest() { return new Promise((accept, reject) => { chrome.storage.local.get("ophirofox_read_request", @@ -8,17 +18,18 @@ async function consumeReadRequest() { }) } -async function onLoad() { - ophirofoxRealoadOnExpired(); +async function consumeReadPDFRequest() { + return new Promise((accept, reject) => { + chrome.storage.local.get("ophirofox_readPDF_request", + (r) => { + accept(r.ophirofox_readPDF_request); + chrome.storage.local.remove("ophirofox_readPDF_request"); + }); + }) +} + +async function loadRead(){ const path = window.location.pathname; - if (!( - path.startsWith("/Search/Reading") || - path.startsWith("/Search/Advanced") || - path.startsWith("/Search/AdvancedMobile") || - path.startsWith("/Search/Express") || - path.startsWith("/Search/Simple") || - path.startsWith("/Search/Result") - )) return; const { search_terms, published_time } = await consumeReadRequest(); if (!search_terms) return; const stopwords = new Set(['d', 'l', 'et', 'sans', 'or']); @@ -89,10 +100,34 @@ async function onLoad() { date_filter.value = filterValue; } } - keyword_field.form.submit(); } +async function loadReadPDF(){ + const { media_id, published_time } = await consumeReadPDFRequest(); + window.location = window.location.origin + `/PDF/EditionDate?sourceCode=${media_id}&singleDate=${published_time}&useFuzzyDate=false`; +} + +async function onLoad() { + ophirofoxRealoadOnExpired(); + const path = window.location.pathname; + if (!( + path.startsWith("/Search/Reading") || + path.startsWith("/Search/Advanced") || + path.startsWith("/Search/AdvancedMobile") || + path.startsWith("/Search/Express") || + path.startsWith("/Search/Simple") || + path.startsWith("/Search/Result") + )) return; + const { type } = await consumeRequestType(); + console.log("request_type", type); + if (type == "readPDF") { + await loadReadPDF(); + } else { + await loadRead(); + } +} + function ophirofoxRealoadOnExpired() { const params = new URLSearchParams(window.location.search) if (params.get("ErrorCode") == "4000112") { diff --git a/ophirofox/content_scripts/telerama.js b/ophirofox/content_scripts/telerama.js new file mode 100644 index 00000000..3b13fb0b --- /dev/null +++ b/ophirofox/content_scripts/telerama.js @@ -0,0 +1,44 @@ +// Function to format date to "YYYY-MM-DD" +function formatDate(date) { + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, '0'); + const day = String(date.getDate()).padStart(2, '0'); + return `${year}-${month}-${day}`; +} + +// Function to add days to a given date +function addDays(date, days) { + const result = new Date(date); + result.setDate(result.getDate() + days); + return result; +} + +async function onLoad() { + // Get articles + const articles = document.querySelectorAll('#liste-magazine-telerama article'); + + for (const article of articles) { + const linkElement = article.querySelector('a.popin-link'); + const tagName = linkElement.getAttribute('data-tagname'); + const datePattern = /clic_magazine_(\d{4}-\d{2}-\d{2})/; + const match = tagName.match(datePattern); + const articleDate = new Date(match[1]); + // Check if the date object is valid + if (isNaN(articleDate.getTime())) { + console.error(`Invalid date: ${year}-${month + 1}-${day}`); + return; + } + // Calculate the new date + 3 days + const newDate = addDays(articleDate, 3); + const formattedDate = formatDate(newDate); + + // Generate the link + const a = await ophirofoxEuropressePDFLink("TA_P",formattedDate); + a.classList.add("btn", "btn--premium"); + + // Inject the link into the article + article.appendChild(a); + } +} + +onLoad().catch(console.error); diff --git a/ophirofox/manifest.json b/ophirofox/manifest.json index 7305bcd8..3e1ab111 100644 --- a/ophirofox/manifest.json +++ b/ophirofox/manifest.json @@ -184,6 +184,15 @@ "content_scripts/lacroix.css" ] }, + { + "matches": [ + "https://www.telerama.fr/kiosque/telerama" + ], + "js": [ + "content_scripts/config.js", + "content_scripts/telerama.js" + ] + }, { "matches": [ "https://www.courrierinternational.com/*"