Skip to content

Commit

Permalink
Release from Ophirofox v2.4.26499.27184
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jan 12, 2025
1 parent d79c549 commit d23e48d
Showing 1 changed file with 61 additions and 37 deletions.
98 changes: 61 additions & 37 deletions ophirofox.user.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ==UserScript==
// @version 2.4.26495.29353
// @version 2.4.26499.27184
// @author Write
// @name OphirofoxScript
// @grant GM.getValue
Expand Down Expand Up @@ -916,20 +916,19 @@

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 @@ -938,21 +937,35 @@
// 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;

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.");
}

// date:published_time is used by default and when firstPublishedDate is not older
if (publishedDate && !publishedDate.trim()) {
// 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)))
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)) {
// If published date is empty or invalid, use firstPublishedDate if available
if (firstPublishedDateInstance && !isNaN(firstPublishedDateInstance)) {
publishedDate = firstPublishedDate;
}
}
Expand All @@ -965,15 +978,14 @@
}
}
});

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 Expand Up @@ -1035,20 +1047,19 @@

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 @@ -1057,21 +1068,35 @@
// 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;

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.");
}

// date:published_time is used by default and when firstPublishedDate is not older
if (publishedDate && !publishedDate.trim()) {
// 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)))
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)) {
// If published date is empty or invalid, use firstPublishedDate if available
if (firstPublishedDateInstance && !isNaN(firstPublishedDateInstance)) {
publishedDate = firstPublishedDate;
}
}
Expand All @@ -1084,15 +1109,14 @@
}
}
});

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 d23e48d

Please sign in to comment.