-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from lbaudin/patch-2
Ajout des Échos
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters