Skip to content

Commit d41178a

Browse files
committed
feat: 评论显示性别 #210
1 parent 9e4ef38 commit d41178a

7 files changed

Lines changed: 59 additions & 19 deletions

File tree

src/_locales/cmn-CN.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ settings:
413413
clean_url_argument_desc: 自动清理URL中的追踪参数,使链接更加简洁
414414
show_ip_location: 显示IP归属地
415415
show_ip_location_desc: 在视频/动态评论区显示IP归属地
416+
show_sex: 显示用户性别
417+
show_sex_desc: 在视频/动态评论区显示用户性别
416418
volume_balance:
417419
title: 音量均衡
418420
enable: 启用音量均衡

src/_locales/cmn-TW.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ settings:
421421
clean_url_argument_desc: 自動清理URL中的追蹤參數,使連結更加簡潔
422422
show_ip_location: 顯示IP歸屬地
423423
show_ip_location_desc: 在視頻/動態評論區顯示IP歸屬地
424+
show_sex: 顯示使用者性別
425+
show_sex_desc: 在視頻/動態評論區顯示使用者性別
424426
video_player_scroll: 調整顯示模式後滾動
425427
video_player_scroll_desc: 啟用後,在默認模式為寬屏和默認時,會自動滾動到合適位置。
426428
volume_balance:

src/_locales/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ settings:
478478
clean_url_argument_desc: Automatically clean tracking parameters from URLs to make links cleaner
479479
show_ip_location: Show IP home location
480480
show_ip_location_desc: Display IP home in the video/dynamic comment area
481+
show_sex: Show user gender
482+
show_sex_desc: Display user gender in the video/dynamic comment area
481483
video_player_scroll: Scroll after adjusting the display mode
482484
video_player_scroll_desc: >-
483485
When enabled, it will automatically scroll to the appropriate position when

src/_locales/jyut.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ settings:
398398
widescreen: 闊螢幕
399399
show_ip_location: 顯示 IP 歸屬地
400400
show_ip_location_desc: 喺影片/動向頁留言區顯示 IP 歸屬地。
401+
show_sex: 顯示用戶性別
402+
show_sex_desc: 喺影片/動向頁留言區顯示用戶性別。
401403
video_player_scroll: 校好顯示模式後自動轆到合適嘅位置
402404
video_player_scroll_desc: 啓用後,若果顯示模式係闊螢幕或預設嗰陣,會自動轆到合適嘅位置。
403405
group_fonts: ''

src/components/Settings/General/General.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ watch(() => settings.value.language, (newValue) => {
8989
<Radio v-model="settings.showIPLocation" />
9090
</SettingsItem>
9191

92+
<SettingsItem :title="$t('settings.show_sex')" :desc="$t('settings.show_sex_desc')">
93+
<Radio v-model="settings.showSex" />
94+
</SettingsItem>
95+
9296
<SettingsItem :title="$t('settings.enable_grid_layout_switcher')">
9397
<Radio v-model="settings.enableGridLayoutSwitcher" />
9498
</SettingsItem>

src/inject/index.ts

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,43 @@ injectFunction(
5555

5656
// 获取IP地理位置字符串
5757
function getLocationString(replyItem: any) {
58-
return replyItem?.reply_control?.location
58+
const location = replyItem?.reply_control?.location
59+
if (typeof location !== 'string')
60+
return location
61+
62+
return location.replace(/^IP[: ]*/u, '')
63+
}
64+
65+
function getSexString(replyItem: any) {
66+
return replyItem?.member?.sex
67+
}
68+
69+
function updateInfoElement(
70+
root: ShadowRoot | null | undefined,
71+
id: string,
72+
shouldShow: boolean,
73+
text: any,
74+
anchor: Element | null | undefined,
75+
): HTMLElement | null {
76+
if (!root)
77+
return null
78+
79+
let element = root.querySelector<HTMLElement>(`#${id}`)
80+
81+
if (!shouldShow || !anchor) {
82+
if (element)
83+
element.remove()
84+
return null
85+
}
86+
87+
if (!element) {
88+
element = document.createElement('div')
89+
element.id = id
90+
anchor.insertAdjacentElement('afterend', element)
91+
}
92+
93+
element.textContent = String(text)
94+
return element
5995
}
6096

6197
// 判断当前页面URL是否支持IP显示
@@ -107,29 +143,19 @@ if (window.customElements && isSupportedPage()) {
107143
const originalUpdate = classConstructor.prototype.update
108144
classConstructor.prototype.update = function (...updateArgs: any[]) {
109145
const result = originalUpdate.apply(this, updateArgs)
110-
const pubDateEl = this.shadowRoot?.querySelector('#pubdate')
146+
const root = this.shadowRoot
147+
const pubDateEl = root?.querySelector('#pubdate')
111148
if (!pubDateEl)
112149
return result
113150

114-
let locationEl = this.shadowRoot?.querySelector('#location')
115151
const locationString = getLocationString(this.data)
152+
const shouldShowLocation = Boolean(currentSettings?.showIPLocation && locationString)
153+
const locationEl = updateInfoElement(root, 'location', shouldShowLocation, locationString, pubDateEl)
116154

117-
// 根据设置决定是否显示IP
118-
if (!currentSettings?.showIPLocation || !locationString) {
119-
if (locationEl)
120-
locationEl.remove()
121-
return result
122-
}
123-
124-
if (locationEl) {
125-
locationEl.textContent = locationString
126-
return result
127-
}
128-
129-
locationEl = document.createElement('div')
130-
locationEl.id = 'location'
131-
locationEl.textContent = locationString
132-
pubDateEl.insertAdjacentElement('afterend', locationEl)
155+
const sexString = getSexString(this.data)
156+
const shouldShowSex = Boolean(currentSettings?.showSex && sexString)
157+
const sexAnchor = locationEl ?? pubDateEl
158+
updateInfoElement(root, 'sex', shouldShowSex, sexString, sexAnchor)
133159
return result
134160
}
135161
return Reflect.apply(target, thisArg, args)

src/logic/storage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export interface Settings {
7171
enableGridLayoutSwitcher: boolean
7272
enableHorizontalScrolling: boolean
7373
showIPLocation: boolean // 添加显示IP归属地设置项
74+
showSex: boolean // 添加显示性别设置项
7475

7576
language: string
7677
customizeFont: 'default' | 'recommend' | 'custom'
@@ -223,6 +224,7 @@ export const originalSettings: Settings = {
223224
enableGridLayoutSwitcher: true,
224225
enableHorizontalScrolling: false,
225226
showIPLocation: true, // 默认启用IP归属地显示
227+
showSex: true, // 默认启用性别显示
226228
language: '',
227229
customizeFont: 'default',
228230
fontFamily: '',

0 commit comments

Comments
 (0)