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

关于content样式隔离 #18

Open
panghuangdehaozi opened this issue Apr 20, 2023 · 10 comments
Open

关于content样式隔离 #18

panghuangdehaozi opened this issue Apr 20, 2023 · 10 comments

Comments

@panghuangdehaozi
Copy link

您好 再content_scripts中页面的样式是和谷歌插件公用的,这样会导致某些页面会出现偏移情况,请问如何能做的样式隔离?

@Jervis2049
Copy link
Owner

你好。没明白你说的意思,可以提供一个简单的demo吗。

@panghuangdehaozi
Copy link
Author

我在项目中使用了 tailwindcss 这样会导致有些网站页面出现错乱,我看别的插件时使用shadow DOM来做样式隔离的,但是目前我不知道怎么注入样式,现在的样式是全局生效的,所以会导致网页布局出现错乱;
https://gitee.com/yaoyaolei/plugin-demo.git 这是一个demo
如果有解决办法麻烦请告诉我一下
目前我取消使用tailwindcss 看起来不会影响页面
1682044898896
比如这个网站https://www.antdv.com/components/overview 我的demo就会影响它的布局

@Jervis2049
Copy link
Owner

初步看了一下,明白你意思了,我之后有空再看看怎么处理这个问题。

@leacoleaco
Copy link

我也遇到了这个问题...

@Jervis2049
Copy link
Owner

Jervis2049 commented May 6, 2023

我在项目中使用了 tailwindcss 这样会导致有些网站页面出现错乱,我看别的插件时使用shadow DOM来做样式隔离的,但是目前我不知道怎么注入样式,现在的样式是全局生效的,所以会导致网页布局出现错乱; https://gitee.com/yaoyaolei/plugin-demo.git 这是一个demo 如果有解决办法麻烦请告诉我一下 目前我取消使用tailwindcss 看起来不会影响页面 1682044898896 比如这个网站https://www.antdv.com/components/overview 我的demo就会影响它的布局

在你这个例子中,可以尝试这样做。后续会想办法,在vite plugin层面做一些工作,以此简化这些写法。

// 删除src/content-scripts/App.tsx的 import "../style.css";
// src/content-scripts/content.ts 调整为以下:

import { createApp } from "vue";
import App from "./App";
import style from "../style.css?inline";
import contentClass from "./content.module.css?inline";

function loadStyle(
  styleContent: string,
  rootElement = document.querySelector("head")
) {
  const style = document.createElement("style");
  style.innerText = styleContent;
  rootElement?.appendChild(style);
}

const createShadowElement = (elementId: string): Element => {
  let rootElement = document.getElementById(elementId);
  if (rootElement) {
    const shadowMountNode = rootElement.shadowRoot?.querySelector(
      `#${elementId}Children`
    ) as Element;
    return shadowMountNode;
  }

  rootElement = document.createElement("div");
  rootElement.setAttribute("id", elementId);
  document.body.appendChild(rootElement);
  const shadowRoot: any = rootElement.attachShadow({ mode: "open" });

  loadStyle(style, shadowRoot);
  loadStyle(contentClass, shadowRoot);

  shadowRoot.defaultView = shadowRoot.ownerDocument.defaultView;

  shadowRoot.createElement = function (elementName: string) {
    const node = document.createElement(elementName);
    Object.defineProperty(node, "ownerDocument", { value: shadowRoot });
    return node;
  };
  const shadowMountNode = shadowRoot.createElement("div");
  shadowMountNode.setAttribute("id", `${elementId}Children`);

  shadowRoot.appendChild(shadowMountNode);

  return shadowMountNode;
};

window.onload =  () => {
  const rootElement = createShadowElement("crx-app");
  const app = createApp(App);
  app.mount(rootElement);
};

@leacoleaco
Copy link

@Jervis2049 这样写好像 vue 页面中的 style 里的样式就不生效了... 😮‍💨

@panghuangdehaozi
Copy link
Author

@leacoleaco 你可以试试这个plasmo

@heng1025
Copy link

可以试试这种方案tailwindlabs/tailwindcss#9633

@leacoleaco
Copy link

@panghuangdehaozi plasmo 用过了, 但是感觉它用于 react 比较友好, 用在vue 感觉好鸡肋哦....写法好麻烦

@chen-af
Copy link

chen-af commented Nov 2, 2023

antd 的 css in js 可以解决呀 如果是content-scripts的话 <style lang="less" scoped></style> 也可以解决吧?

import { defineConfig, splitVendorChunkPlugin } from "vite";
import vue from "@vitejs/plugin-vue";
import crx from "vite-plugin-crx-mv3";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";

export default defineConfig(({ mode }) => {
  return {
    plugins: [
      vue(),
      crx({
        manifest: "./src/manifest.json",
      }),
      Components({
        resolvers: [
          AntDesignVueResolver({
            importStyle: false, // css in js
          }),
        ],
      }),
      splitVendorChunkPlugin(),
    ],
    build: {
      emptyOutDir: mode === "production",
      minify: "esbuild",
    },
  };
});

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

No branches or pull requests

5 participants