Skip to content

Commit

Permalink
Fixed load translate.google.com
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Oct 17, 2020
1 parent 9736925 commit 2285083
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 42 deletions.
42 changes: 10 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
"dependencies": {
"@mantou/gem": "^0.2.18",
"auto-launch": "^5.0.5",
"electron-updater": "^4.3.5",
"internet-available": "^1.0.0",
"lodash": "^4.17.15",
"node-fetch": "^2.6.0",
"node-ipc": "^9.1.1",
"regedit": "^3.0.3",
"robotjs": "^0.6.0"
Expand All @@ -44,7 +44,6 @@
"electron": "^7.1.2",
"electron-builder": "^22.9.1",
"electron-rebuild": "^1.8.8",
"electron-updater": "^4.3.5",
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-import": "^2.18.2",
Expand Down
19 changes: 11 additions & 8 deletions src/main/window.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BrowserWindow, screen, app } from 'electron';
import fetch from 'node-fetch';
import internetAvailable from 'internet-available';

import config from '../config';
Expand Down Expand Up @@ -72,13 +71,17 @@ export default class Window extends BrowserWindow {
internetAvailable({ retries: 3600 })
.then(() => {
return new Promise(async (resolve, reject) => {
setTimeout(() => reject(new Error('timeout')), 3000);
try {
const res = await fetch(config.translateUrl, { method: 'HEAD' });
res.ok ? resolve() : reject(new Error('fetch fail'));
} catch {
reject(new Error('network error'));
}
// https://github.com/electron/electron/issues/20357
const testWindow = new BrowserWindow({ show: false });
testWindow.loadURL(config.translateUrl);
const timer = setTimeout(() => {
testWindow.close();
reject(new Error('timeout'));
}, 3000);
testWindow.webContents.on('dom-ready', () => {
resolve();
clearTimeout(timer);
});
});
})
.then(() => {
Expand Down

0 comments on commit 2285083

Please sign in to comment.