Skip to content

Commit 076ea77

Browse files
committed
Support loading multiple toast scripts
This may not be necessary
1 parent 09a2538 commit 076ea77

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

simpletoast.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,22 @@
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();
@@ -61,7 +71,7 @@
6171
toast.close();
6272
});
6373
}, 1000);
64-
return el;
74+
return document.getElementById('AlertToast') || create();
6575
})();
6676
let count = 0;
6777

0 commit comments

Comments
 (0)