// main.js要配置webviewTag: true
const mainWindow = new BrowserWindow({
    width: 900,
    height: 670,
    show: false,
    autoHideMenuBar: true,
    ...(process.platform === 'linux' ? { icon } : {}),
    webPreferences: {
      preload: join(__dirname, '../preload/index.js'),
      sandbox: false,
      webviewTag: true
    }
})



// 组件内使用webview
import { useEffect } from 'react'

function App() {
  useEffect(() => {
    const webview = document.querySelector('webview')
    webview.addEventListener('did-start-loading', () => {
      // webview正在加载事件
      console.log('webview正在加载事件');
      
    })

    webview.addEventListener('did-start-loading', () => {
      // webview加载完毕事件
      console.log('webview加载完毕事件');

      // 对webview内的css添加样式
      webview.insertCSS('#su{background: red !important;}')

      // 对webvie内的输入框添加默认值, 且过三秒后自动点击查询
      webview.executeJavaScript(`
        setTimeout(() => {
          const input = document.querySelector('#kw')
          const btn = document.querySelector('#su')
          input.value = '往这个输入框添加默认值'
          btn.click()
        }, 3000);  
      `)
    })
  }, [])

  return (
    <div className="container">
      {/* nodeintegration 使webview中的访客页具有node集成,并且可以使用require和process */}
      <webview nodeintegration="true" src='http://www.baidu.com' style={{ width: 700, height: 700 }}></webview>
    </div>
  )
}

export default App




Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐