Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 1 | export function injectStylesheet(stylesheetName, attributes = {}) { |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 2 | var link = document.createElement('link'); |
| 3 | link.setAttribute('rel', 'stylesheet'); |
| 4 | link.setAttribute('href', stylesheetName); |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 5 | if ('media' in attributes) link.setAttribute('media', attributes['media']); |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 6 | document.head.appendChild(link); |
| 7 | } |
| 8 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 9 | export function injectStyles(css) { |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 10 | injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css)); |
| 11 | } |
| 12 | |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 13 | export function injectScript(scriptName, prepend = false) { |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 14 | var script = document.createElement('script'); |
| 15 | script.src = scriptName; |
Adrià Vilanova Martínez | 102d54b | 2022-12-18 11:12:11 +0100 | [diff] [blame] | 16 | const root = (document.head || document.documentElement); |
| 17 | if (prepend) |
| 18 | root.prepend(script); |
| 19 | else |
| 20 | root.append(script); |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 21 | } |