Code Monkey home page Code Monkey logo

Comments (10)

Jervis2049 avatar Jervis2049 commented on June 18, 2024

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

from vite-plugin-crx-mv3.

panghuangdehaozi avatar panghuangdehaozi commented on June 18, 2024

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

from vite-plugin-crx-mv3.

Jervis2049 avatar Jervis2049 commented on June 18, 2024

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

from vite-plugin-crx-mv3.

leacoleaco avatar leacoleaco commented on June 18, 2024

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

from vite-plugin-crx-mv3.

Jervis2049 avatar Jervis2049 commented on June 18, 2024

我在项目中使用了 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);
};

from vite-plugin-crx-mv3.

leacoleaco avatar leacoleaco commented on June 18, 2024

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

from vite-plugin-crx-mv3.

panghuangdehaozi avatar panghuangdehaozi commented on June 18, 2024

@leacoleaco 你可以试试这个plasmo

from vite-plugin-crx-mv3.

heng1025 avatar heng1025 commented on June 18, 2024

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

from vite-plugin-crx-mv3.

leacoleaco avatar leacoleaco commented on June 18, 2024

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

from vite-plugin-crx-mv3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.