-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BNF-only redirection mediapart (#290)
- Loading branch information
1 parent
a11f17d
commit c837c8a
Showing
2 changed files
with
59 additions
and
33 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 |
---|---|---|
@@ -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); |
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