/* eslint-disable no-underscore-dangle */ import { defineComponentMetadata } from '@/components/define' import { CommentItem, CommentReplyItem } from '@/components/utils/comment-apis' // 新版评论区IP属地获取 const getIpLocation = (element: HTMLElement) => { const props = (element as any).__vueParentComponent?.props const reply = props?.reply ?? props?.subReply return reply?.reply_control?.location ?? undefined } let version = 2 let bbComment: any // 带IP属地显示的评论创建 const createListCon = function listCon(item, i, pos) { const blCon = this._parentBlacklistDom(item, i, pos) const con = [ `
`, `
${this._createNickNameDom(item)}`, this._createLevelLink(item), this._identity(item.mid, item.assist, item.member.fans_detail), `${this._createNameplate(item.member.nameplate) + this._createUserSailing(item)}
`, this._createMsgContent(item), this._createPerfectReply(item), '
', this._createPlatformDom(item.content.plat), '', ''.concat(this._formateTime(item.ctime), ''), item?.reply_control?.location ? `${item?.reply_control?.location || ''}` : '', '', item.lottery_id ? '' : ``, item.lottery_id ? '' : ``, item.lottery_id ? '' : this._createReplyBtn(item.rcount), item.lottery_id && item.mid !== bbComment.userStatus.mid ? '' : `
    ${ this._canSetTop(item) ? `
  • ${item.isUpTop ? '取消置顶' : '设为置顶'}
  • ` : '' }${this._canBlackList(item.mid) ? '
  • 加入黑名单
  • ' : ''}${ this._canReport(item.mid) ? '
  • 举报
  • ' : '' }${ this._canDel(item.mid) && !item.isTop ? `
  • 删除
  • ` : '' }
`, this._createLotteryContent(item.content), this._createVoteContent(item.content), this._createTags(item), '
', '
', this._createSubReplyList( item.replies, item.rcount, false, item.rpid, item.folder && item.folder.has_folded, item.reply_control, ), '
', '
', '
', '
', ].join('') return item.state === bbComment.blacklistCode ? blCon : con } // 带IP属地显示的回复创建 const createSubReplyItem = function subReply(item, i) { const dom = [ `
`, this._createSubReplyUserFace(item), '
', '
', this._createNickNameDom(item), this._createLevelLink(item), this._identity(item.mid), this._createSubMsgContent(item), '
', '
', '
', '', ''.concat(this._formateTime(item.ctime), ''), item?.reply_control?.location ? `${item?.reply_control?.location || ''}` : '', '', ``, ``, '回复', `
    ${ this._canBlackList(item.mid) ? '
  • 加入黑名单
  • ' : '' }${this._canReport(item.mid) ? '
  • 举报
  • ' : ''}${ this._canDel(item.mid) ? `
  • 删除
  • ` : '' }
`, '
', '
', ].join('') return dom } const observer = new MutationObserver(mutations => { mutations.forEach(() => { // 监听旧版评论区全局变量 if (typeof unsafeWindow.bbComment !== 'undefined') { // 评论创建函数替换 bbComment = unsafeWindow.bbComment bbComment.prototype._createListCon = createListCon bbComment.prototype._createSubReplyItem = createSubReplyItem version = 1 observer.disconnect() } }) }) observer.observe(document.head, { childList: true }) const processItems = (items: CommentReplyItem[]) => { items.forEach(item => { const location = getIpLocation(item.element) if (location !== undefined) { const replyTime = item.element.querySelector('.reply-info>.reply-time') ?? item.element.querySelector('.sub-reply-info>.sub-reply-time') if (replyTime.childElementCount === 0) { // 避免在评论更新的情况下重复添加 const replyLocation = document.createElement('span') replyLocation.style.marginLeft = '5px' replyLocation.innerText = location replyTime.appendChild(replyLocation) } } }) } const entry = async () => { const { forEachCommentItem } = await import('@/components/utils/comment-apis') const addIpLocation = (comment: CommentItem) => { if (version === 2) { processItems([comment, ...comment.replies]) comment.addEventListener('repliesUpdate', replies => processItems(replies.detail)) } } forEachCommentItem({ added: addIpLocation, }) } export const component = defineComponentMetadata({ name: 'ipShow', author: { name: 'Light_Quanta', link: 'https://github.com/LightQuanta', }, displayName: '评论区IP属地显示', tags: [componentsTags.utils], entry, description: { 'zh-CN': '在评论区显示评论的IP属地信息', }, })