From b73a5f24fcc82256147643a1b1fdc8635e064e7d Mon Sep 17 00:00:00 2001 From: Aoichaan0513 Date: Fri, 17 May 2019 20:51:39 +0900 Subject: [PATCH] =?utf8?q?v1.2.4=20=E3=83=BB=E8=A4=87=E6=95=B0=E3=82=A6?= =?utf8?q?=E3=82=A3=E3=83=B3=E3=83=89=E3=82=A6=E5=8C=96=E3=81=AE=E6=AD=A3?= =?utf8?q?=E5=BC=8F=E5=AF=BE=E5=BF=9C=20=E3=83=BB=E3=83=97=E3=83=A9?= =?utf8?q?=E3=82=A4=E3=83=99=E3=83=BC=E3=83=88=E3=83=A2=E3=83=BC=E3=83=89?= =?utf8?q?=E3=81=AE=E6=AD=A3=E5=BC=8F=E5=AF=BE=E5=BF=9C=20=E3=83=BB?= =?utf8?q?=E3=81=9D=E3=81=AE=E4=BB=96=E3=83=90=E3=82=B0=E3=83=BB=E4=B8=8D?= =?utf8?q?=E5=85=B7=E5=90=88=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- electron/Application.js | 128 +--------------------------------------------- electron/WindowManager.js | 75 +++++++++++++++++++++++---- package.json | 2 +- src/Browser.js | 12 ++--- 4 files changed, 75 insertions(+), 142 deletions(-) diff --git a/electron/Application.js b/electron/Application.js index 41e65ca..7e55656 100644 --- a/electron/Application.js +++ b/electron/Application.js @@ -141,68 +141,7 @@ module.exports = class Application { loadFilters(); registerProtocols(); - // Menu.setApplicationMenu(null); - - /* - const { width, height, x, y } = config.get('window.bounds'); - mainWindow = getBaseWindow(config.get('window.isMaximized') ? 1110 : width, config.get('window.isMaximized') ? 680 : height, 320, 600, x, y, !config.get('window.isCustomTitlebar')); - - config.get('window.isMaximized') && mainWindow.maximize(); - - const startUrl = process.env.ELECTRON_START_URL || url.format({ - pathname: path.join(__dirname, '/../build/index.html'), // 警告:このファイルを移動する場合ここの相対パスの指定に注意してください - protocol: 'file:', - slashes: true, - }); - - mainWindow.loadURL(startUrl); - - localShortcut.register(mainWindow, 'CmdOrCtrl+Shift+I', () => { - if (mainWindow.getBrowserView() == undefined) return; - - const view = mainWindow.getBrowserView(); - if (view.webContents.isDevToolsOpened()) { - view.webContents.devToolsWebContents.focus(); - } else { - view.webContents.openDevTools(); - } - }); - - localShortcut.register(mainWindow, 'CmdOrCtrl+R', () => { - if (mainWindow.getBrowserView() == undefined) return; - - const view = mainWindow.getBrowserView(); - view.webContents.reload(); - }); - - localShortcut.register(mainWindow, 'CmdOrCtrl+Shift+R', () => { - if (mainWindow.getBrowserView() == undefined) return; - - const view = mainWindow.getBrowserView(); - view.webContents.reloadIgnoringCache(); - }); - - mainWindow.on('closed', () => { - mainWindow = null; - }); - - ['resize', 'move'].forEach(ev => { - mainWindow.on(ev, () => { - config.set('window.isMaximized', mainWindow.isMaximized()); - config.set('window.bounds', mainWindow.getBounds()); - }) - }); - - mainWindow.on('maximize', this.fixBounds); - mainWindow.on('unmaximize', this.fixBounds); - mainWindow.on('enter-full-screen', this.fixBounds); - mainWindow.on('leave-full-screen', this.fixBounds); - mainWindow.on('enter-html-full-screen', this.fixBounds); - mainWindow.on('leave-html-full-screen', this.fixBounds); - - // mainWindow.webContents.openDevTools(); - - */ + Menu.setApplicationMenu(null); windowManager.addWindow(); }); @@ -214,49 +153,13 @@ module.exports = class Application { }); app.on('activate', () => { - if (mainWindow === null) { - Menu.setApplicationMenu(null); - - const { width, height, x, y } = config.get('window.bounds'); - mainWindow = getBaseWindow(config.get('window.isMaximized') ? 1110 : width, config.get('window.isMaximized') ? 680 : height, 320, 600, x, y, !config.get('window.isCustomTitlebar')); - - config.get('window.isMaximized') && mainWindow.maximize(); - - const startUrl = process.env.ELECTRON_START_URL || url.format({ - pathname: path.join(__dirname, '/../build/index.html'), // 警告:このファイルを移動する場合ここの相対パスの指定に注意してください - protocol: 'file:', - slashes: true, - }); - - mainWindow.loadURL(startUrl); - - mainWindow.on('closed', () => { - mainWindow = null; - }); - - ['resize', 'move'].forEach(ev => { - mainWindow.on(ev, () => { - config.set('window.isMaximized', mainWindow.isMaximized()); - config.set('window.bounds', mainWindow.getBounds()); - }) - }); - - mainWindow.on('maximize', this.fixBounds); - mainWindow.on('unmaximize', this.fixBounds); - mainWindow.on('enter-full-screen', this.fixBounds); - mainWindow.on('leave-full-screen', this.fixBounds); - mainWindow.on('enter-html-full-screen', this.fixBounds); - mainWindow.on('leave-html-full-screen', this.fixBounds); - - registerProtocols(); - } }); app.on('login', (e, webContents, request, authInfo, callback) => { e.preventDefault(); subWindow = getBaseWindow(320, 230, 320, 230); - subWindow.setParentWindow(mainWindow); + // subWindow.setParentWindow(mainWindow); subWindow.setMovable(false); subWindow.setResizable(false); subWindow.setMinimizable(false); @@ -310,36 +213,9 @@ module.exports = class Application { config.clear(); db.history.remove({}, { multi: true }); }); - - } - - fixBounds = () => { - if (mainWindow.getBrowserView() == undefined) return; - - const view = mainWindow.getBrowserView(); - const { width, height } = mainWindow.getContentBounds(); - - view.setAutoResize({ width: true, height: true }); - if (mainWindow.isFullScreen()) { - view.setBounds({ - x: 0, - y: 0, - width: width, - height: height, - }); - } else { - view.setBounds({ - x: 1, - y: 73 + 1, - width: width - 2, - height: mainWindow.isMaximized() ? height - 73 : (height - 73) - 2, - }); - } - view.setAutoResize({ width: true, height: true }); } loadExtension = (id) => { - console.log(os.homedir()) const extensionDir = path.resolve(os.homedir(), 'AppData\\Local\\Google\\Chrome\\User Data\\Default\\Extensions'); const versions = fs.readdirSync(`${extensionDir}/${id}`).sort(); diff --git a/electron/WindowManager.js b/electron/WindowManager.js index 1fe2755..0dee560 100644 --- a/electron/WindowManager.js +++ b/electron/WindowManager.js @@ -107,6 +107,40 @@ registerProtocols = () => { }, (error) => { if (error) console.error('Failed to register protocol: ' + error); }); + + session.fromPartition('persist:private').protocol.registerFileProtocol('my', (request, callback) => { + const parsed = url.parse(request.url); + + if (parsed.hostname.endsWith('.css') || parsed.hostname.endsWith('.js')) { + return callback({ + path: path.join(app.getAppPath(), 'pages', parsed.hostname), + }); + } else { + return callback({ + path: path.join(app.getAppPath(), 'pages', `${parsed.hostname}.html`), + }); + } + }, (error) => { + if (error) console.error('Failed to register protocol: ' + error); + }); +} + +registerProtocolWithPrivateMode = (windowId) => { + session.fromPartition(`persist:${windowId}`).protocol.registerFileProtocol('my', (request, callback) => { + const parsed = url.parse(request.url); + + if (parsed.hostname.endsWith('.css') || parsed.hostname.endsWith('.js')) { + return callback({ + path: path.join(app.getAppPath(), 'pages', parsed.hostname), + }); + } else { + return callback({ + path: path.join(app.getAppPath(), 'pages', `${parsed.hostname}.html`), + }); + } + }, (error) => { + if (error) console.error('Failed to register protocol: ' + error); + }); } module.exports = class WindowManager { @@ -114,13 +148,19 @@ module.exports = class WindowManager { this.windows = new Map(); console.log('WindowManager: constructor'); + + ipcMain.on('window-add', (e, args) => { + this.addWindow(args.isPrivate); + }); } - addWindow = () => { + addWindow = (isPrivate = false) => { + loadFilters(); + const { width, height, x, y } = config.get('window.bounds'); const window = getBaseWindow(config.get('window.isMaximized') ? 1110 : width, config.get('window.isMaximized') ? 680 : height, 320, 600, x, y, !config.get('window.isCustomTitlebar')); - const id = window.id; + const id = (!isPrivate ? window.id : `private-${window.id}`); config.get('window.isMaximized') && window.maximize(); @@ -169,6 +209,14 @@ module.exports = class WindowManager { }) }); + window.on('close', (e) => { + for (var i = 0; i < views.length; i++) { + if (views[i].windowId == id) { + views.splice(i, 1); + } + } + }); + window.on('maximize', this.fixBounds.bind(this, id)); window.on('unmaximize', this.fixBounds.bind(this, id)); window.on('enter-full-screen', this.fixBounds.bind(this, id)); @@ -198,8 +246,10 @@ module.exports = class WindowManager { ipcMain.on(`browserview-get-${id}`, (e, args) => { let datas = []; for (var i = 0; i < views.length; i++) { - const url = views[i].view.webContents.getURL(); - datas.push({ id: views[i].id, title: views[i].view.webContents.getTitle(), url: url, icon: url.startsWith('my://') ? undefined : `http://www.google.com/s2/favicons?domain=${url}` }); + if (views[i].windowId == id) { + const url = views[i].view.webContents.getURL(); + datas.push({ id: views[i].id, title: views[i].view.webContents.getTitle(), url: url, icon: url.startsWith('my://') ? undefined : `http://www.google.com/s2/favicons?domain=${url}` }); + } } e.sender.send(`browserview-get-${id}`, { views: datas }); }); @@ -333,12 +383,13 @@ module.exports = class WindowManager { getViews = (windowId) => { let datas = []; for (var i = 0; i < views.length; i++) { - if (views[i].windowId != windowId) continue; - const url = views[i].view.webContents.getURL(); - datas.push({ id: views[i].id, title: views[i].view.webContents.getTitle(), url: url, icon: url.startsWith('my://') ? undefined : `http://www.google.com/s2/favicons?domain=${url}` }); + if (views[i].windowId == windowId) { + const url = views[i].view.webContents.getURL(); + datas.push({ id: views[i].id, title: views[i].view.webContents.getTitle(), url: url, icon: url.startsWith('my://') ? undefined : `http://www.google.com/s2/favicons?domain=${url}` }); + } } const window = this.windows.get(windowId); - window.webContents.send('browserview-get', { views: datas }); + window.webContents.send(`browserview-get-${windowId}`, { views: datas }); } addTab = (windowId, id, url = config.get('homePage.defaultPage'), isActive = true) => { @@ -352,10 +403,15 @@ module.exports = class WindowManager { experimentalFeatures: true, safeDialogs: true, safeDialogsMessage: '今後このページではダイアログを表示しない', + ...(String(windowId).startsWith('private') && { partition: `persist:${windowId}` }), preload: require.resolve('./Preload') } }); + if (String(windowId).startsWith('private')) { + registerProtocolWithPrivateMode(windowId); + } + view.webContents.on('did-start-loading', () => { window.webContents.send(`browserview-start-loading-${windowId}`, { id: id }); }); @@ -383,7 +439,7 @@ module.exports = class WindowManager { window.setTitle(view.webContents.getTitle()); window.webContents.send(`browserview-load-${windowId}`, { id: id, title: view.webContents.getTitle(), url: view.webContents.getURL() }); - if (!view.webContents.getURL().startsWith('my://')) + if (!String(windowId).startsWith('private') && !view.webContents.getURL().startsWith('my://')) db.history.insert({ title: view.webContents.getTitle(), url: view.webContents.getURL() }); this.updateNavigationState(windowId, id, view); @@ -614,6 +670,7 @@ module.exports = class WindowManager { view.webContents.loadURL(url); views.push({ windowId, id, view }); + console.log(views); if (isActive) { window.webContents.send(`browserview-set-${windowId}`, { id: id }); diff --git a/package.json b/package.json index 737f2de..b0b7a8d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "MyBrowser-Stable", - "version": "1.0.0", + "version": "1.2.4", "private": true, "main": "electron/Starter.js", "homepage": "./", diff --git a/src/Browser.js b/src/Browser.js index c708a48..0de7501 100644 --- a/src/Browser.js +++ b/src/Browser.js @@ -287,7 +287,7 @@ class BrowserView extends Component { userMenu = async () => { const menu = Menu.buildFromTemplate([ { - label: `${process.env.USERNAME} でログイン中`, + label: (!this.props.windowId.startsWith('private') ? `${process.env.USERNAME} でログイン中` : 'プライベートモード'), enabled: false, }, { type: 'separator' }, @@ -312,12 +312,12 @@ class BrowserView extends Component { { label: '新しいウィンドウ', accelerator: 'CmdOrCtrl+N', - click: () => { this.props.addTab('https://www.google.co.jp/'); } + click: () => { ipcRenderer.send(`window-add`, { isPrivate: false }); } }, { label: 'シークレット ウィンドウを開く', accelerator: 'CmdOrCtrl+Shift+N', - click: () => { this.props.addTab('https://www.google.co.jp/'); } + click: () => { ipcRenderer.send(`window-add`, { isPrivate: true }); } }, { type: 'separator' }, { @@ -362,7 +362,7 @@ class BrowserView extends Component { { this.reload(); }} /> { this.goHome(); }} /> { this.setState({ barText: e.target.value }); }} onKeyDown={this.handleKeyDown} onContextMenu={this.handleContextMenu} /> - { this.userMenu(); }} /> + { this.userMenu(); }} /> { this.moreMenu(); }} /> @@ -446,12 +446,12 @@ class App extends Component { updateTab = () => { ipcRenderer.send(`browserview-get-${this.props.match.params.windowId}`, {}); } - + getForegroundColor = (hexColor) => { var r = parseInt(hexColor.substr(1, 2), 16); var g = parseInt(hexColor.substr(3, 2), 16); var b = parseInt(hexColor.substr(5, 2), 16); - + return ((((r * 299) + (g * 587) + (b * 114)) / 1000) < 128) ? '#ffffff' : '#000000'; } -- 2.11.0