Skip to content

Commit

Permalink
BNF-only redirection mediapart (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremieGiffard authored Feb 2, 2025
1 parent a11f17d commit c837c8a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
87 changes: 56 additions & 31 deletions ophirofox/content_scripts/mediapart.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,73 @@

/**
* @description create link <a> to BNF mirror
* @param {string} AUTH_URL_MEDIAPART
*/
async function createLink(AUTH_URL_MEDIAPART) {
const span = document.createElement("span");
span.textContent = "Lire avec BNF";

const a = document.createElement("a");
a.href = new URL(AUTH_URL_MEDIAPART);
a.appendChild(span);
const span = document.createElement("span");
span.textContent = "Lire avec BNF";

return a;
const a = document.createElement("a");
a.href = new URL(window.location);
a.host = AUTH_URL_MEDIAPART;
a.appendChild(span);
return a;
}

/**
* @description check DOM for article under paywall
* @description check DOM for article under paywall
* @return {HTMLElement} DOM Premium Banner and head of the article
*/
*/
function findPremiumBanner() {
const article = document.querySelector(".news__body__center__container");
if (!article) return null;
const elems = article.querySelectorAll(".paywall-message");
console.log("elements",elems)
//labels not the same for mobile or PC display
const textToFind = ["réservée aux abonné·es", "réservé aux abonné·es"]

return [...elems].filter((balise) => textToFind.some((text) => balise.textContent.toLowerCase().includes(text)) )
const article = document.querySelector(".news__body__center__container");
if (!article) return null;
const elems = article.querySelectorAll(".paywall-message");
//labels not the same for mobile or PC display
const textToFind = ["réservée aux abonné·es", "réservé aux abonné·es"];

return [...elems].filter((balise) =>
textToFind.some((text) => balise.textContent.toLowerCase().includes(text))
);
}

/**@description check for BNF users. If yes, create link button */
async function onLoad() {
/**
* @description if not properly logged on the mirror website, fetch the login page
*/
function handleMediapartMirror(config) {
const navBar = document.querySelector("ul.nav__actions");
const spans = navBar.querySelectorAll("span");

let isNotConnected = Array.from(spans).find(
(elem) => elem.textContent == "Se connecter"
);
if (isNotConnected) {
//account name not found. fetch login page
const LOGIN_PAGE = new URL(
"licence",
"https://" + config.AUTH_URL_MEDIAPART
);
fetch(LOGIN_PAGE).then(() => window.location.reload());
}
}

const config = await configurationsSpecifiques(['BNF'])
if(!config) return;
const reserve = findPremiumBanner();
if (!reserve) return;
async function handleMediapart(config) {
const reserve = findPremiumBanner();
if (!reserve) return;

for (const balise of reserve) {
balise.appendChild(await createLink(config.AUTH_URL_MEDIAPART))
}
for (const balise of reserve) {
balise.appendChild(await createLink(config.AUTH_URL_MEDIAPART));
}
}

/**@description check for users with mediapart access. If yes, create link button */
async function onLoad() {
const config = await configurationsSpecifiques(["BNF"]);
if (!config) return;
const currentPage = new URL(window.location);
if (currentPage.host == config.AUTH_URL_MEDIAPART) {
handleMediapartMirror(config);
} else {
handleMediapart(config);
}
}

setTimeout(function(){
onLoad().catch(console.error);
}, 1000);
onLoad().catch(console.error);
5 changes: 3 additions & 2 deletions ophirofox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@
},
{
"matches": [
"https://www.mediapart.fr/*"
"https://www.mediapart.fr/*",
"https://www-mediapart-fr.bnf.idm.oclc.org/*"
],
"js": [
"content_scripts/config.js",
Expand Down Expand Up @@ -721,7 +722,7 @@
"name": "BNF",
"AUTH_URL": "https://bnf.idm.oclc.org/login?url=https://nouveau.europresse.com/access/ip/default.aspx?un=D000067U_1",
"AUTH_URL_ARRETSURIMAGES" : "www-arretsurimages-net.bnf.idm.oclc.org",
"AUTH_URL_MEDIAPART": "https://bnf.idm.oclc.org/login?url=http://www.mediapart.fr/licence",
"AUTH_URL_MEDIAPART": "www-mediapart-fr.bnf.idm.oclc.org",
"AUTH_URL_PRESSREADER" : "www-pressreader-com.bnf.idm.oclc.org"
},
{
Expand Down

0 comments on commit c837c8a

Please sign in to comment.