@@ -124,7 +124,7 @@ async function handleNewNode(
124124 ) ;
125125 if ( linkPreview ) {
126126 anchor = linkPreview . anchor ;
127- container ? .remove ( ) ;
127+ container && container . remove ( ) ;
128128 container = linkPreview . card . parentElement as HTMLElement ;
129129 } else {
130130 if ( container ) {
@@ -137,7 +137,7 @@ async function handleNewNode(
137137 }
138138 }
139139
140- if ( ! anchor ) return ;
140+ if ( ! anchor || ! container ) return ;
141141
142142 const shortenedUrl = anchor . href ;
143143 const actionUrl = await resolveTwitterShortenedUrl ( shortenedUrl ) ;
@@ -188,7 +188,7 @@ async function handleNewNode(
188188 return ;
189189 }
190190
191- addMargin ( container ) ? .replaceChildren (
191+ addMargin ( container ) . replaceChildren (
192192 createAction ( {
193193 originalUrl : actionUrl ,
194194 action,
@@ -259,37 +259,39 @@ function findElementByTestId(element: Element, testId: string) {
259259
260260function findContainerInTweet ( element : Element , searchUp ?: boolean ) {
261261 const message = searchUp
262- ? element . closest ( `[data-testid="tweet"]` ) ??
263- element . closest ( `[data-testid="messageEntry"]` )
264- : findElementByTestId ( element , 'tweet' ) ??
265- findElementByTestId ( element , 'messageEntry' ) ;
262+ ? ( element . closest ( `[data-testid="tweet"]` ) ??
263+ element . closest ( `[data-testid="messageEntry"]` ) )
264+ : ( findElementByTestId ( element , 'tweet' ) ??
265+ findElementByTestId ( element , 'messageEntry' ) ) ;
266266
267267 if ( message ) {
268268 return message . querySelector ( '.dialect-wrapper' ) as HTMLElement ;
269269 }
270+ return null ;
270271}
271272
272273function findLinkPreview ( element : Element ) {
273274 const card = findElementByTestId ( element , 'card.wrapper' ) ;
274- if ( card ) {
275- const linkPreview = card . children [ 0 ] ;
276- if ( linkPreview ) {
277- const anchor = linkPreview . children [ 0 ] as HTMLAnchorElement ;
278- return { anchor, card } ;
279- }
275+ if ( ! card ) {
276+ return null ;
280277 }
278+
279+ const anchor = card . children [ 0 ] ?. children [ 0 ] as HTMLAnchorElement ;
280+
281+ return anchor ? { anchor, card } : null ;
281282}
282283function findLastLinkInText ( element : Element ) {
283284 const tweetText = findElementByTestId ( element , 'tweetText' ) ;
284285 if ( ! tweetText ) {
285- return ;
286+ return null ;
286287 }
287288
288289 const links = tweetText . getElementsByTagName ( 'a' ) ;
289290 if ( links . length > 0 ) {
290291 const anchor = links [ links . length - 1 ] as HTMLAnchorElement ;
291292 return { anchor, tweetText } ;
292293 }
294+ return null ;
293295}
294296
295297function getContainerForLink ( tweetText : Element ) {
@@ -305,7 +307,7 @@ function getContainerForLink(tweetText: Element) {
305307 return root ;
306308}
307309
308- function addMargin ( element ? : HTMLElement ) {
310+ function addMargin ( element : HTMLElement ) {
309311 if ( element && element . classList . contains ( 'dialect-wrapper' ) ) {
310312 element . style . marginTop = '12px' ;
311313 if ( element . classList . contains ( 'dialect-dm' ) ) {
0 commit comments