在我的某些标签描述中,我想添加一个像这样的链接:
<a href="https://store.sgiant.me" target="_blank">Click here</a> to visit the official Sir Giant store.
当我第一次保存时,它会生效,并且会在新窗口中打开。
当我刷新页面时,它不再生效,当我检查代码时,它变成了:
<a href="https://store.sgiant.me">Click here</a> to visit the official Sir Giant store.
我已经将“在新标签页中打开外部链接”的选项设置为“是”,但这似乎对我在添加 HTML 代码的这个特定情况没有影响。
ChatGPT 建议创建一个包含 JS 选项卡的组件:
import { apiInitializer } from "discourse/lib/api";
export default apiInitializer((api) => {
function processTagLinks() {
document.querySelectorAll('.tag-title-contents a[href^="http"]').forEach(link => {
if (!link.href.includes(window.location.hostname) && !link.hasAttribute('data-processed')) {
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
link.setAttribute('data-processed', 'true');
}
});
}
// Process immediately
processTagLinks();
// Watch for DOM changes
const observer = new MutationObserver(() => {
processTagLinks();
});
observer.observe(document.body, {
childList: true,
subtree: true
});
// Also run on page changes
api.onPageChange(() => {
setTimeout(processTagLinks, 100);
});
});
但我不是开发者,所以我不知道这是否是一个好的解决方案?
