I have a great code here. I managed to modify the HTTP URL link as well as the link text (description) by modifying them both with a warning message and the URL of my choice for each domain. I am currently redirecting to my checkout page.
The code also differentiates with text before and after the link and does not take it into account.
It works :
- In the topics
- In the carousel
- In the previews of the last post in the recent page, unread etcā¦
- In Chat
There are certainly things to improve but Iāll share the code with you here.
ā> This will be my Christmas present
If you have any improvements or suggestions, donāt hesitate @phenomlab
// ------------------------------------------------------------------
// Block Domain URL on topics & Topic teasers & caroussel topic info
// ------------------------------------------------------------------
$(document).ready(function () {
// Function to replace URLs and link text in a given HTML content
function replaceUrls(html) {
// Replace the old domain URLs with the new domain URL and update link text
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?francesoir\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The PW anti fake news system detected a disinformation domain name] š¤</strong></a>');
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?francais\.rt\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The PW anti fake news system detected a domain name of disinformation and propaganda] š¤</strong></a>');
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?fr\.sputniknews\.africa(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The PW anti fake news system detected a domain name of disinformation and propaganda] š¤</strong></a>');
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lecourrierdesstrateges\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The PW anti fake news system detected a disinformation domain name] š¤</strong></a>');
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lelibrepenseur\.org(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The PW anti fake news system detected a disinformation domain name] š¤</strong></a>');
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lesmoutonsenrages\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The anti fake news system detected a domain name spreading hate speech and anti-Semitic comments] š¤</strong></a>');
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?upr\.fr(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The PW anti fake news system detected a disinformation domain name] š¤</strong></a>');
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?lecourrier-du-soir\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The PW anti fake news system detected a disinformation domain name] š¤</strong></a>');
html = html.replace(/(<a[^>]*href="https?:\/\/(?:www\.)?fdesouche\.com(?:\/[^\s>]*)?"[^>]*>)([^<]*)<\/a>/g, '<a href="https://YOUR_URL_REDIRECTION/rules"><strong>š¤ [Oups. The anti-fake news system detected a domain name disseminating racist hate articles] š¤</strong></a>');
// Add more lines for additional domains if needed
return html;
}
// Function to replace URLs and link text in a given content element
function replaceUrlsInContent($content) {
$content.html(function (_, oldHtml) {
return replaceUrls(oldHtml);
});
}
// Replace URLs and link text when a new post is added
$(window).on('action:posts.loaded', function () {
$('[component="post"]').each(function () {
var $postContent = $(this).find('.content, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break');
// Replace URLs and link text in the post content
$postContent.html(replaceUrls($postContent.html()));
});
});
// Replace URLs and link text when a post is edited
$(window).on('action:topic.loaded', function () {
$('[component="post"]').each(function () {
var $postContent = $(this).find('.content, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break');
// Replace URLs and link text in the post content
$postContent.html(replaceUrls($postContent.html()));
});
});
// Replace URLs and link text when a new page is loaded or content is updated for topic teaser & carroussel topic info
$(document).ajaxComplete(function () {
$('.post-content.text-xs.ps-2.line-clamp-sm-2.lh-sm.text-break, .topic-info.text-sm.text-break, .content.mt-2.text-break, .stretched-link, .message-body.ps-0.py-0.overflow-auto.text-break').each(function () {
var $content = $(this);
replaceUrlsInContent($content);
});
});
});
PS : nothing to see but appearance code on topics bug :
The Block code is no longer displayed in its entirety but only on one line :