Skip to content

Commit

Permalink
fix(main): preload window
Browse files Browse the repository at this point in the history
  • Loading branch information
u3u committed Sep 6, 2018
1 parent bc4cfa1 commit 0235a98
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ function createMainWindow() {
},
});

const { window } = mb;
const { webContents } = window;

systemPreferences.setUserDefault(
'AppleShowScrollBars',
'string',
Expand Down Expand Up @@ -88,45 +91,39 @@ function createMainWindow() {
const newString = clipboard.readText();
clipboard.writeText(oldString);
mb.showWindow();
mb.window.webContents.send('translate-clipboard-text', newString.trim());
webContents.send('translate-clipboard-text', newString.trim());
});

mb.on('after-create-window', () => {
const { window } = mb;
const { webContents } = window;

if (isDevelopment && !process.env.IS_TEST) {
webContents.openDevTools({ mode: 'undocked' });
}
if (isDevelopment && !process.env.IS_TEST) {
webContents.openDevTools({ mode: 'undocked' });
}

webContents.session.webRequest.onBeforeSendHeaders((detail, cb) => {
const { url, requestHeaders } = detail;
if (url.includes('fanyi.youdao.com')) {
requestHeaders.Referer = 'http://fanyi.youdao.com';
} else delete requestHeaders.Referer;
cb({ requestHeaders });
});
webContents.session.webRequest.onBeforeSendHeaders((detail, cb) => {
const { url, requestHeaders } = detail;
if (url.includes('fanyi.youdao.com')) {
requestHeaders.Referer = 'http://fanyi.youdao.com';
} else delete requestHeaders.Referer;
cb({ requestHeaders });
});

window.on('closed', () => {
mainWindow = null;
});
window.on('closed', () => {
mainWindow = null;
});

webContents.on('did-finish-load', () => {
webContents.setZoomFactor(1);
webContents.setVisualZoomLevelLimits(1, 1);
webContents.setLayoutZoomLevelLimits(0, 0);
});
webContents.on('did-finish-load', () => {
webContents.setZoomFactor(1);
webContents.setVisualZoomLevelLimits(1, 1);
webContents.setLayoutZoomLevelLimits(0, 0);
});

webContents.on('devtools-opened', () => {
webContents.on('devtools-opened', () => {
window.focus();
setImmediate(() => {
window.focus();
setImmediate(() => {
window.focus();
});
});
});

mb.on('after-show', () => {
const { window } = mb;
window.focus();
});

Expand Down

0 comments on commit 0235a98

Please sign in to comment.