javascript – 将CSS注入Chrome / Electron / HTML / CSS / JS

谁能告诉我为什么以下不会工作?请原谅我对这一切都不熟悉的任何错误

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>

我试图得到它,以便一旦webview中的内容加载后,背景将通过CSS更改为红色.对任何替代方案持开放态度或帮助解释上述原因不适用的原因.

谢谢

解决方法:

它适用于我使用以下设置,基本上只是电子quick start

作为电子索引运行.js

Result

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.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>

相关文章

这篇文章主要讲解了“electron打包中的坑如何解决”,文中的...
这篇文章主要介绍“electron打包的坑如何解决”的相关知识,...
这篇文章主要为大家分析了VSCode中如何调试Electron应用的主...
这篇“如何在VSCode上调试Electron应用的主进程代码”文章的...
vue-cli+electron一种新的脚手架(vue-electron)vue-electron...
1、首先成功安装Node.js。2、配置好环境变量path,参加上一篇...