File tree Expand file tree Collapse file tree 1 file changed +17
-7
lines changed
Expand file tree Collapse file tree 1 file changed +17
-7
lines changed Original file line number Diff line number Diff line change 4646
4747 const toasts = new Map ( ) ;
4848 const root = ( ( ) => {
49- const el = document . createElement ( 'div' ) ;
50- el . setAttribute ( 'id' , 'AlertToast' ) ;
51- applyCSS ( el , style . root ) ;
52-
53- const body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
54- body . insertBefore ( el , body . firstChild ) ;
49+ function create ( ) {
50+ const el = document . createElement ( 'div' ) ;
51+ el . setAttribute ( 'id' , 'AlertToast' ) ;
52+ applyCSS ( el , style . root ) ;
53+
54+ const body = document . getElementsByTagName ( 'body' ) [ 0 ] ;
55+ if ( body ) { // Depending on when the script is loaded... this might be null
56+ body . appendChild ( el ) ;
57+ } else {
58+ window . addEventListener ( 'load' , ( ) => {
59+ if ( document . getElementById ( el . id ) ) return ; // Another script may have created it already
60+ document . getElementsByTagName ( 'body' ) [ 0 ] . appendChild ( el ) ;
61+ } ) ;
62+ }
63+ return el ;
64+ }
5565
5666 setInterval ( ( ) => { // TODO: don't always run a timer
5767 const now = Date . now ( ) ;
6171 toast . close ( ) ;
6272 } ) ;
6373 } , 1000 ) ;
64- return el ;
74+ return document . getElementById ( 'AlertToast' ) || create ( ) ;
6575 } ) ( ) ;
6676 let count = 0 ;
6777
You can’t perform that action at this time.
0 commit comments