1
1
import html2canvas from 'html2canvas'
2
+ import sentinel from 'sentinel-js'
2
3
import { chatGPTAvatarSVG , fileCode , iconCamera , iconCopy } from './icons'
3
4
import { copyToClipboard , downloadFile , downloadUrl , escapeHtml , getBase64FromImg , onloadSafe , sleep , timestamp } from './utils'
4
5
import templateHtml from './template.html?raw'
@@ -24,18 +25,13 @@ main()
24
25
25
26
function main ( ) {
26
27
onloadSafe ( ( ) => {
27
- const firstMenuItem = document . querySelector ( 'nav > a' )
28
- const container = firstMenuItem ?. parentElement
29
- if ( ! firstMenuItem || ! container ) {
30
- console . error ( 'Failed to locate the menu container element.' )
31
- // alert('Failed to locate the menu container element. Please report this issue to the developer.')
28
+ const nav = document . querySelector ( 'nav' )
29
+ if ( ! nav ) {
30
+ console . error ( 'Failed to locate the nav element. Please report this issue to the developer.' )
31
+ // alert('Failed to locate the nav element. Please report this issue to the developer.')
32
32
return
33
33
}
34
34
35
- const divider = document . createElement ( 'div' )
36
- divider . className = 'border-b border-white/20'
37
- divider . style . marginBottom = '0.5rem'
38
-
39
35
const copyHtml = `${ iconCopy } Copy Text`
40
36
const copiedHtml = `${ iconCopy } Copied`
41
37
const onCopyText = ( e : MouseEvent ) => {
@@ -52,16 +48,52 @@ function main() {
52
48
} , 3000 )
53
49
}
54
50
51
+ const divider = createDivider ( )
55
52
const textExport = createMenuItem ( iconCopy , 'Copy Text' , onCopyText )
56
53
const pngExport = createMenuItem ( iconCamera , 'Screenshot' , exportToPng )
57
54
const htmlExport = createMenuItem ( fileCode , 'Export WebPage' , exportToHtml )
58
- firstMenuItem . after ( divider , textExport , pngExport , htmlExport )
55
+ const container = createMenuContainer ( )
56
+ container . append ( textExport , pngExport , htmlExport , divider )
59
57
} )
60
58
}
61
59
60
+ function createMenuContainer ( ) {
61
+ const container = document . createElement ( 'div' )
62
+ container . id = 'exporter-menu'
63
+ container . className = 'pt-1'
64
+
65
+ const chatList = document . querySelector ( 'nav > div' )
66
+ if ( chatList ) {
67
+ chatList . after ( container )
68
+ sentinel . on ( 'nav > div.overflow-y-auto' , ( el ) => {
69
+ const nav = document . querySelector ( 'nav' ) !
70
+ if ( container . parentElement !== nav ) {
71
+ el . after ( container )
72
+ }
73
+ } )
74
+ }
75
+ else {
76
+ const nav = document . querySelector ( 'nav' ) !
77
+ nav . append ( container )
78
+ sentinel . on ( 'nav' , ( el ) => {
79
+ if ( container . parentElement !== nav ) {
80
+ el . append ( container )
81
+ }
82
+ } )
83
+ }
84
+
85
+ return container
86
+ }
87
+
88
+ function createDivider ( ) {
89
+ const divider = document . createElement ( 'div' )
90
+ divider . className = 'border-b border-white/20'
91
+ return divider
92
+ }
93
+
62
94
function createMenuItem ( icon : string , title : string , onClick : ( e : MouseEvent ) => void ) {
63
- const firstMenuItem = document . querySelector ( 'nav > a') !
64
- const menuItem = firstMenuItem . cloneNode ( true ) as HTMLAnchorElement
95
+ const menuItem = document . createElement ( ' a')
96
+ menuItem . className = 'flex py-3 px-3 items-center gap-3 rounded-md hover:bg-gray-500/10 transition-colors duration-200 text-white cursor-pointer text-sm mb-2 flex-shrink-0 border border-white/20'
65
97
menuItem . removeAttribute ( 'href' )
66
98
menuItem . innerHTML = `${ icon } ${ title } `
67
99
menuItem . addEventListener ( 'click' , onClick )
0 commit comments