Skip to content

Commit

Permalink
Add support for linking to PDFs (#255)
Browse files Browse the repository at this point in the history
Support for Telerama magazine (PDF)
  • Loading branch information
Altonss authored Sep 5, 2024
1 parent 921d5f4 commit 0e5b747
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
69 changes: 59 additions & 10 deletions ophirofox/content_scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLAnchorElement>}
*/
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();
Expand Down Expand Up @@ -192,4 +241,4 @@ async function requiredAdditionalPermissions() {
return missing_permissions;
}

requiredAdditionalPermissions();
requiredAdditionalPermissions();
57 changes: 46 additions & 11 deletions ophirofox/content_scripts/europresse_search.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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']);
Expand Down Expand Up @@ -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") {
Expand Down
44 changes: 44 additions & 0 deletions ophirofox/content_scripts/telerama.js
Original file line number Diff line number Diff line change
@@ -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);
9 changes: 9 additions & 0 deletions ophirofox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"
Expand Down

0 comments on commit 0e5b747

Please sign in to comment.