Adrià Vilanova MartÃnez | dfe71fc | 2024-05-25 22:13:32 +0200 | [diff] [blame] | 1 | import { StylesheetAttributes, injectStylesheet } from './contentScriptsUtils'; |
| 2 | |
| 3 | export default class StylesheetManager { |
| 4 | private injectedElement: HTMLElement; |
| 5 | |
| 6 | constructor( |
| 7 | /** |
| 8 | * Relative path to the stylesheet from the extension root. |
| 9 | */ |
| 10 | public stylesheet: string, |
| 11 | |
| 12 | /** |
| 13 | * Attributes to include in the injected <link> element. |
| 14 | */ |
| 15 | public attributes: StylesheetAttributes = {}, |
| 16 | ) {} |
| 17 | |
| 18 | isInjected() { |
| 19 | return this.injectedElement !== undefined; |
| 20 | } |
| 21 | |
| 22 | inject() { |
| 23 | this.injectedElement = injectStylesheet( |
| 24 | chrome.runtime.getURL(this.stylesheet), |
| 25 | this.attributes, |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | remove() { |
| 30 | this.injectedElement.remove(); |
| 31 | this.injectedElement = undefined; |
| 32 | } |
| 33 | } |