blob: 588e4af827d478b032ca05844f64a8a0b03fc5a8 [file] [log] [blame]
Adrià Vilanova Martínezd7951b22024-05-25 22:13:32 +02001export interface StylesheetAttributes {
2 media?: string;
3}
4
5export function injectStylesheet(
6 stylesheetName: string,
7 attributes: StylesheetAttributes = {},
8) {
9 var link = document.createElement('link');
10 link.setAttribute('rel', 'stylesheet');
11 link.setAttribute('href', stylesheetName);
12 if ('media' in attributes) {
13 link.setAttribute('media', attributes['media']);
14 }
15 document.head.appendChild(link);
16 return link;
17}
18
19export function injectStyles(css: string) {
20 injectStylesheet('data:text/css;charset=UTF-8,' + encodeURIComponent(css));
21}
22
23export function injectScript(scriptName: string, prepend = false) {
24 var script = document.createElement('script');
25 script.src = scriptName;
26 const root = document.head || document.documentElement;
27 if (prepend) {
28 root.prepend(script);
29 } else {
30 root.append(script);
31 }
32}