Skip to content

Commit d86583f

Browse files
committed
fix: support Horizon topic links for openpostblank
1 parent 4794359 commit d86583f

1 file changed

Lines changed: 58 additions & 19 deletions

File tree

entrypoints/SettingComponents/BasicSettings/MenuOpenpostblank.vue

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,66 @@
99
export default {
1010
props: ["modelValue", "sort"],
1111
emits: ["update:modelValue"],
12-
created() {
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-
}
26-
}
27-
28-
// 使用事件委托,在 body 上监听一次即可,无需 MutationObserver
29-
// 使用 { capture: true } 在捕获阶段拦截事件,确保最高优先级
30-
document.body.addEventListener('click', handleLinkClick, { capture: true });
12+
data() {
13+
return {
14+
handleLinkClick: null,
15+
};
16+
},
17+
watch: {
18+
modelValue(newVal) {
19+
if (newVal) {
20+
this.bindClickHandler();
21+
} else {
22+
this.unbindClickHandler();
3123
}
3224
},
25+
},
26+
methods: {
27+
bindClickHandler() {
28+
if (this.handleLinkClick) return;
29+
30+
const linkSelector = [
31+
".topic-list-item a.raw-topic-link",
32+
".topic-list-item .link-top-line a.raw-link",
33+
".topic-list-item a.topic-excerpt",
34+
".search-results a.search-link",
35+
".search-result-topic a.search-link",
36+
].join(", ");
37+
38+
this.handleLinkClick = (e) => {
39+
if (!(e.target instanceof Element) || e.defaultPrevented || e.button !== 0) {
40+
return;
41+
}
42+
43+
const link = e.target.closest(linkSelector);
44+
45+
if (!link?.href) {
46+
return;
47+
}
48+
49+
e.preventDefault();
50+
e.stopPropagation();
51+
e.stopImmediatePropagation();
52+
53+
window.open(link.href, "_blank", "noopener,noreferrer");
54+
};
55+
56+
document.body.addEventListener("click", this.handleLinkClick, true);
57+
},
58+
unbindClickHandler() {
59+
if (!this.handleLinkClick) return;
3360
61+
document.body.removeEventListener("click", this.handleLinkClick, true);
62+
this.handleLinkClick = null;
63+
},
64+
},
65+
created() {
66+
if (this.modelValue) {
67+
this.bindClickHandler();
68+
}
69+
},
70+
beforeUnmount() {
71+
this.unbindClickHandler();
72+
},
3473
};
3574
</script>

0 commit comments

Comments
 (0)