Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron #6

Open
canvascat opened this issue Dec 24, 2020 · 4 comments
Open

Electron #6

canvascat opened this issue Dec 24, 2020 · 4 comments
Labels

Comments

@canvascat
Copy link
Owner

Electron相关问题🚀

@canvascat
Copy link
Owner Author

Electron拦截HTTP请求,补充额外信息后转发。
问题:Mac平台POST请求body丢失 ➡ Some data is missing when requested via custom HTTP Protocol

@canvascat
Copy link
Owner Author

dialog.showSaveDialogSync([browserWindow, ]options) 有提到:

browserWindow 参数允许该对话框将自身附加到父窗口, 作为父窗口的模态框。

在项目的渲染进程中,一般使用 remote.dialog.showSaveDialog(remote.getCurrentWindow(), options) 将对话框作为父窗口的模态框。
但在最近的项目中发现该方法失效,并不能生产模态框,且 options 参数失效。
于是联想到第一个参数 browserWindow 存在问题,项目中使用了 vue 框架,并将 remote.getCurrentWindow() 获取的 win 挂载到了 vuex 上,第一步猜想是 vue 将 win 转为了 响应式对象导致,但是将项目的此写法去除后依然无法生产模态框。
之后又重新建立了一个 electron-quick-start 项目,测试以上代码一切正常。最后看了下主进程代码(基本不是我写的😥),发现了一行代码:class Window extends BrowserWindow {,且后面所有的窗口都是基于这个 Window 类创建的。在由 BrowserWindow 直接创建的窗口,remote.getCurrentWindow().constructor.name 返回 'BrowserWindow',而 Window 创建的窗口返回 'l'(项目由webpack打包),猜想 showSaveDialog 会检查第一个参数的类型,若不是 BrowserWindow 则当作 options 处理...

@canvascat
Copy link
Owner Author

lib/browser/api/dialog.ts

export function showOpenDialog (windowOrOptions: BrowserWindow | OpenDialogOptions, maybeOptions?: OpenDialogOptions): OpenDialogReturnValue {
  const window = (windowOrOptions && !(windowOrOptions instanceof BrowserWindow) ? null : windowOrOptions);
  const options = (windowOrOptions && !(windowOrOptions instanceof BrowserWindow) ? windowOrOptions : maybeOptions);
  return openDialog(false, window, options);
}

最新的源码采用 instanceof 判断,讲道理使用继承于 BrowserWindow 的子类构建也是不存在问题的...
最后突然意识由于某些特殊原因到现在为止项目依然使用的是 9.1.0 版本😥,于是去查阅 electron 的 9-x-y 分支的 源码 if (window && window.constructor !== BrowserWindow) {,果然是判断的构造函数是否为 BrowserWindow

@canvascat
Copy link
Owner Author

获取剪切板文件列表

import { clipboard } from 'electron';
import plist from 'plist';

function getClipboardFiles(): Array<string> {
  return clipboard.has('NSFilenamesPboardType')
    ? plist.parse(clipboard.read('NSFilenamesPboardType'))
    : [];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant