@@ -5,11 +5,17 @@ import { checkAndDisplayDiscs } from '../utils/check-and-display-disks';
55
66interface ResolveLogActionProps {
77 onclick : string ;
8+ link : HTMLAnchorElement ;
89 serverHost : string ;
910}
1011
11- const resolveLogAction = ( { onclick, serverHost } : ResolveLogActionProps ) : LogAction | null => {
12+ const resolveLogAction = ( { onclick, link, serverHost } : ResolveLogActionProps ) : LogAction | null => {
13+ if ( serverHost . match ( / o r p h e u s / ) && link . classList . contains ( 'view-riplog' ) ) {
14+ LOGGER . debug ( 'Orpheus' ) ;
15+ return 'viewlog' ;
16+ }
1217 if ( onclick . match ( / s h o w _ l o g s / ) ) {
18+ // TODO: Orpheus had changed the way to show logs, so this is not working anymore. Will keep it here just in case and remove in the future.
1319 if ( serverHost . match ( / o r p h e u s / ) ) {
1420 LOGGER . debug ( 'Orpheus' ) ;
1521 return 'viewlog' ;
@@ -31,6 +37,56 @@ const resolveLogAction = ({ onclick, serverHost }: ResolveLogActionProps): LogAc
3137 return null ;
3238} ;
3339
40+ const getTorrentIdFromHref = ( link : HTMLAnchorElement ) : string | null => {
41+ const href = link . getAttribute ( 'href' ) ;
42+ if ( ! href ) {
43+ return null ;
44+ }
45+
46+ const url = new URL ( href , window . location . origin ) ;
47+ const torrentId = url . searchParams . get ( 'torrentid' ) ;
48+ if ( torrentId ) {
49+ return torrentId ;
50+ }
51+
52+ const action = url . searchParams . get ( 'action' ) ;
53+ if (
54+ ( url . pathname . endsWith ( '/torrents.php' ) && action === 'download' ) ||
55+ ( url . pathname . endsWith ( '/ajax.php' ) && action === 'torrent' ) ||
56+ ( url . pathname . endsWith ( '/reportsv2.php' ) && action === 'report' )
57+ ) {
58+ return url . searchParams . get ( 'id' ) ;
59+ }
60+
61+ return null ;
62+ } ;
63+
64+ const resolveTorrentId = ( link : HTMLAnchorElement , onclick : string ) : string | null => {
65+ const inlineHandlerMatch = / ( s h o w _ l o g s | g e t _ l o g | s h o w _ l o g ) \( ' ( \d + ) / . exec ( onclick ) ;
66+ if ( inlineHandlerMatch ?. [ 2 ] ) {
67+ return inlineHandlerMatch [ 2 ] ;
68+ }
69+
70+ const dataIdContainer = link . closest < HTMLElement > ( '[data-id]' ) ;
71+ if ( dataIdContainer ?. dataset [ 'id' ] ) {
72+ return dataIdContainer . dataset [ 'id' ] ;
73+ }
74+
75+ const torrentInfo = link . closest ( 'tr' ) ?? link . parentElement ;
76+ if ( ! torrentInfo ) {
77+ return null ;
78+ }
79+
80+ for ( const torrentLink of torrentInfo . querySelectorAll < HTMLAnchorElement > ( 'a[href]' ) ) {
81+ const torrentId = getTorrentIdFromHref ( torrentLink ) ;
82+ if ( torrentId ) {
83+ return torrentId ;
84+ }
85+ }
86+
87+ return null ;
88+ } ;
89+
3490interface ProcessLogLinkProps {
3591 link : HTMLAnchorElement ;
3692 artistName : string ;
@@ -45,14 +101,13 @@ function processLogLink({ link, artistName, releaseName, serverHost }: ProcessLo
45101
46102 LOGGER . debug ( 'Log link' , link ) ;
47103 const onclick = link . getAttribute ( 'onclick' ) ?? '' ;
48- const logAction = resolveLogAction ( { onclick, serverHost } ) ;
104+ const logAction = resolveLogAction ( { onclick, link , serverHost } ) ;
49105 if ( ! logAction ) {
50106 return ;
51107 }
52108
53- const targetContainer = link . closest ( '.linkbox' ) ;
54- const torrentIdMatch = / ( s h o w _ l o g s | g e t _ l o g | s h o w _ l o g ) \( ' ( \d + ) / . exec ( onclick ) ;
55- const torrentId = torrentIdMatch ?. [ 2 ] ;
109+ const targetContainer = link . closest ( '.linkbox' ) ?? link . closest ( '[data-id]' ) ?? link . parentElement ;
110+ const torrentId = resolveTorrentId ( link , onclick ) ;
56111 if ( ! torrentId ) {
57112 return ;
58113 }
0 commit comments