Inject CSS stylesheet as string using Javascript

后端 未结 4 1265
一整个雨季
一整个雨季 2020-11-30 01:37

I\'m developing a Chrome extension, and I\'d like users to be able to add their own CSS styles to change the appearance of the extension\'s pages (not web pages). I\'ve look

4条回答
  •  孤城傲影
    2020-11-30 02:25

    I had this same need recently and wrote a function to do the same as Liam's, except to also allow for multiple lines of CSS.

    injectCSS(function(){/*
        .ui-button {
            border: 3px solid #0f0;
            font-weight: bold;
            color: #f00;
        }
        .ui-panel {
            border: 1px solid #0f0;
            background-color: #eee;
            margin: 1em;
        }
    */});
    
    // or the following for one line
    
    injectCSS('.case2 { border: 3px solid #00f; } ');
    

    The source of this function. You can download from the Github repo. Or see some more example usage here.

    My preference is to use it with RequireJS, but it also will work as a global function in the absence of an AMD loader.

提交回复
热议问题