最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Inject CSS into <webview> ChromeElectronHTMLCSSJS - Stack Overflow

programmeradmin2浏览0评论

Can anyone tell me why the below wont work? Please forgive any mistakes I am new to all of this

HTML

    <webview id="wv1" src="/" style="display:inline-flex; width:100%; height:140px" nodeintegration></webview>



    <script>
    var webview = document.getElementById('wv1');
    webview.addEventListener('dom-ready', function() {
    webview.insertCSS('html,body{ background-color: #FF0000 !important;}')
    });

    </script>

I'm trying to get it so that once the content within the webview has loaded the background is changed to red via CSS. Open to any alternatives or help with why the above wont work.

thanks

Can anyone tell me why the below wont work? Please forgive any mistakes I am new to all of this

HTML

    <webview id="wv1" src="https://www.github./" style="display:inline-flex; width:100%; height:140px" nodeintegration></webview>



    <script>
    var webview = document.getElementById('wv1');
    webview.addEventListener('dom-ready', function() {
    webview.insertCSS('html,body{ background-color: #FF0000 !important;}')
    });

    </script>

I'm trying to get it so that once the content within the webview has loaded the background is changed to red via CSS. Open to any alternatives or help with why the above wont work.

thanks

Share Improve this question asked Mar 21, 2017 at 14:52 user3236169user3236169 1552 gold badges3 silver badges13 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

It works for me using the following setup, basically just the electron quick start

Run as electron index.js

index.js

const { app, BrowserWindow } = require('electron')
const path = require('path')
const url = require('url')

let win

function createWindow() {
    win = new BrowserWindow({ width: 800, height: 600 })

    win.loadURL(url.format({
        pathname: path.join(__dirname, 'index.html'),
        protocol: 'file:',
        slashes: true
    }))

    win.webContents.openDevTools()

    win.on('closed', () => {
        win = null
    })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (win === null) {
        createWindow()
    }
})

index.html

<webview id="wv1" src="https://www.github./" style="display:inline-flex; width:100%; height:140px" nodeintegration></webview>

<script>
    var webview = document.getElementById('wv1');
    webview.addEventListener('dom-ready', function () {
        webview.insertCSS('html,body{ background-color: #FF0000 !important;}')
    });

</script>

Just to be sure you may want to use instead of dom-ready the DOMContentLoaded and instead of insertCss, insertRule.

var webview = document.getElementById('wv1');

webview.addEventListener('DOMContentLoaded', function() {
   webview.insertRule('html,body{ background-color: #FF0000 !important;}',1)
 });

However, if it won't work you may want to try

webview.querySelector(body) !== null

Inside the function.

hope it works.

发布评论

评论列表(0)

  1. 暂无评论