Skip to content

Commit

Permalink
Fix fusionMetadata returning null on firefox (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
Write authored Jan 11, 2025
1 parent e30a680 commit 6cd9b5b
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions ophirofox/content_scripts/liberation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@ function findPremiumBanner() {

async function onLoad(premiumBanner) {
if (premiumBanner) {
/*
The UI is reactive (and DOM rewritten), so we need to wait for some nodes to be rewritten to the DOM
before we can add our link. It seems that the React components are added to the DOM in a particular order.
/*
The UI is reactive (and DOM rewritten), so we need to wait for some nodes to be rewritten to the DOM
before we can add our link. It seems that the React components are added to the DOM in a particular order.
With heavy loading, the MutationObserver execution is too late, and only catch .dossier-feed class.
After caching, we can rely on the .article-body-paywall added node.
Weird choices for a nearly-static content-driven website with SEO concerns.
*/
const observer = new MutationObserver(async mutationsList => {
for (let mutation of mutationsList) {
if (mutation.addedNodes.length > 0) {
const addedNode = mutation.addedNodes[0];

if (addedNode.classList.contains('dossier-feed') ||
addedNode.classList.contains('article-body-paywall')
) {
Expand All @@ -46,23 +45,37 @@ 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')) {
// 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
// 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);
let publishedDate = document.querySelector("meta[property='article:published_time'], meta[property='og:article:published_time'], meta[property='date:published_time']")?.getAttribute("content") || '';

let firstPublishedDate = null;
let firstPublishedDateInstance = null;

// date:published_time is used by default and when firstPublishedDate is not older
if (publishedDate && !publishedDate.trim()) {
let fusionMetadata = document.getElementById('fusion-metadata');
if (fusionMetadata && fusionMetadata.textContent) {
let match = /"first_publish_date":"(\d{4}-\d{2}-\d{2}[A-Z]+\d{2}:\d{2}:\d{2}\.[0-9+-:]+Z)"/.exec(fusionMetadata.textContent); // 2024-08-27T18:18:55.663Z => UTC
if (match) {
firstPublishedDate = match[1];
firstPublishedDateInstance = new Date(firstPublishedDate);
} else {
console.error("No match for 'first_publish_date' found.");
}
} else {
console.error("'fusion-metadata' element not found or empty.");
}

// Check if publishedDate exists and is not empty
if (publishedDate && publishedDate.trim()) {
// If the first published date is valid and older
if (!isNaN(firstPublishedDateInstance) && (firstPublishedDateInstance < new Date(publishedDate)))
publishedDate = firstPublishedDate;
if (firstPublishedDateInstance && !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;
}
// If published date is empty or invalid, use firstPublishedDate if available
if (firstPublishedDateInstance && !isNaN(firstPublishedDateInstance)) {
publishedDate = firstPublishedDate;
}
}

findPremiumBanner().after(await createLink(publishedDate));
Expand All @@ -73,12 +86,11 @@ async function onLoad(premiumBanner) {
}
}
});

observer.observe(document.querySelector('#fusion-app'), { childList: true, subtree: true });
}
}

// Edge-cases, it costs nothing to try to add it *à l'anciene*
// Edge-cases, it costs nothing to try to add it *à l'ancienne*
createLink().then(link => {
const premiumBanner = findPremiumBanner();
if (premiumBanner) {
Expand Down

0 comments on commit 6cd9b5b

Please sign in to comment.