Isolating CSS for Chrome extension

前端 未结 4 1506
小鲜肉
小鲜肉 2021-01-02 21:39

I\'m building a Chrome extension that does some UI injection using content scripts. The problem is that since every website is different and may try to screw around with the

4条回答
  •  佛祖请我去吃肉
    2021-01-02 22:29

    I recently created Boundary, a CSS+JS library to solve problems just like this. Boundary creates elements that are completely separate from the existing webpage's CSS.

    Take creating a dialog for example. After installing Boundary, you can do this in your content script

    var dialog = Boundary.createBox("yourDialogID", "yourDialogClassName");
    
    Boundary.loadBoxCSS("#yourDialogID", "style-for-elems-in-dialog.css");
    
    Boundary.appendToBox(
        "#yourDialogID",
        ""
    );
    
    Boundary.find("#submit_button").click(function() {
      // find() function returns a regular jQuery DOM element
      // so you can do whatever you want with it.
      // some js after button is clicked.
    });
    

    Elements within #yourDialogID will not be affected by the existing webpage.

    Hope this helps. Please let me know if you have any question.

    https://github.com/liviavinci/Boundary

提交回复
热议问题