<script>
(function() {
'use strict';
const config = {
method: 'remove',
allowedDomains: [
window.location.hostname,
'www.' + window.location.hostname
],
whitelist: []
};
function isExternalLink(url) {
try {
const link = new URL(url, window.location.href);
const hostname = link.hostname;
if (config.whitelist.some(domain => hostname.includes(domain))) {
return false;
}
return !config.allowedDomains.some(domain =>
hostname === domain || hostname.endsWith('.' + domain)
);
} catch (e) {
return false;
}
}
function processLink(link) {
const href = link.getAttribute('href');
if (!href || href.startsWith('#') || href.startsWith('javascript:')) {
return;
}
if (isExternalLink(href)) {
switch(config.method) {
case 'block':
link.addEventListener('click', function(e) {
e.preventDefault();
alert(config.alertMessage);
return false;
});
link.style.color = '#999';
link.style.textDecoration = 'line-through';
link.style.cursor = 'not-allowed';
link.title = 'رابط خارجي محظور';
break;
case 'remove':
const text = link.textContent;
const textNode = document.createTextNode(text);
link.parentNode.replaceChild(textNode, link);
break;
case 'replace':
const span = document.createElement('span');
span.textContent = link.textContent + ' [رابط خارجي محذوف]';
span.style.color = '#999';
link.parentNode.replaceChild(span, link);
break;
}
}
}
function processAllLinks() {
const contentSelectors = [
'.message-body',
'.message-content',
'.bbWrapper',
'article.message-body',
'.js-post'
];
contentSelectors.forEach(selector => {
const containers = document.querySelectorAll(selector);
containers.forEach(container => {
const links = container.querySelectorAll('a[href]');
links.forEach(link => processLink(link));
});
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', processAllLinks);
} else {
processAllLinks();
}
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1) {
if (node.matches('a[href]')) {
processLink(node);
}
const links = node.querySelectorAll('a[href]');
links.forEach(link => processLink(link));
}
});
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
})();
</script>