-
Notifications
You must be signed in to change notification settings - Fork 8
/
tipping.ts
298 lines (258 loc) · 8.71 KB
/
tipping.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
import { getPort } from '../common/messaging'
import { MediaMetaData } from '../common/types'
import * as commonUtils from '../common/utils'
import * as styles from '../common/styles'
import * as api from './api'
import * as types from './types'
import * as utils from './utils'
let timeout: any = null
let newTwitter = true
const getMediaMetaData = (tweet: Element, tweetId: string) => {
if (!tweet || !tweetId) {
return Promise.reject(new Error('Invalid parameters'))
}
return api.getTweetDetails(tweetId)
.then((details: any) => {
return {
user: {
id: details.user.id_str,
screenName: details.user.screen_name,
fullName: details.user.name,
favIconUrl: details.user.profile_image_url_https
.replace('_normal', '')
},
post: {
id: tweetId,
timestamp: details.created_at,
text: details.text
}
}
})
.catch((error: any) => {
console.error(`Failed to fetch tweet details for ${tweetId}: ${error.message}`)
return Promise.reject(error)
})
}
const getMediaMetaDataForOldTwitter = (tweet: Element, tweetId: string) => {
if (!tweet) {
return null
}
const tweetTextElements = tweet.getElementsByClassName('tweet-text')
if (!tweetTextElements || tweetTextElements.length === 0) {
return null
}
const tweetText = tweetTextElements[0] as HTMLElement
const tweetTimestampElements = tweet.getElementsByClassName('js-short-timestamp')
if (!tweetTimestampElements || tweetTimestampElements.length === 0) {
return null
}
const tweetTimestamp = tweetTimestampElements[0].getAttribute('data-time') || ''
return {
user: {
id: tweet.getAttribute('data-user-id') || '',
screenName: tweet.getAttribute('data-screen-name') || '',
fullName: tweet.getAttribute('data-name') || '',
favIconUrl: ''
},
post: {
id: tweetId,
timestamp: tweetTimestamp,
text: tweetText.innerText || ''
}
}
}
const onTipActionKey = (e: KeyboardEvent) => {
if (e.key !== 'Enter' && e.code !== 'Space') {
return
}
const activeItem = e.target as HTMLElement
if (!activeItem) {
return
}
const shadowRoot = activeItem.shadowRoot
if (!shadowRoot) {
return
}
const tipButton: HTMLElement | null = shadowRoot.querySelector('.js-actionButton')
if (tipButton) {
tipButton.click()
}
}
const createTipAction = (
tweet: Element,
tweetId: string,
hasUserActions: boolean
) => {
// Create the tip action
const tipAction = document.createElement('div')
tipAction.className = 'ProfileTweet-action js-tooltip action-brave-tip'
tipAction.style.display = 'inline-flex'
tipAction.style.textAlign = hasUserActions ? 'right' : 'start'
tipAction.setAttribute('role', 'button')
tipAction.setAttribute('tabindex', '0')
tipAction.addEventListener('keydown', onTipActionKey)
// Create the tip button
const tipButton = document.createElement('button')
tipButton.className =
'ProfileTweet-actionButton u-textUserColorHover js-actionButton'
tipButton.style.background = 'transparent'
tipButton.style.border = '0'
tipButton.style.color = '#657786'
tipButton.style.cursor = 'pointer'
tipButton.style.display = 'inline-block'
tipButton.style.fontSize = '16px'
tipButton.style.lineHeight = '1'
tipButton.style.outline = '0'
tipButton.style.paddingBottom = '2px'
tipButton.style.position = 'relative'
tipButton.type = 'button'
tipButton.onclick = function (event) {
if (newTwitter) {
getMediaMetaData(tweet, tweetId)
.then((mediaMetaData: MediaMetaData) => {
if (mediaMetaData) {
tipUser(mediaMetaData)
}
})
.catch((error: any) => {
console.error(`Failed to fetch tweet metadata for ${tweet}:`, error)
})
} else {
const mediaMetaData = getMediaMetaDataForOldTwitter(tweet, tweetId)
if (mediaMetaData) {
tipUser(mediaMetaData)
}
}
event.stopPropagation()
}
// Create the tip icon container
const tipIconContainer = document.createElement('div')
tipIconContainer.className = 'IconContainer js-tooltip'
tipIconContainer.style.display = 'inline-block'
tipIconContainer.style.lineHeight = '0'
tipIconContainer.style.position = 'relative'
tipIconContainer.style.verticalAlign = 'middle'
tipButton.appendChild(tipIconContainer)
// Create the tip icon
const tipIcon = document.createElement('span')
tipIcon.className = 'Icon Icon--medium'
tipIcon.style.background = 'transparent'
tipIcon.style.content = styles.getTippingIconDataURL()
tipIcon.style.display = 'inline-block'
tipIcon.style.fontSize = '18px'
tipIcon.style.fontStyle = 'normal'
tipIcon.style.height = '16px'
tipIcon.style.marginTop = '2px'
tipIcon.style.position = 'relative'
tipIcon.style.verticalAlign = 'baseline'
tipIcon.style.width = '16px'
tipIconContainer.appendChild(tipIcon)
// Create the tip action count (typically used to present a counter
// associated with the action, but we'll use it to display a static
// action label)
const tipActionCount = document.createElement('span')
tipActionCount.className = 'ProfileTweet-actionCount'
tipActionCount.style.color = '#657786'
tipActionCount.style.display = 'inline-block'
tipActionCount.style.fontSize = '12px'
tipActionCount.style.fontWeight = 'bold'
tipActionCount.style.lineHeight = '1'
tipActionCount.style.marginLeft = '6px'
tipActionCount.style.position = 'relative'
tipActionCount.style.verticalAlign = 'text-bottom'
tipButton.appendChild(tipActionCount)
// Create the tip action count presentation
const tipActionCountPresentation = document.createElement('span')
tipActionCountPresentation.className =
'ProfileTweet-actionCountForPresentation'
tipActionCountPresentation.textContent =
chrome.i18n.getMessage('twitterTipsIconLabel')
tipActionCount.appendChild(tipActionCountPresentation)
// Create the shadow DOM root that hosts our injected DOM elements
const shadowRoot = tipAction.attachShadow({ mode: 'open' })
shadowRoot.appendChild(tipButton)
// Create style element for hover color
const style = document.createElement('style')
const css = '.ProfileTweet-actionButton :hover { color: #6781db }'
style.appendChild(document.createTextNode(css))
shadowRoot.appendChild(style)
return tipAction
}
const tipUser = (mediaMetaData: MediaMetaData) => {
if (!mediaMetaData) {
return
}
const profileUrl =
utils.buildProfileUrl(mediaMetaData.user.screenName, mediaMetaData.user.id)
const publisherKey =
commonUtils.buildPublisherKey(types.mediaType, mediaMetaData.user.id)
const publisherName = mediaMetaData.user.screenName
const publisherScreenName = mediaMetaData.user.screenName
const port = getPort()
if (!port) {
return
}
port.postMessage({
type: 'TipUser',
mediaType: types.mediaType,
data: {
url: profileUrl,
publisherKey,
publisherName,
publisherScreenName,
favIconUrl: mediaMetaData.user.favIconUrl,
postId: mediaMetaData.post.id,
postTimestamp: mediaMetaData.post.timestamp,
postText: mediaMetaData.post.text
}
})
}
export const configure = () => {
clearTimeout(timeout)
// Reset page state since first run of this function may have
// been pre-content
newTwitter = true
let tweets = document.querySelectorAll('[role="article"]')
if (tweets.length === 0) {
tweets = document.querySelectorAll('.tweet')
newTwitter = false
}
for (let i = 0; i < tweets.length; ++i) {
const tweetId = utils.getTweetId(tweets[i], newTwitter)
if (!tweetId) {
continue
}
let actions
if (newTwitter) {
const groups = tweets[i].querySelectorAll('[role="group"]')
for (let group of groups) {
const buttons = group.querySelectorAll('[role="button"]')
if (!buttons || buttons.length < 3) {
continue
} else {
actions = group
break
}
}
if (!actions && groups.length > 0) {
actions = groups[groups.length - 1]
}
} else {
actions = tweets[i].querySelector('.js-actions')
}
if (!actions) {
continue
}
const braveTipActions = actions.getElementsByClassName('action-brave-tip')
if (braveTipActions.length === 0) {
const numActions = actions.querySelectorAll(':scope > div').length || 0
const hasUserActions = numActions > 3
const tipAction = createTipAction(tweets[i], tweetId, hasUserActions)
actions.appendChild(tipAction)
}
}
timeout = setTimeout(configure, 3000)
}