diff options
Diffstat (limited to 'src/shared/script-injector.ts')
| -rw-r--r-- | src/shared/script-injector.ts | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/src/shared/script-injector.ts b/src/shared/script-injector.ts index 697c8d6..3422bab 100644 --- a/src/shared/script-injector.ts +++ b/src/shared/script-injector.ts @@ -1,12 +1,19 @@ -type DataType = { - [key: string]: string -}; +type DataType = Record<string, string>; -export function injectScriptHead(src: string, data: DataType = {}) { - const s = document.createElement("script") as HTMLScriptElement; +/** + * Injects a `<script>` element into the document head (or `<html>`) with + * the given `src` pointing to an extension resource. Optional key/value + * data is attached as `data-*` attributes so the injected script can + * read them via `document.currentScript`. + * + * @param src - The extension-relative script path. + * @param data - Optional key/value pairs to set as `data-*` attributes. + */ +export function injectScriptHead(src: string, data: DataType = {}): void { + const s = document.createElement("script"); s.src = chrome.runtime.getURL(src); for (const i in data) { s.setAttribute(`data-${i}`, data[i]); } - (document.head || document.documentElement).append(s); + document.head.append(s); }; |
