|
| 1 | +<template> |
| 2 | + <div class="item"> |
| 3 | + <div class="tit"> |
| 4 | + {{ sort }}. 标签屏蔽功能(使用英文,分隔)屏蔽指定标签 |
| 5 | + </div> |
| 6 | + </div> |
| 7 | + <textarea v-model="textarea" @input="handleChange"> </textarea> |
| 8 | +</template> |
| 9 | + |
| 10 | +<script> |
| 11 | +import $ from "jquery"; |
| 12 | +export default { |
| 13 | + props: { |
| 14 | + value: { |
| 15 | + type: String, |
| 16 | + default: "", |
| 17 | + }, |
| 18 | + sort: { |
| 19 | + type: Number, |
| 20 | + required: true, |
| 21 | + }, |
| 22 | + }, |
| 23 | + data() { |
| 24 | + return { |
| 25 | + textarea: this.value, |
| 26 | + }; |
| 27 | + }, |
| 28 | + watch: { |
| 29 | + value(newValue) { |
| 30 | + this.textarea = newValue; |
| 31 | + }, |
| 32 | + }, |
| 33 | + methods: { |
| 34 | + handleChange() { |
| 35 | + this.$emit("update:value", this.textarea); |
| 36 | + }, |
| 37 | + init() { |
| 38 | + if (!this.textarea) return; |
| 39 | +
|
| 40 | + // 将用户输入的关键字分割为数组,并去除空格和空值 |
| 41 | + const keywords = this.textarea |
| 42 | + .split(",") |
| 43 | + .map((keyword) => keyword.trim()) |
| 44 | + .filter(Boolean); |
| 45 | + if (keywords.length === 0) return; |
| 46 | +
|
| 47 | + // 安全检查 jQuery 的使用,防止元素不可用时出错 |
| 48 | + try { |
| 49 | + // 检查话题标签 |
| 50 | + $(".topic-list-item .discourse-tags a") |
| 51 | + .filter((index, element) => { |
| 52 | + const text = $(element).text(); |
| 53 | + return keywords.some((keyword) => text.includes(keyword)); |
| 54 | + }) |
| 55 | + .parents("tr.topic-list-item") |
| 56 | + .remove(); |
| 57 | + } catch (error) { |
| 58 | + console.error("init 方法出错:", error); |
| 59 | + } |
| 60 | + }, |
| 61 | + }, |
| 62 | + created() { |
| 63 | + if (this.textarea) { |
| 64 | + let previousTopicListLength = 0; |
| 65 | + let previousPostStreamLength = 0; |
| 66 | +
|
| 67 | + this.pollingInterval = setInterval(() => { |
| 68 | + try { |
| 69 | + const currentTopicListLength = $(".topic-list-body tr").length || 0; |
| 70 | + const currentPostStreamLength = $(".post-stream .topic-post").length || 0; |
| 71 | +
|
| 72 | + if (previousTopicListLength !== currentTopicListLength) { |
| 73 | + previousTopicListLength = currentTopicListLength; |
| 74 | + this.init(); |
| 75 | + } |
| 76 | + if (previousPostStreamLength !== currentPostStreamLength) { |
| 77 | + previousPostStreamLength = currentPostStreamLength; |
| 78 | + this.init(); |
| 79 | + } |
| 80 | + } catch (error) { |
| 81 | + console.error("轮询逻辑出错:", error); |
| 82 | + } |
| 83 | + }, 1000); |
| 84 | + } |
| 85 | + }, |
| 86 | + beforeDestroy() { |
| 87 | + // 在组件销毁前清除定时器,防止内存泄漏 |
| 88 | + if (this.pollingInterval) { |
| 89 | + clearInterval(this.pollingInterval); |
| 90 | + } |
| 91 | + }, |
| 92 | +}; |
| 93 | +</script> |
| 94 | + |
| 95 | +<style lang="less" scoped> |
| 96 | +.item { |
| 97 | + border: none !important; |
| 98 | +} |
| 99 | +</style> |
0 commit comments