<commit_before>import { injectCSS } from 'src/search-injection/dom'

const CONTAINER_CLASS = 'memex-tooltip-notification'

export function createRootElement() {
    const container = document.createElement('div')
    container.classList.add(CONTAINER_CLASS)

    const cssFile = window['browser'].extension.getURL('/content_script.css')
    const rootElement = document.createElement('div')
    if (container.attachShadow) {
        const shadow = container.attachShadow({ mode: 'closed' })
        shadow.appendChild(rootElement)
        injectCSS(cssFile, shadow)
    } else {
        container.appendChild(rootElement)
        injectCSS(cssFile)
    }
    document.body.appendChild(container)

    return rootElement
}

export function destroyRootElement() {
    const container = document.querySelector(`.${CONTAINER_CLASS}`)
    if (container) {
        container.remove()
    }
}<commit_msg>Factor out Shadow DOM creation function<commit_after>import { injectCSS } from 'src/search-injection/dom'

const CONTAINER_CLASS = 'memex-tooltip-notification'

export function createRootElement() {
    const container = document.createElement('div')
    container.classList.add(CONTAINER_CLASS)

    const cssFile = window['browser'].extension.getURL('/content_script.css')
    const rootElement = createShadowRootIfSupported(container, cssFile)
    document.body.appendChild(container)

    return rootElement
}

export function destroyRootElement() {
    const container = document.querySelector(`.${CONTAINER_CLASS}`)
    if (container) {
        container.remove()
    }
}

export function createShadowRootIfSupported(
    container: HTMLElement,
    cssFile?: string,
) {
    const rootElement = document.createElement('div')
    if (container.attachShadow) {
        const shadow = container.attachShadow({ mode: 'closed' })
        shadow.appendChild(rootElement)
        injectCSS(cssFile, shadow)
    } else {
        container.appendChild(rootElement)
        injectCSS(cssFile)
    }

    return rootElement
}
{'fraction_non_alphanumeric': 0.10555004955401387, 'fraction_numerical': 0.0, 'mean_word_length': 3.88861985472155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1081662', 'n_tokens_mistral': 549, 'n_tokens_neox': 539, 'n_words': 108}