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 Libération link #195

Merged
merged 1 commit into from
Jan 26, 2024
Merged
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
56 changes: 47 additions & 9 deletions ophirofox/content_scripts/liberation.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
console.log('Ophirofox injected');

function extractKeywords() {
return document
.querySelector("meta[property='og:title']")
.getAttribute("content");
return document
.querySelector("meta[property='og:title']")
.getAttribute("content");
}

async function createLink() {
const a = await ophirofoxEuropresseLink(extractKeywords());
return a;
const a = await ophirofoxEuropresseLink(extractKeywords());
return a;
}

function findPremiumBanner() {
Expand All @@ -20,9 +20,47 @@ function findPremiumBanner() {
}

async function onLoad() {
const premiumBanner = findPremiumBanner();
if (!premiumBanner) return;
premiumBanner.after(await createLink());
const premiumBanner = findPremiumBanner();
if (!premiumBanner) return;

/*
The UI is reactive (and DOM rewritten), so we need to wait for the banner to be added
to the DOM before we can add our link. It seems that it is a React component added to
the DOM in a particular order (last). Safe to use until it is not.
Weird choices for a nearly-static content-driven website with SEO concerns.
*/
const observer = new MutationObserver(async mutationsList => {
for (let mutation of mutationsList) {
if (mutation.addedNodes.length > 0) {
const addedNode = mutation.addedNodes[0];

/*
<figure class="lead-art-wrapper">
<div>
<div class="dynamicClass1 dynamicClass2">
...
</div>
</div>
...
</figure> is added dynamically.
*/

if (addedNode && addedNode.parentElement && addedNode.parentElement.parentElement &&
addedNode.parentElement.parentElement.nodeName === 'FIGURE') {
const link = await createLink();
if (link) {
observer.disconnect();
premiumBanner.after(link);
break;
} else {
console.error('Ophirofox HTML anchor failed to create');
}
}
}
}
});

observer.observe(document.querySelector('#fusion-app'), { childList: true, subtree: true });
}

onLoad();
onLoad();
Loading