将 CSS 注入 <webview> Chrome/Electron/HTML/CSS/JS

use*_*169 6 javascript css jquery webview electron

谁能告诉我为什么下面的不起作用?请原谅任何错误我对这一切都很陌生

HTML

    <webview id="wv1" src="https://www.github.com/" 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>
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取它,以便一旦加载了 webview 中的内容,背景就会通过 CSS 更改为红色。对任何替代方案持开放态度或帮助解释为什么上述方法不起作用。

谢谢

E. *_*din 4

它适用于我使用以下设置,基本上只是电子快速启动

运行为electron index.js

结果

索引.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()
    }
})
Run Code Online (Sandbox Code Playgroud)

索引.html

<webview id="wv1" src="https://www.github.com/" 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>
Run Code Online (Sandbox Code Playgroud)