/
replay-and-repost@page.js
148 lines (121 loc) · 5.19 KB
/
replay-and-repost@page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import select from 'select-dom'
import { on, off } from 'delegated-events'
import triggerEvent from 'compat-trigger-event'
import { isPrivateMessagePage } from '@libs/pageDetect'
import every from '@libs/promiseEvery'
import getLoggedInUserProfilePageUrl from '@libs/getLoggedInUserProfilePageUrl'
import arrayUniquePush from '@libs/arrayUniquePush'
import neg from '@libs/neg'
import { POST_STATUS_SUCCESS_EVENT_TYPE } from '@constants'
export default context => {
const { readOptionValue, elementCollection, registerDOMEventListener } = context
elementCollection.add({
update: '#phupdate',
form: { parent: 'update', selector: 'form' },
textarea: { parent: 'update', selector: 'textarea' },
})
registerDOMEventListener('textarea', POST_STATUS_SUCCESS_EVENT_TYPE, onPostStatusSuccess)
function onPostStatusSuccess(event) {
const { textarea } = elementCollection.getAll()
const { formDataJson } = event.detail
const statusContent = formDataJson.content || formDataJson.desc
if (readOptionValue('keepFocusAfterPosting')) {
textarea.focus()
}
if (readOptionValue('keepAtNamesAfterPosting') && statusContent.startsWith('@')) {
const userNicknames = []
extractUserNicknamesFromStatusContent(statusContent, userNicknames)
textarea.value = userNicknames.join(' ') + ' '
const selectionPos = textarea.value.length
textarea.setSelectionRange(selectionPos, selectionPos)
}
}
function extractStatusId(li) {
return select('.stamp .time', li).getAttribute('ffid')
}
function extractStatusContent(li) {
// 不使用 select('.content', li).textContent.trim() 的方式
// 因为 .content 的内容可能会被修改(比如展开短链接),也可能包含被截断的链接
// 而转发的时候应该保留原始消息内容
const repostLink = select(':scope > .op > .repost', li)
const queryString = repostLink.search.substr(1) // 去掉开头的 '?'
const urlSearchParams = new URLSearchParams(queryString)
const queryMap = Object.fromEntries(urlSearchParams.entries())
const statusContent = queryMap.status
return statusContent
}
function extractUserNicknamesFromStatusDOM(li, callback) {
if (Array.isArray(callback)) {
const userNicknames = []
callback = userNickname => arrayUniquePush(userNicknames, userNickname)
}
for (const atUserElement of select.all('.content a.former', li)) {
const userUrl = atUserElement.href
const userNickname = '@' + atUserElement.textContent
if (userUrl !== getLoggedInUserProfilePageUrl()) callback(userNickname)
}
}
function extractUserNicknamesFromStatusContent(statusContent, callback) {
if (Array.isArray(callback)) {
const userNicknames = []
callback = userNickname => arrayUniquePush(userNicknames, userNickname)
}
for (const textPart of statusContent.split(/\s+/)) {
if (textPart.charAt(0) === '@') callback(textPart)
}
}
const createHandler = type => event => {
event.preventDefault()
const { form, textarea } = elementCollection.getAll()
const li = event.path.find(element => element.tagName.toLowerCase() === 'li')
const authorElement = select('.author', li)
const targetStatusId = extractStatusId(li)
const targetStatusAuthorNickname = '@' + authorElement.textContent
const targetStatusText = extractStatusContent(li)
const oldStatusContent = textarea.value.trim()
let inReplyToStatusId = '', repostStatusId = ''
let newStatusContent = ''
let selectionStartPos = -1, selectionEndPos = -1
if (type === 'reply') {
const atUserNicknames = [ targetStatusAuthorNickname ]
const oldStatusNickNames = []
extractUserNicknamesFromStatusContent(oldStatusContent, oldStatusNickNames)
extractUserNicknamesFromStatusDOM(li, nickname => {
if (!oldStatusNickNames.includes(nickname)) {
arrayUniquePush(atUserNicknames, nickname)
}
})
newStatusContent = atUserNicknames.join(' ') + ' ' + oldStatusContent
selectionStartPos = targetStatusAuthorNickname.length + 1 // 第一个@用户名及其后面的空格之后的位置
selectionEndPos = newStatusContent.length
inReplyToStatusId = targetStatusId
} else if (type === 'repost') {
newStatusContent = `${oldStatusContent}${targetStatusText}`
selectionStartPos = 0
selectionEndPos = oldStatusContent.length
repostStatusId = targetStatusId
}
textarea.value = newStatusContent
textarea.focus()
triggerEvent(textarea, 'change')
textarea.setSelectionRange(selectionStartPos, selectionEndPos)
form.elements.in_reply_to_status_id.value = inReplyToStatusId
form.elements.repost_status_id.value = repostStatusId
}
const onClickReply = createHandler('reply')
const onClickRepost = createHandler('repost')
return {
applyWhen: () => every([
neg(isPrivateMessagePage()),
elementCollection.ready('update'),
]),
onLoad() {
on('click', '#stream ol .op .reply', onClickReply)
on('click', '#stream ol .op .repost', onClickRepost)
},
onUnload() {
off('click', '#stream ol .op .reply', onClickReply)
off('click', '#stream ol .op .repost', onClickRepost)
},
}
}