From 96d323c6691989cb704da37f00b323cdffd82561 Mon Sep 17 00:00:00 2001 From: PrestonN Date: Sat, 8 Jun 2019 21:36:38 -0400 Subject: [PATCH] Fix issues with initial startup for builds --- src/js/history.js | 3 +- src/js/init.js | 117 ++++++++++++++++++++++++++++++---------------- src/js/layout.js | 5 +- src/js/player.js | 46 +++++++++--------- 4 files changed, 106 insertions(+), 65 deletions(-) diff --git a/src/js/history.js b/src/js/history.js index 6d38550c4..318999a3e 100644 --- a/src/js/history.js +++ b/src/js/history.js @@ -41,9 +41,10 @@ function addToHistory(data){ description: data.description, viewCount: data.viewCount, title: data.title, - description: data.description, lengthSeconds: data.lengthSeconds, videoThumbnails: data.videoThumbnails, + liveNow: false, + paid: false, type: 'video', timeWatched: data.timeWatched, watchProgress: data.watchProgress, diff --git a/src/js/init.js b/src/js/init.js index f03f167e8..895348b2d 100644 --- a/src/js/init.js +++ b/src/js/init.js @@ -34,8 +34,8 @@ const Datastore = require('nedb'); // database logic const localDataStorage = app.getPath('userData'); // Grabs the userdata directory based on the user's OS const settingsDb = new Datastore({ - filename: localDataStorage + '/settings.db', - autoload: true + filename: localDataStorage + '/settings.db', + autoload: true }); require('electron-context-menu')({ @@ -46,24 +46,17 @@ let win; protocol.registerStandardSchemes(['freetube']); -app.setAsDefaultProtocolClient('freetube');//--autoplay-policy=no-user-gesture-required +app.setAsDefaultProtocolClient('freetube'); app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required'); app.commandLine.appendSwitch('disable-web-security'); app.commandLine.appendSwitch('enable-modern-media-controls', 'disabled'); -/*const isSecondInstance = app.makeSingleInstance((commandLine, workingDirectory) => { - // Someone tried to run a second instance, we should focus our window. - if (win) { - if (win.isMinimized()) win.restore() - win.focus() - win.webContents.send('ping', commandLine) - } -});*/ +const gotTheLock = app.requestSingleInstanceLock() -if (require('electron-squirrel-startup')) app.quit(); +if (require('electron-squirrel-startup') || !gotTheLock) app.quit(); /** * Initialize the Electron application @@ -86,30 +79,46 @@ let init = function () { settingsDb.findOne({ _id: 'bounds' }, function (err, doc) { - if (doc.value !== false) { - win.setBounds(doc.value); + if (doc !== null) { + if (doc.value !== false) { + win.setBounds(doc.value); + } } }); - settingsDb.find({_id: 'useTor'}, (err, docs) => { - if (docs[0]['value'] !== false) { - settingsDb.find({_id: 'proxy'}, (err, docs) => { - win.webContents.session.setProxy({ proxyRules: docs[0]['value'] }, function () { + settingsDb.findOne({ + _id: 'useTor' + }, (err, doc) => { + if (doc !== null && doc.value !== false) { + settingsDb.findOne({ + _id: 'proxy' + }, (err, doc) => { + if (doc !== null) { + win.webContents.session.setProxy({ + proxyRules: doc.value + }, function () { + win.loadURL(url.format({ + pathname: path.join(__dirname, '../index.html'), + protocol: 'file:', + slashes: true, + })); + }); + } + else { + win.loadURL(url.format({ + pathname: path.join(__dirname, '../index.html'), + protocol: 'file:', + slashes: true, + })); + } + }); + } else { win.loadURL(url.format({ pathname: path.join(__dirname, '../index.html'), protocol: 'file:', slashes: true, })); - }); - }); - } - else { - win.loadURL(url.format({ - pathname: path.join(__dirname, '../index.html'), - protocol: 'file:', - slashes: true, - })); - } + } }); if (process.env = 'development') { @@ -212,25 +221,35 @@ let init = function () { * example data "SOCKS5://127.0.0.1:9050" */ ipcMain.on("setProxy", (_e, data) => { - win.webContents.session.setProxy({ proxyRules: data }, function () { + win.webContents.session.setProxy({ + proxyRules: data + }, function () { win.webContents.send("proxyAvailable"); }); }); ipcMain.on("setBounds", (_e, data) => { - let bounds = win.getBounds(); + let bounds = win.getBounds(); - settingsDb.findOne({ _id: 'bounds' }, function (err, doc) { - if(doc !== null) { - settingsDb.update({ _id: 'bounds' }, - { $set: { value: bounds }}, - {}, (err, newDoc) => {}); - } - else { - settingsDb.insert({_id: 'bounds', value: bounds,}); - } - }); + settingsDb.findOne({ + _id: 'bounds' + }, function (err, doc) { + if (doc !== null) { + settingsDb.update({ + _id: 'bounds' + }, { + $set: { + value: bounds + } + }, {}, (err, newDoc) => {}); + } else { + settingsDb.insert({ + _id: 'bounds', + value: bounds, + }); + } + }); }); }; @@ -253,11 +272,27 @@ let active = function () { } }; +if (!gotTheLock) { + app.quit() +} else { + app.on('second-instance', (event, commandLine, workingDirectory) => { + // Someone tried to run a second instance, we should focus our window. + if (win) { + if (win.isMinimized()) win.restore() + win.focus() + + win.webContents.send('ping', commandLine) + } + }) + + // Create myWindow, load the rest of the app, etc... + app.on('ready', init); +} + /** * bind events, ready (initialize), * window-all-closed (what happens when the windows closed), * activate */ -app.on('ready', init); app.on('window-all-closed', allWindowsClosed); app.on('activate', active); diff --git a/src/js/layout.js b/src/js/layout.js index 3cd305de9..e60da34e7 100644 --- a/src/js/layout.js +++ b/src/js/layout.js @@ -88,7 +88,10 @@ $(document).ready(() => { _id: 'invidious' }] }, (err, docs) => { - invidiousInstance = docs[0].value; + if (typeof(docs[0]) !== 'undefined') { + invidiousInstance = docs[0].value; + } + loadingView.seen = true; if (typeof(docs[1]) !== 'undefined') { diff --git a/src/js/player.js b/src/js/player.js index ef3dada75..d215df5d3 100644 --- a/src/js/player.js +++ b/src/js/player.js @@ -778,30 +778,29 @@ function clickMiniPlayer(videoId) { miniPlayer.webContents.send('ping', videoData); showToast('Video has been opened in a new window.'); // TODO: Add video to history once fully loaded. - if (rememberHistory === true) { - let historyData = { - videoId: videoData.videoId, - published: videoData.published, - publishedText: videoData.publishedText, - description: videoData.description, - viewCount: videoData.viewCount, - title: videoData.videoTitle, - lengthSeconds: videoData.lengthSeconds, - videoThumbnails: videoData.videoThumbnail, - author: videoData.channelName, - authorId: videoData.channelId, - liveNow: false, - paid: false, - type: 'video', - timeWatched: new Date().getTime(), - watchProgress: videoData.currentTime, - }; - ft.log(historyData); - addToHistory(historyData); - } }); - return; + if (rememberHistory === true) { + let historyData = { + videoId: videoData.videoId, + author: videoData.channelName, + authorId: videoData.channelId, + published: videoData.published, + publishedText: videoData.publishedText, + description: videoData.description, + viewCount: videoData.viewCount, + title: videoData.videoTitle, + lengthSeconds: videoData.lengthSeconds, + videoThumbnails: videoData.videoThumbnail, + liveNow: false, + paid: false, + type: 'video', + timeWatched: new Date().getTime(), + watchProgress: videoData.currentTime, + }; + ft.log(historyData); + addToHistory(historyData); + } }; let validateData = function(data) { @@ -870,6 +869,9 @@ function clickMiniPlayer(videoId) { if (doc !== null) { videoData.currentTime = doc.watchProgress; } + else { + videoData.currentTime = 0; + } }); if (defaultPlayer === 'dash') {