OSDN Git Service

v1.7.6
[serene/MyBrowser.git] / app / electron / Application.js
1 const { app, ipcMain, protocol, session, BrowserWindow, BrowserView, Menu, nativeImage, clipboard, dialog, Notification } = require('electron');
2 const path = require('path');
3 const fs = require('fs');
4 const url = require('url');
5 const os = require('os');
6
7 const { autoUpdater } = require('electron-updater');
8
9 const { extensionsMain } = require('electron-extensions');
10 const config = require('./Config');
11
12 const WindowManager = require('./WindowManager');
13 const windowManager = new WindowManager();
14
15 const protocolStr = 'flast';
16 const fileProtocolStr = `${protocolStr}-file`;
17
18 let loginCallback;
19 let mainWindow;
20 let subWindow;
21
22 getBaseWindow = (width = 1100, height = 680, minWidth = 320, minHeight = 600, x, y, frame = false) => {
23     return new BrowserWindow({
24         width, height, minWidth, minHeight, x, y, 'titleBarStyle': 'hidden', frame, fullscreenable: true,
25         webPreferences: {
26             nodeIntegration: true,
27             webviewTag: true,
28             plugins: true,
29             experimentalFeatures: true,
30             contextIsolation: false,
31         }
32     });
33 }
34
35 module.exports = class Application {
36     loadWindow = () => {
37         protocol.registerSchemesAsPrivileged([
38             { scheme: protocolStr, privileges: { standard: true, bypassCSP: true, secure: true } },
39             { scheme: fileProtocolStr, privileges: { standard: false, bypassCSP: true, secure: true } }
40         ]);
41
42         autoUpdater.on('checking-for-update', () => {
43             console.log('Checking for update...')
44         })
45         autoUpdater.on('update-available', (info) => {
46             console.log('Update available.');
47         })
48         autoUpdater.on('update-not-available', (info) => {
49             console.log('Update not available.');
50         })
51         autoUpdater.on('error', (err) => {
52             console.log('Error in auto-updater. ' + err);
53         })
54         autoUpdater.on('download-progress', (progressObj) => {
55             console.log('Download speed: ' + progressObj.bytesPerSecond + ' - Downloaded ' + progressObj.percent + '% (' + progressObj.transferred + "/" + progressObj.total + ')');
56         })
57         autoUpdater.on('update-downloaded', (info) => {
58             console.log('Update downloaded.');
59         });
60
61         app.on('ready', () => {
62             process.env.GOOGLE_API_KEY = config.googleAPIKey;
63
64             session.defaultSession.setUserAgent(session.defaultSession.getUserAgent().replace(/ Electron\/[0-9\.]*/g, ''));
65
66             autoUpdater.checkForUpdatesAndNotify();
67             Menu.setApplicationMenu(null);
68
69             windowManager.addWindow();
70             // this.loadExtension('gmngpagflejjoblmmamaonmnkghjmebh');
71             // this.loadExtension('bdiadchoogngocfifcomfeakccmcecee');
72         });
73
74         app.on('window-all-closed', () => {
75             if (process.platform !== 'darwin') {
76                 app.quit();
77             }
78         });
79
80         app.on('activate', () => {
81         });
82
83         app.on('login', (e, webContents, request, authInfo, callback) => {
84             e.preventDefault();
85
86             subWindow = getBaseWindow(320, 230, 320, 230);
87             // subWindow.setParentWindow(mainWindow);
88             subWindow.setMovable(false);
89             subWindow.setResizable(false);
90             subWindow.setMinimizable(false);
91             subWindow.setMaximizable(false);
92             const startUrl = process.env.ELECTRON_START_URL || url.format({
93                 pathname: path.join(__dirname, '/../build/index.html'),
94                 protocol: 'file:',
95                 slashes: true,
96                 hash: '/authentication',
97             });
98
99             subWindow.loadURL(startUrl);
100             loginCallback = callback;
101         });
102
103         ipcMain.on('authorization', (event, arg) => {
104             loginCallback(arg.username, arg.password);
105             subWindow.close();
106         });
107     }
108
109     loadExtension = (id) => {
110         const extensionDir = path.resolve(os.homedir(), 'AppData\\Local\\Google\\Chrome\\User Data\\Default\\Extensions');
111
112         const versions = fs.readdirSync(`${extensionDir}/${id}`).sort();
113         const version = versions.pop();
114
115         // BrowserWindow.addExtension(`${extensionDir}/${id}/${version}`);
116         // BrowserWindow.addDevToolsExtension(`${extensionDir}/${id}/${version}`);
117         extensionsMain.setSession(session.defaultSession);
118         extensionsMain.load(`${extensionDir}/${id}/${version}`);
119     }
120 };