Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic shadow-dom support #226

Merged
merged 6 commits into from
Nov 26, 2024
Merged

Basic shadow-dom support #226

merged 6 commits into from
Nov 26, 2024

Conversation

cezaraugusto
Copy link
Member

@cezaraugusto cezaraugusto commented Nov 22, 2024

During development, Extension.js inject styles in the tag via <style> tag. This works great unless you want to scope the styles to avoid leaking CSS to the webpage. The common workaround is to inject the extension within a shadow DOM component.

This PR adds support to window.__EXTENSION_SHADOW_ROOT__. After defining the shadow root within the DOM, set it as a reference and the styles will be injected in there instead of the regular approach.

import ReactDOM from 'react-dom/client'
import ContentApp from './ContentApp'
import './styles.css'

setTimeout(initial, 1000)

function initial() {
  // Create a new div element and append it to the document's body
  const rootDiv = document.createElement('div')
  rootDiv.id = 'extension-root'
  document.body.appendChild(rootDiv)

  // Injecting content_scripts inside a shadow dom
  // prevents conflicts with the host page's styles.
  // This way, styles from the extension won't leak into the host page.
  const shadowRoot = rootDiv.attachShadow({mode: 'open'})

  if (process.env.EXTENSION_MODE === 'development') {
    // to use the shadow root as the root element
    // to inject styles into.
    window.__EXTENSION_SHADOW_ROOT__ = shadowRoot
  }

  const root = ReactDOM.createRoot(shadowRoot)
  root.render(
    <div className="content_script">
      <ContentApp />
    </div>
  )
}

Copy link

pkg-pr-new bot commented Nov 22, 2024

Open in Stackblitz

npm i https://pkg.pr.new/extension-js/extension.js/extension@226
npm i https://pkg.pr.new/extension-js/extension.js/extension-create@226
npm i https://pkg.pr.new/extension-js/extension.js/extension-develop@226

commit: 851a519

Copy link

codecov bot commented Nov 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

This package is used by vue-loader by default,
so it's redundant to have it in the plugin-css as well
This tells webpack to inline styles even
when running in production mode
@cezaraugusto cezaraugusto marked this pull request as ready for review November 26, 2024 00:38
@cezaraugusto cezaraugusto merged commit 6914072 into main Nov 26, 2024
15 checks passed
@cezaraugusto cezaraugusto deleted the shadow-dom branch November 26, 2024 00:39
cezaraugusto added a commit that referenced this pull request Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant