|
1 | 1 | // ==UserScript== |
2 | | -// @version 2.4.26324.25526 |
| 2 | +// @version 2.4.26325.5652 |
3 | 3 | // @author Write |
4 | 4 | // @name OphirofoxScript |
5 | 5 | // @grant GM.getValue |
|
1354 | 1354 | return titleElem && titleElem.textContent; |
1355 | 1355 | } |
1356 | 1356 |
|
1357 | | - let buttonAdded = false; |
1358 | | - |
1359 | 1357 | async function addEuropresseButton() { |
1360 | | - if (!buttonAdded) { |
| 1358 | + const ophiroBtnPresence = document.querySelector('.ophirofox-europresse'); |
| 1359 | + if (!ophiroBtnPresence) { |
1361 | 1360 | const elt = document.querySelector("button[aria-label=Commenter]")?.parentElement?.parentElement; |
1362 | 1361 | if (elt) { |
1363 | 1362 | const a = await ophirofoxEuropresseLink(extractKeywords()); |
1364 | 1363 | elt.appendChild(a); |
1365 | | - buttonAdded = true; |
1366 | 1364 | } |
1367 | 1365 | } |
1368 | 1366 | } |
|
1374 | 1372 | by a new meta with name ad:postAcces) and add the button (this is the first observer). |
1375 | 1373 | 2. Or a page is newly routed (for instance, when one goes from the homepage to an article) : |
1376 | 1374 | - it is detected with the second observer that watches for changes in <title> and reset the button |
1377 | | - - we wait for the end of actual loading of the new content by observing <main><meta content>. |
| 1375 | + - we wait for the end of actual loading of the new content by checking if meta[name="ad:postAccess"] exist. |
1378 | 1376 | */ |
1379 | 1377 |
|
1380 | | - const isPremium = () => { |
1381 | | - if (document.querySelector("meta[name='ad:postAccess']").content == 'subscribers') { |
| 1378 | + const isPremium = (metaElement) => { |
| 1379 | + if (metaElement.content == 'subscribers') { |
1382 | 1380 | return true; |
1383 | 1381 | } |
1384 | 1382 | return false; |
1385 | 1383 | }; |
1386 | 1384 |
|
1387 | | - const callback = (mutationList, observer) => { |
1388 | | - if (document.querySelector('meta[name="ad:postAccess"]')) { |
1389 | | - addEuropresseButton(); |
| 1385 | + // Observer [ Direct URL Access ] |
| 1386 | + const callbackDirectAccess = (mutationList, observer) => { |
| 1387 | + const metaElement = document.querySelector('meta[name="ad:postAccess"]'); |
| 1388 | + if (metaElement) { |
| 1389 | + if (isPremium(metaElement)) { |
| 1390 | + addEuropresseButton(); |
| 1391 | + } |
1390 | 1392 | observer.disconnect(); |
1391 | 1393 | return; |
1392 | 1394 | } |
1393 | 1395 | for (const mutation of mutationList) { |
1394 | 1396 | for (const e of mutation.addedNodes) { |
1395 | 1397 | if (e.name == "ad:postAccess") { |
1396 | | - if (isPremium()) |
| 1398 | + if (isPremium(e)) { |
1397 | 1399 | addEuropresseButton(); |
| 1400 | + } |
| 1401 | + observer.disconnect(); |
| 1402 | + return; |
1398 | 1403 | } |
1399 | 1404 | } |
1400 | 1405 | } |
1401 | 1406 | }; |
1402 | 1407 |
|
1403 | | - const observer = new MutationObserver(callback); |
1404 | | - observer.observe(document.body, { |
1405 | | - childList: true |
1406 | | - }); |
1407 | | - |
1408 | | - const observerTitle = new MutationObserver(() => { |
1409 | | - buttonAdded = false; |
1410 | | - if (isPremium()) |
1411 | | - addEuropresseButton(); |
1412 | | - }); |
1413 | | - |
1414 | | - const title = document.querySelector("title") |
1415 | | - observerTitle.observe(title, { |
| 1408 | + const observerDirectAccess = new MutationObserver(callbackDirectAccess); |
| 1409 | + observerDirectAccess.observe(document.body, { |
1416 | 1410 | childList: true, |
1417 | 1411 | subtree: false |
1418 | 1412 | }); |
1419 | 1413 |
|
1420 | | - const observerMain = new MutationObserver(() => { |
1421 | | - addEuropresseButton(); |
1422 | | - }); |
1423 | | - const main = document.querySelector("main meta[content]") |
1424 | | - observerMain.observe(main, { |
| 1414 | + // Observer [ Dynamic page Loading ] |
| 1415 | + const callbackDynamicLoading = (mutationList, observer) => { |
| 1416 | + const metaElement = document.querySelector('meta[name="ad:postAccess"]'); |
| 1417 | + if (metaElement) { |
| 1418 | + if (isPremium(metaElement)) { |
| 1419 | + addEuropresseButton(); |
| 1420 | + } |
| 1421 | + } |
| 1422 | + }; |
| 1423 | + |
| 1424 | + const observerDynamicLoading = new MutationObserver(callbackDynamicLoading); |
| 1425 | + observerDynamicLoading.observe(document.querySelector('title'), { |
1425 | 1426 | childList: true, |
1426 | 1427 | subtree: false |
1427 | 1428 | }); |
|
1862 | 1863 | if ("https://www.nouvelobs.com/*".includes(hostname)) { |
1863 | 1864 |
|
1864 | 1865 | window.addEventListener("load", function(event) { |
1865 | | - function findPremiumBanner() { |
1866 | | - const title = document.querySelector(".article-page-header"); |
1867 | | - if (!title) return null; |
1868 | | - const elems = title.parentElement.querySelectorAll("a"); |
1869 | | - return [...elems].find(d => d.href.includes("ph-abo")) |
| 1866 | + function extractKeywords() { |
| 1867 | + return document.querySelector("h1").textContent; |
1870 | 1868 | } |
1871 | 1869 |
|
| 1870 | + const isPremium = () => { |
| 1871 | + const metaElement = document.querySelector('meta[name="ad:teaser"]'); |
| 1872 | + if (metaElement) { |
| 1873 | + if (metaElement.content === 'true') |
| 1874 | + return true; |
| 1875 | + } |
| 1876 | + return false; |
| 1877 | + }; |
| 1878 | + |
1872 | 1879 | async function onLoad() { |
1873 | | - const head = findPremiumBanner(); |
1874 | | - if (!head) return; |
1875 | | - head.after(await ophirofoxEuropresseLink()); |
| 1880 | + if (!isPremium()) return; |
| 1881 | + document.querySelector("h1").after(await ophirofoxEuropresseLink(extractKeywords())); |
1876 | 1882 | } |
1877 | 1883 |
|
1878 | 1884 | onLoad().catch(console.error); |
|
1890 | 1896 | font-family: "Karla",ff-karla,sans-serif; |
1891 | 1897 | font-weight: 700; |
1892 | 1898 | text-align: center; |
1893 | | - margin-bottom: 2rem!important; |
1894 | | - |
| 1899 | + display: block; |
| 1900 | + width: 200px; |
| 1901 | + margin-top: 1rem; |
| 1902 | + margin-bottom: 1rem; |
| 1903 | + |
1895 | 1904 | } |
1896 | 1905 | |
1897 | 1906 | .ophirofox-europresse:hover, .ophirofox-europresse:active { |
1898 | | - background-color: #000000; |
1899 | | - color: #ffffff; |
| 1907 | + background-color: #000000; |
| 1908 | + color: #ffffff; |
1900 | 1909 | } |
1901 | 1910 | `); |
1902 | 1911 | } |
|
0 commit comments