Skip to content

Commit

Permalink
Merge pull request #190 from lbaudin/patch-2
Browse files Browse the repository at this point in the history
Ajout des Échos
  • Loading branch information
lovasoa authored Jan 23, 2024
2 parents 249d5eb + f64f6ce commit 082e63d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ophirofox/content_scripts/lesechos.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.ophirofox-europresse {
background-color: #faec70;
padding: 0.4em;
padding-right: 1em;
padding-left: 1em;
border: 1px solid grey;
border-radius: 50px;
text-decoration: none;
font-size: 0.85em;
}
56 changes: 56 additions & 0 deletions ophirofox/content_scripts/lesechos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function extractKeywords() {
const titleElem = document.querySelector("h1").childNodes[0];
return titleElem && titleElem.textContent;
}

let buttonAdded = false;

async function addEuropresseButton() {
if(!buttonAdded) {
const elt = document.querySelector("button[aria-label=Commenter]")?.parentElement?.parentElement;
if (elt) {
const a = await ophirofoxEuropresseLink(extractKeywords());
elt.appendChild(a);
buttonAdded = true;
}
}
}

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) :
- 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>
*/

const callback = (mutationList, observer) => {
for (const mutation of mutationList) {
for (const e of mutation.addedNodes) {
if(e.id == "rufous-sandbox") {
addEuropresseButton();
}
}
}
};

const observer = new MutationObserver(callback);
observer.observe(document.body, { childList: true});

const observerTitle = new MutationObserver(() => {
buttonAdded = false;
addEuropresseButton();
});
const title = document.querySelector("title")
observerTitle.observe(title, { childList: true, subtree: false });

const observerMain = new MutationObserver(() => {
addEuropresseButton();
});
const main = document.querySelector("main")
observerMain.observe(main, { childList: true, subtree: false });
}

onLoad().catch(console.error);
12 changes: 12 additions & 0 deletions ophirofox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@
"content_scripts/lesoir.css"
]
},
{
"matches": [
"https://www.lesechos.fr/*"
],
"js": [
"content_scripts/config.js",
"content_scripts/lesechos.js"
],
"css": [
"content_scripts/lesechos.css"
]
},
{
"matches": [
"https://www.letemps.ch/*"
Expand Down

0 comments on commit 082e63d

Please sign in to comment.