Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lesechos #242

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading