From f2b2085bba193e9fe834ea27f714cf3318bec7ee Mon Sep 17 00:00:00 2001 From: Joffrey <541722+Write@users.noreply.github.com> Date: Sun, 1 Sep 2024 07:34:02 +0200 Subject: [PATCH] Fix lesechos (#242) * fix lesechos * update descriptions onLoad() of lesechos --- ophirofox/content_scripts/lesechos.css | 4 +- ophirofox/content_scripts/lesechos.js | 59 ++++++++++++++++++-------- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/ophirofox/content_scripts/lesechos.css b/ophirofox/content_scripts/lesechos.css index a9bb4f60..aecd8f62 100644 --- a/ophirofox/content_scripts/lesechos.css +++ b/ophirofox/content_scripts/lesechos.css @@ -6,5 +6,5 @@ border: 1px solid grey; border-radius: 50px; text-decoration: none; - font-size: 0.85em; -} + font-size: 1.2rem; +} \ No newline at end of file diff --git a/ophirofox/content_scripts/lesechos.js b/ophirofox/content_scripts/lesechos.js index 502bf7ee..dad48d5b 100644 --- a/ophirofox/content_scripts/lesechos.js +++ b/ophirofox/content_scripts/lesechos.js @@ -6,7 +6,7 @@ function extractKeywords() { let buttonAdded = false; async function addEuropresseButton() { - if(!buttonAdded) { + if (!buttonAdded) { const elt = document.querySelector("button[aria-label=Commenter]")?.parentElement?.parentElement; if (elt) { const a = await ophirofoxEuropresseLink(extractKeywords()); @@ -17,40 +17,63 @@ async function addEuropresseButton() { } async function onLoad() { - + /* 2 cases: - 1. either a page is initially loaded, and we must wait for the actual end of loading (determined - by a new iframe #rufous-sandbox) and add the button (this is the first observer) - 2. Or a page is newly routed (for instance, when one goes from the homepage to an article) : + 1. either a page is initially loaded, and we must wait for the actual end of loading (determined + by a new meta with name ad:postAcces) and add the button (this is the first observer). + 2. Or a page is newly routed (for instance, when one goes from the homepage to an article) : - it is detected with the second observer that watches for changes in and reset the button - - we wait for the end of actual loading of the new content by observing <main> + - we wait for the end of actual loading of the new content by observing <main><meta content>. */ - + + const isPremium = () => { + if (document.querySelector("meta[name='ad:postAccess']").content == 'subscribers') { + return true; + } + return false; + }; + const callback = (mutationList, observer) => { + if (document.querySelector('meta[name="ad:postAccess"]')) { + addEuropresseButton(); + observer.disconnect(); + return; + } for (const mutation of mutationList) { for (const e of mutation.addedNodes) { - if(e.id == "rufous-sandbox") { - addEuropresseButton(); + if (e.name == "ad:postAccess") { + if (isPremium()) + addEuropresseButton(); } } } }; const observer = new MutationObserver(callback); - observer.observe(document.body, { childList: true}); - + observer.observe(document.body, { + childList: true + }); + const observerTitle = new MutationObserver(() => { buttonAdded = false; - addEuropresseButton(); + if (isPremium()) + addEuropresseButton(); }); + const title = document.querySelector("title") - observerTitle.observe(title, { childList: true, subtree: false }); - + observerTitle.observe(title, { + childList: true, + subtree: false + }); + const observerMain = new MutationObserver(() => { addEuropresseButton(); - }); - const main = document.querySelector("main") - observerMain.observe(main, { childList: true, subtree: false }); + }); + const main = document.querySelector("main meta[content]") + observerMain.observe(main, { + childList: true, + subtree: false + }); } -onLoad().catch(console.error); +onLoad().catch(console.error); \ No newline at end of file