@@ -6395,27 +6395,32 @@ async function getLanguage(player, response, title, description) {
6395
6395
! window . location . hostname . includes ( "m.youtube.com" ) &&
6396
6396
player ?. getAudioTrack
6397
6397
) {
6398
- // ! Experimental ! get lang from selected audio track if availabled
6399
- const audioTracks = player . getAudioTrack ( ) ;
6400
- const trackInfo = audioTracks ?. getLanguageInfo ( ) ; // get selected track info (id === "und" if tracks are not available)
6398
+ // Experimental: Get language from selected audio track if available
6399
+ const trackInfo = player . getAudioTrack ( ) ?. getLanguageInfo ( ) ; // Get selected track info
6401
6400
if ( trackInfo ?. id !== "und" ) {
6402
6401
return normalizeLang ( trackInfo . id . split ( "." ) [ 0 ] ) ;
6403
6402
}
6404
- }
6405
6403
6406
- // TODO: If the audio tracks will work fine, transfer the receipt of captions to the audioTracks variable
6407
- // Check if there is an automatic caption track in the response
6408
- const captionTracks =
6409
- response ?. captions ?. playerCaptionsTracklistRenderer ?. captionTracks ;
6410
- if ( captionTracks ?. length ) {
6411
- const autoCaption = captionTracks . find ( ( caption ) => caption . kind === "asr" ) ;
6412
- if ( autoCaption && autoCaption . languageCode ) {
6404
+ // Get available audio tracks
6405
+ const audioTracks = player . getAvailableAudioTracks ( ) ;
6406
+ // Find automatic caption track in audio tracks
6407
+ const autoCaption = audioTracks ?. find (
6408
+ track => track . kind === "asr" && track . languageCode
6409
+ ) ;
6410
+ if ( autoCaption ) {
6413
6411
return normalizeLang ( autoCaption . languageCode ) ;
6414
6412
}
6415
6413
}
6416
6414
6417
6415
// If there is no caption track, use detect to get the language code from the description
6416
+ const autoCaption = response ?. captions ?. playerCaptionsTracklistRenderer ?. captionTracks ?. find (
6417
+ ( caption ) => caption . kind === "asr" && caption . languageCode
6418
+ ) ;
6419
+ if ( autoCaption ) {
6420
+ return normalizeLang ( autoCaption . languageCode ) ;
6421
+ }
6418
6422
6423
+ // Use the description text to detect the language if no captions are available
6419
6424
const text = cleanText ( title , description ) ;
6420
6425
6421
6426
debug . log ( `Detecting language text: ${ text } ` ) ;
@@ -6948,24 +6953,21 @@ class SubtitlesWidget {
6948
6953
6949
6954
bindEvents ( ) {
6950
6955
const { signal } = this . abortController ;
6951
-
6952
- this . onMouseDownBound = ( e ) => this . onMouseDown ( e ) ;
6953
- this . onMouseUpBound = ( ) => this . onMouseUp ( ) ;
6954
- this . onMouseMoveBound = ( e ) => this . onMouseMove ( e ) ;
6956
+ this . onPointerDownBound = ( e ) => this . onPointerDown ( e ) ;
6957
+ this . onPointerUpBound = ( ) => this . onPointerUp ( ) ;
6958
+ this . onPointerMoveBound = ( e ) => this . onPointerMove ( e ) ;
6955
6959
this . onTimeUpdateBound = this . debounce ( ( ) => this . update ( ) , 100 ) ;
6956
-
6957
- document . addEventListener ( "mousedown" , this . onMouseDownBound , { signal } ) ;
6958
- document . addEventListener ( "mouseup" , this . onMouseUpBound , { signal } ) ;
6959
- document . addEventListener ( "mousemove" , this . onMouseMoveBound , { signal } ) ;
6960
- this . video ?. addEventListener ( "timeupdate" , this . onTimeUpdateBound , {
6961
- signal,
6962
- } ) ;
6963
-
6960
+
6961
+ document . addEventListener ( "pointerdown" , this . onPointerDownBound , { signal } ) ;
6962
+ document . addEventListener ( "pointerup" , this . onPointerUpBound , { signal } ) ;
6963
+ document . addEventListener ( "pointermove" , this . onPointerMoveBound , { signal } ) ;
6964
+
6965
+ this . video ?. addEventListener ( "timeupdate" , this . onTimeUpdateBound , { signal } ) ;
6964
6966
this . resizeObserver = new ResizeObserver ( ( ) => this . onResize ( ) ) ;
6965
6967
this . resizeObserver . observe ( this . container ) ;
6966
6968
}
6967
6969
6968
- onMouseDown ( e ) {
6970
+ onPointerDown ( e ) {
6969
6971
if ( ! this . subtitlesContainer . contains ( e . target ) ) return ;
6970
6972
6971
6973
const rect = this . subtitlesContainer . getBoundingClientRect ( ) ;
@@ -6983,12 +6985,11 @@ class SubtitlesWidget {
6983
6985
} ,
6984
6986
} ;
6985
6987
}
6986
-
6987
- onMouseUp ( ) {
6988
+ onPointerUp ( ) {
6988
6989
this . dragging . active = false ;
6989
6990
}
6990
6991
6991
- onMouseMove ( e ) {
6992
+ onPointerMove ( e ) {
6992
6993
if ( ! this . dragging . active ) return ;
6993
6994
6994
6995
e . preventDefault ( ) ;
@@ -10726,50 +10727,69 @@ class VideoHandler {
10726
10727
this . votButton . translateButton . addEventListener ( "click" , async ( ) => {
10727
10728
await this . handleTranslationBtnClick ( ) ;
10728
10729
} ) ;
10729
-
10730
- this . votButton . pipButton . addEventListener ( "click" , ( ) => {
10731
- ( async ( ) => {
10732
- if ( this . video !== document . pictureInPictureElement ) {
10733
- await this . video . requestPictureInPicture ( ) ;
10734
- } else {
10735
- await document . exitPictureInPicture ( ) ;
10736
- }
10737
- } ) ( ) ;
10730
+
10731
+ this . votButton . pipButton . addEventListener ( "click" , async ( ) => {
10732
+ const isPiPActive = this . video === document . pictureInPictureElement ;
10733
+ await ( isPiPActive ? document . exitPictureInPicture ( ) : this . video . requestPictureInPicture ( ) ) ;
10738
10734
} ) ;
10739
-
10740
10735
this . votButton . menuButton . addEventListener ( "click" , ( ) => {
10741
10736
this . votMenu . container . hidden = ! this . votMenu . container . hidden ;
10742
10737
} ) ;
10743
-
10744
- this . votButton . container . addEventListener ( "mousedown" , ( ) => {
10738
+
10739
+ // Position update logic
10740
+ const updateButtonPosition = async ( percentX ) => {
10741
+ const isBigContainer = this . container . clientWidth > 550 ;
10742
+ const position = isBigContainer
10743
+ ? ( percentX <= 44 ? "left" : percentX >= 66 ? "right" : "default" )
10744
+ : "default" ;
10745
+
10746
+ this . data . buttonPos = position ;
10747
+ this . votButton . container . dataset . direction = position === "default" ? "row" : "column" ;
10748
+ this . votButton . container . dataset . position = position ;
10749
+ this . votMenu . container . dataset . position = position ;
10750
+
10751
+ if ( isBigContainer ) {
10752
+ await votStorage . set ( "buttonPos" , position ) ;
10753
+ }
10754
+ } ;
10755
+
10756
+ // Drag event handler
10757
+ const handleDragMove = async ( clientX , rect = this . container . getBoundingClientRect ( ) ) => {
10758
+ if ( ! this . dragging ) return ;
10759
+
10760
+ const x = rect ? clientX - rect . left : clientX ;
10761
+ const percentX = ( x / ( rect ? rect . width : this . container . clientWidth ) ) * 100 ;
10762
+ await updateButtonPosition ( percentX ) ;
10763
+ } ;
10764
+
10765
+ // Mouse/pointer events
10766
+ this . votButton . container . addEventListener ( "pointerdown" , ( e ) => {
10745
10767
this . dragging = true ;
10768
+ e . preventDefault ( ) ;
10746
10769
} ) ;
10747
-
10748
- this . container . addEventListener ( "mouseup" , ( ) => {
10749
- this . dragging = false ;
10750
- } ) ;
10751
-
10752
- this . container . addEventListener ( "mousemove" , async ( e ) => {
10753
- if ( this . dragging ) {
10754
- e . preventDefault ( ) ;
10755
-
10756
- const percentX = ( e . clientX / this . container . clientWidth ) * 100 ;
10757
- // const percentY = (e.clientY / this.video.clientHeight) * 100;
10758
- const isBigContainer = this . container . clientWidth > 550 ;
10759
- const isLeft = percentX <= 44 ;
10760
- const isRightSide = percentX >= 66 ? "right" : "default" ;
10761
- const side = isLeft ? "left" : isRightSide ;
10762
-
10763
- this . data . buttonPos = isBigContainer ? side : "default" ;
10764
- this . votButton . container . dataset . direction =
10765
- this . data . buttonPos === "default" ? "row" : "column" ;
10766
- this . votButton . container . dataset . position = this . data . buttonPos ;
10767
- this . votMenu . container . dataset . position = this . data . buttonPos ;
10768
- if ( isBigContainer ) {
10769
- await votStorage . set ( "buttonPos" , this . data . buttonPos ) ;
10770
- }
10771
- }
10770
+
10771
+ this . container . addEventListener ( "pointerup" , ( ) => this . dragging = false ) ;
10772
+ this . container . addEventListener ( "pointermove" , ( e ) => {
10773
+ e . preventDefault ( ) ;
10774
+ handleDragMove ( e . clientX ) ;
10772
10775
} ) ;
10776
+
10777
+ // Touch events
10778
+ this . votButton . container . addEventListener ( "touchstart" , ( e ) => {
10779
+ this . dragging = true ;
10780
+ e . preventDefault ( ) ;
10781
+ } , { passive : false } ) ;
10782
+
10783
+ this . container . addEventListener ( "touchend" , ( ) => this . dragging = false ) ;
10784
+ this . container . addEventListener ( "touchmove" , ( e ) => {
10785
+ e . preventDefault ( ) ;
10786
+ handleDragMove ( e . touches [ 0 ] . clientX , this . container . getBoundingClientRect ( ) ) ;
10787
+ } , { passive : false } ) ;
10788
+
10789
+ // Cancel events
10790
+ for ( const event of [ "pointercancel" , "touchcancel" ] ) {
10791
+ document . addEventListener ( event , ( ) => this . dragging = false ) ;
10792
+ }
10773
10793
}
10774
10794
10775
10795
// VOT Menu
@@ -11460,18 +11480,18 @@ class VideoHandler {
11460
11480
if ( eventContainer )
11461
11481
addExtraEventListeners (
11462
11482
eventContainer ,
11463
- [ "mousemove " , "mouseout " ] ,
11483
+ [ "pointermove " , "pointerout " ] ,
11464
11484
this . resetTimer ,
11465
11485
) ;
11466
11486
11467
11487
addExtraEventListener (
11468
11488
this . votButton . container ,
11469
- "mousemove " ,
11489
+ "pointermove " ,
11470
11490
this . changeOpacityOnEvent ,
11471
11491
) ;
11472
11492
addExtraEventListener (
11473
11493
this . votMenu . container ,
11474
- "mousemove " ,
11494
+ "pointermove " ,
11475
11495
this . changeOpacityOnEvent ,
11476
11496
) ;
11477
11497
// remove listener on xvideos to fix #866
@@ -11484,10 +11504,10 @@ class VideoHandler {
11484
11504
}
11485
11505
11486
11506
// fix youtube hold to fast
11487
- addExtraEventListener ( this . votButton . container , "mousedown " , ( e ) => {
11507
+ addExtraEventListener ( this . votButton . container , "pointerdown " , ( e ) => {
11488
11508
e . stopImmediatePropagation ( ) ;
11489
11509
} ) ;
11490
- addExtraEventListener ( this . votMenu . container , "mousedown " , ( e ) => {
11510
+ addExtraEventListener ( this . votMenu . container , "pointerdown " , ( e ) => {
11491
11511
e . stopImmediatePropagation ( ) ;
11492
11512
} ) ;
11493
11513
0 commit comments