Skip to content

Commit

Permalink
🐛 Fix Libération published date time used (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
lnoss authored Dec 1, 2024
1 parent a142600 commit baf7f3b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
14 changes: 12 additions & 2 deletions ophirofox/content_scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,26 @@ const ophirofox_config = getOphirofoxConfig();

/**
* Crée un lien vers Europresse avec les keywords donnés
* @param {string} keywords
* @param {string} keywords - input for Europress search engine
* @param {string} publishedTime - article publication date (i.e. 2024-08-27T18:18:55.663Z or 2024-08-27)
* @returns {Promise<HTMLAnchorElement>}
*/
async function ophirofoxEuropresseLink(keywords, { publishedTime } = {}) {
// Keywords is the article name
keywords = keywords ? keywords.trim() : document.querySelector("h1").textContent;

// Trying to determine published time with meta tags (Open Graph values)
// Trying generically to determine published time with meta tags (Open Graph values)
// Heuristics would suggest that it's normally UTC in the media world.
publishedTime = publishedTime || document.querySelector( "meta[property='article:published_time'], meta[property='og:article:published_time'], meta[property='date:published_time']")
?.getAttribute("content") || '';
let publishedTimeInstance = new Date(publishedTime);

if (!isNaN(publishedTimeInstance)) {
// JavaScript, what a pleasure.
publishedTime = publishedTimeInstance.toISOString().slice(0, 10);
} else {
publishedTime = ''
}

// Creating HTML anchor element
const a = document.createElement("a");
Expand Down
31 changes: 28 additions & 3 deletions ophirofox/content_scripts/liberation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ function extractKeywords() {
.getAttribute("content");
}

async function createLink() {
const a = await ophirofoxEuropresseLink(extractKeywords());
/**
* Crée un lien vers Europresse avec les keywords donnés
* @param {string} publishedTime - article publication date (2024-01-01)
* @returns {Promise<HTMLAnchorElement>}
*/
async function createLink(publishedTime) {
const a = await ophirofoxEuropresseLink(extractKeywords(), { publishedTime: publishedTime });
return a;
}

Expand Down Expand Up @@ -40,7 +45,27 @@ async function onLoad(premiumBanner) {

// Not sure if premiumBanner is (and will be) still valid after DOM rewrite
if (!document.querySelector('div.TypologyArticle__BlockPremium-sc-1vro4tp-2 + a.ophirofox-europresse')) {
findPremiumBanner().after(await createLink());
// See #239, Libération replaces date:published_time with the date of edit, which means that a search limited by the time of publication may be too restrictive
// We need to specify the date to use for the generic ophirofoxEuropresseLink function
// Might need refactor if other medias have the same problem, more properties for fail-safe
let publishedDate = document.querySelector( "meta[property='article:published_time'], meta[property='og:article:published_time'], meta[property='date:published_time']")
?.getAttribute("content") || '';
let firstPublishedDate = /\"first_publish_date\":\"(\d{4}-\d{2}-\d{2}[A-Z]+\d{2}:\d{2}:\d{2}.[0-9+-:]+Z)/.exec(document.getElementById('fusion-metadata').textContent)[1] // 2024-08-27T18:18:55.663Z => UTC
let firstPublishedDateInstance = new Date(firstPublishedDate);

// date:published_time is used by default and when firstPublishedDate is not older
if (publishedDate && !publishedDate.trim()) {
// If the first published date is valid and older
if (!isNaN(firstPublishedDateInstance) && (firstPublishedDateInstance < new Date(publishedDate)))
publishedDate = firstPublishedDate;
} else {
// If we are here, Libération did big shit or just changed their Open Grahs properties
if (!isNaN(firstPublishedDateInstance)) {
publishedDate = firstPublishedDate;
}
}

findPremiumBanner().after(await createLink(publishedDate));
console.log('Ophirofox injected after React DOM rewrite');
break;
}
Expand Down

0 comments on commit baf7f3b

Please sign in to comment.