微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐