@@ -55,7 +55,43 @@ injectFunction(
5555
5656// 获取IP地理位置字符串
5757function 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 ( / ^ I P 属 地 [ : : ] * / 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 )
0 commit comments