Skip to content

Commit

Permalink
Fix lesechos (#242)
Browse files Browse the repository at this point in the history
* fix lesechos

* update descriptions onLoad() of lesechos
  • Loading branch information
Write authored Sep 1, 2024
1 parent dc3c84b commit f2b2085
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
4 changes: 2 additions & 2 deletions ophirofox/content_scripts/lesechos.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
border: 1px solid grey;
border-radius: 50px;
text-decoration: none;
font-size: 0.85em;
}
font-size: 1.2rem;
}
59 changes: 41 additions & 18 deletions ophirofox/content_scripts/lesechos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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 <title> 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);

0 comments on commit f2b2085

Please sign in to comment.