Skip to content

Commit

Permalink
feat: mount pop-up window element using Shadow DOM
Browse files Browse the repository at this point in the history
Signed-off-by: DingChil <xu.dingchao@gmail.com>
  • Loading branch information
DingChil committed Nov 1, 2023
1 parent c0153ae commit d2473d5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions assets/styles/csui-reset.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
box-shadow: none !important;
}

@import './solarized_light.scss';
// @import './solarized_light.scss';
}

@media (prefers-color-scheme: dark) {
Expand All @@ -51,6 +51,6 @@
background: #002b36;
color: #839496;}

@import './solarized_dark.scss';
// @import './solarized_dark.scss';
}
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@
"matches": [
"<all_urls>"
],
"resources": []
"resources": [
"manifest.json",
"Index.*.css"
]
}
]
}
Expand Down
26 changes: 24 additions & 2 deletions src/contents/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const config = {
}
async function render({createRootContainer}) {
const rootContainer = await createRootContainer()
const rootElement = await createRootContainer()
const shadowElement = rootElement.attachShadow({mode: 'open'})
const pinia = createPinia()
const app = createApp(App)
Expand All @@ -30,8 +31,10 @@ async function render({createRootContainer}) {
// init pinia data
const store = useStore()
await store.initConfig()
app.mount(shadowElement)
app.mount(rootContainer)
// inject styles
insertStylesheet(shadowElement)
}
async function getRootContainer() {
Expand All @@ -44,4 +47,23 @@ async function getRootContainer() {
$html.append($rootContainer)
return $rootContainer
}
async function insertStylesheet(parentElement) {
const config = await (await fetch(chrome.runtime.getURL('manifest.json'))).json()
if (!config.content_scripts && !config.content_scripts.length) return
const tasks = []
config.content_scripts.forEach(item => {
if (!item.matches[0] || item.matches[0] !== '<all_urls>') return
item.css.forEach(name => {
if (!/^Index\.\w+\.css$/.test(name)) return
tasks.push(fetch(chrome.runtime.getURL(name)).then(v => v.text()))
})
})
const files = await Promise.all(tasks)
files.forEach(themes => {
const childElement = document.createElement('style')
childElement.textContent = themes
parentElement.insertBefore(childElement, parentElement.firstChild)
})
}
</script>

0 comments on commit d2473d5

Please sign in to comment.