2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Electronの`shell.openExternal`が上手く動かない際の注意すべき点

Last updated at Posted at 2020-11-25

先日Electronを使用したWindowsアプリケーションを作成している際、Electronのshell.openExternalが上手く動かないトラブルにハマりました。
原因としては非常に初歩的なうっかりミスだったのですが、誰か同じ点で躓いている方のお役に立てばと思い記事にさせていただきました。

やりたかったこと

「外部ブラウザで何か画面を開き、自分のウィンドウは閉じる」みたいな処理をしたい

ハマった点

Electronのshell.openExternalは非同期で開く処理をしています。
そのため、安直に

shell.OpenExternal('https://example.com/');
//何か処理
window.close();

のように書いてしまうと開く前にレンダラプロセスが終了してしまい、意図したとおりに動かなくなってしまう可能性があります。

解決法

shell.openExternalPromiseを返してくれるので、それにメソッドチェーンとして

shell.openExternal('https://example.com/').then(() => {
    // 何か処理
    window.close();
}).catch((e) => {
    // 何かエラー時の処理
    console.log(`Error!: ${e}`);
});

のように書くと、意図したとおり動いてくれます。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?