Skip to content

Commit 2add22c

Browse files
authored
Merge pull request #269 from cyscjl/fix/open-post-blank
fix: 修复点击搜索结果无法在新标签页打开的问题
2 parents 57c3567 + 3b4a3cf commit 2add22c

1 file changed

Lines changed: 19 additions & 51 deletions

File tree

entrypoints/SettingComponents/BasicSettings/MenuOpenpostblank.vue

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,26 @@ export default {
1010
props: ["modelValue", "sort"],
1111
emits: ["update:modelValue"],
1212
created() {
13-
if (this.modelValue) {
14-
15-
// 处理链接点击
16-
function handleLinkClick(e) {
17-
e.preventDefault();
18-
e.stopPropagation(); // 阻止事件冒泡
19-
e.stopImmediatePropagation(); // 阻止事件捕获和后续相同事件的处理
20-
window.open(this.href, '_blank', 'noopener,noreferrer');
21-
}
22-
23-
// 主要功能
24-
function processLinks() {
25-
// 查找所有帖子标题链接,扩展选择器以包含搜索页面的链接
26-
const links = document.querySelectorAll('.link-top-line a.title:not([data-processed])');
27-
28-
links.forEach(link => {
29-
// 标记该链接已处理
30-
link.setAttribute('data-processed', 'true');
31-
// 添加事件监听器
32-
link.addEventListener('click', handleLinkClick);
33-
});
34-
}
35-
36-
// 监听 DOM 变化,处理动态加载的内容
37-
const observer = new MutationObserver((mutations) => {
38-
const hasNewLinks = mutations.some(mutation => {
39-
return Array.from(mutation.addedNodes).some(node => {
40-
return node.nodeType === 1 && (
41-
node.classList.contains('link-top-line') ||
42-
node.querySelector('.link-top-line') ||
43-
node.classList.contains('search-results') ||
44-
node.querySelector('.search-results')
45-
);
46-
});
47-
});
48-
49-
if (hasNewLinks) {
50-
processLinks();
13+
if (this.modelValue) {
14+
function handleLinkClick(e) {
15+
const linkSelector='.link-top-line a.raw-link, .search-results a.search-link, .search-result-topic a.search-link'
16+
// 检查被点击的元素或其父元素是否是要找的<a>标签
17+
const link = e.target.closest(linkSelector);
18+
19+
if (link && link.href) {
20+
e.preventDefault();
21+
e.stopPropagation();
22+
e.stopImmediatePropagation();
23+
24+
window.open(link.href, '_blank', 'noopener,noreferrer');
25+
}
5126
}
52-
});
53-
54-
// 开始观察
55-
observer.observe(document.body, {
56-
childList: true,
57-
subtree: true
58-
});
59-
60-
// 初始处理
61-
processLinks();
62-
63-
}
64-
},
27+
28+
// 使用事件委托,在 body 上监听一次即可,无需 MutationObserver
29+
// 使用 { capture: true } 在捕获阶段拦截事件,确保最高优先级
30+
document.body.addEventListener('click', handleLinkClick, { capture: true });
31+
}
32+
},
6533
6634
};
6735
</script>

0 commit comments

Comments
 (0)