【问题标题】:Get the size of BrowserView from a Webview in Electron从 Electron 中的 Webview 获取 BrowserView 的大小
【发布时间】:2019-07-20 11:36:34
【问题描述】:

我正在尝试将功能添加为seen here 在其他元素下方的 Web 视图中。因此,它获得的 x 和 y 值当然只是 webview 本身,但是inspectElement() 方法需要将窗口的 x 和 y 值作为一个整体来获取,而不仅仅是 webview。所以我需要能够得到整个窗口的高度,这样我就可以做一些数学运算来抵消我传递给inspectElement()的值。

This answer 当我打电话给remote.getCurrentWindow() 时出现此错误

Uncaught TypeError: Cannot read property 'getSize' of null
    at click (<anonymous>:3950:147)
    at CallbacksRegistry.apply (<anonymous>:1090:25)
    at handleMessage (<anonymous>:898:21)
    at EventEmitter.ipcRenderer.on.args (<anonymous>:888:7)
    at EventEmitter.emit (<anonymous>:5163:17)
    at Object.ipcNative.onInternalMessage (<anonymous>:3010:15)

所以它返回 null。因此,这个答案是不够的。我似乎在 StackOverflow 或其他网站上找不到任何关于如何从 webview 获取窗口高度,或者 webview 从窗口本身偏移多少的信息。

【问题讨论】:

    标签: webview electron


    【解决方案1】:

    经过一番调查和阅读文档后,我得到了这个解决方案。

    这是一个函数,你将 webContents 传递给这个函数,你就可以访问 BrowserWindow

    function getBrowserWindowFromWebContents(
      contents: Electron.WebContents
    ): Electron.BrowserWindow {
      let topContents = contents
      while (topContents.hostWebContents) {
        topContents = topContents.hostWebContents
      }
    
      return remote.BrowserWindow.fromWebContents(topContents)
    }
    

    那么我们就可以访问getSize()

    getBrowserWindowFromWebContents(remote.getCurrentWebContents()).getSize()
    

    我们去吧:)

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 1970-01-01
      相关资源
      最近更新 更多