@@ -44,6 +44,43 @@ export function renderChatView(
4444 shareButton . title = 'Share current video' ;
4545 shareButton . appendChild ( createShareIcon ( ) ) ;
4646
47+ const shareMenu = document . createElement ( 'div' ) ;
48+ shareMenu . className = 'yt-dm-context-menu' ;
49+
50+ const shareWithTs = document . createElement ( 'div' ) ;
51+ shareWithTs . className = 'yt-dm-context-menu-item' ;
52+ shareWithTs . textContent = 'Share with Timestamp' ;
53+ shareWithTs . addEventListener ( 'click' , async e => {
54+ e . stopPropagation ( ) ;
55+ shareMenu . classList . remove ( 'visible' ) ;
56+ await handleShareVideo ( true ) ;
57+ } ) ;
58+ shareMenu . appendChild ( shareWithTs ) ;
59+
60+ const shareWrapper = document . createElement ( 'div' ) ;
61+ shareWrapper . className = 'yt-dm-header-controls' ;
62+ shareWrapper . append ( shareButton ) ;
63+ shareButton . appendChild ( shareMenu ) ;
64+ footerContainer . appendChild ( shareWrapper ) ;
65+
66+ const shareClickListener = async ( e : MouseEvent ) => {
67+ e . preventDefault ( ) ;
68+ e . stopPropagation ( ) ;
69+ shareMenu . classList . toggle ( 'visible' ) ;
70+
71+ setTimeout ( ( ) => {
72+ const closeShareMenu = ( evt : MouseEvent ) => {
73+ if ( ! shareMenu . contains ( evt . target as Node ) ) {
74+ shareMenu . classList . remove ( 'visible' ) ;
75+ document . removeEventListener ( 'click' , closeShareMenu ) ;
76+ }
77+ } ;
78+ document . addEventListener ( 'click' , closeShareMenu ) ;
79+ } , 0 ) ;
80+ } ;
81+
82+ shareButton . addEventListener ( 'contextmenu' , shareClickListener ) ;
83+
4784 const videoId = parseVideoIdFromUrl ( window . location . href ) ;
4885 if ( ! videoId ) {
4986 footerContainer . style . paddingRight = '16px' ;
@@ -59,7 +96,7 @@ export function renderChatView(
5996 footerContainer . appendChild ( inputWrapper ) ;
6097 footerContainer . appendChild ( shareButton ) ;
6198
62- const handleShareVideo = async ( ) => {
99+ const handleShareVideo = async ( includeTimestamp ?: boolean | Event ) => {
63100 if ( ! videoId ) {
64101 alert ( "No video found on this page to share." ) ;
65102 return ;
@@ -70,8 +107,11 @@ export function renderChatView(
70107
71108 shareButton . disabled = true ;
72109 try {
73- const videoData = await fetchYouTubeVideoDetails ( videoId ) ;
74-
110+ const timestamp = parseInt ( ( document . getElementById ( "movie_player" ) as any ) ?. getCurrentTime ( ) . toFixed ( 0 ) ) ;
111+ const videoData = ( includeTimestamp === true && timestamp ) ?? false ?
112+ await fetchYouTubeVideoDetails ( videoId , timestamp ) :
113+ await fetchYouTubeVideoDetails ( videoId ) ;
114+
75115 const optimisticMessage : Message = {
76116 id : `optimistic_${ Date . now ( ) } ` ,
77117 from : myUid ,
@@ -127,7 +167,7 @@ export function renderChatView(
127167 id : `optimistic_${ Date . now ( ) } ` ,
128168 from : myUid ,
129169 text : textToSend ,
130- timestamp : Timestamp . now ( )
170+ timestamp : Timestamp . now ( )
131171 } ;
132172
133173 messages = [ ...messages , optimisticMessage ] ;
@@ -173,7 +213,7 @@ export function renderChatView(
173213
174214 allSeparators . forEach ( separator => {
175215 const currentDateString = separator . getAttribute ( 'data-date-string' ) ;
176-
216+
177217 if ( currentDateString === lastDateString ) {
178218 separator . remove ( ) ;
179219 } else {
@@ -227,7 +267,7 @@ export function renderChatView(
227267 newMessagesListener = listenToNewMessages ( chatId , latestMessage . id , async ( newMsgs ) => {
228268 const myUid = auth . currentUser ?. uid ;
229269 const trulyNewMsgs = newMsgs . filter ( newMsg => newMsg . from !== myUid ) ;
230-
270+
231271 if ( trulyNewMsgs . length > 0 ) {
232272 const isAtBottom = listContainer . scrollHeight - listContainer . scrollTop - listContainer . clientHeight < 50 ;
233273 messages = [ ...messages , ...trulyNewMsgs ] ;
@@ -247,5 +287,7 @@ export function renderChatView(
247287 inputElement . removeEventListener ( 'input' , handleInput ) ;
248288 inputElement . removeEventListener ( 'keydown' , onKeydown ) ;
249289 shareButton . removeEventListener ( 'click' , handleShareVideo ) ;
290+ shareButton . removeEventListener ( 'click' , shareClickListener ) ;
291+ shareButton . removeEventListener ( 'contextmenu' , shareClickListener ) ;
250292 } ;
251293}
0 commit comments