【问题标题】:Inject CSS into <webview> Chrome/Electron/HTML/CSS/JS将 CSS 注入 <webview> Chrome/Electron/HTML/CSS/JS
【发布时间】:2017-08-13 07:45:25
【问题描述】:

谁能告诉我为什么下面的行不通?请原谅我对所有这些都是新手的任何错误

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 更改为红色。愿意接受任何替代方案或帮助解决上述问题的原因。

谢谢

【问题讨论】:

    标签: javascript jquery css webview electron


    【解决方案1】:

    只是为了确保您可能想要使用 DOMContentLoaded 而不是 dom-ready,而不是 insertCss,insertRule。

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

    但是,如果它不起作用,您可能想尝试一下

    webview.querySelector(body) !== null
    

    函数内部。

    希望它有效。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-21
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-25
        相关资源
        最近更新 更多