blob: e5fc03dbe623179a8e60058e2596ff5a4b953dd9 [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) {
Adrià Vilanova Martíneze7f9be82024-05-31 22:37:04 +02009 const link = document.createElement('link');
Adrià Vilanova Martínezd7951b22024-05-25 22:13:32 +020010 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) {
Adrià Vilanova Martíneze7f9be82024-05-31 22:37:04 +020024 const script = document.createElement('script');
Adrià Vilanova Martínezd7951b22024-05-25 22:13:32 +020025 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}