Merge pull request #1093 from Svallinn/946-version-flag

Support for version flag
This commit is contained in:
Luca Hohmann 2021-04-10 00:09:58 +02:00 committed by GitHub
commit 23c2cd65f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 468 additions and 463 deletions

View File

@ -1,76 +1,150 @@
import { app, BrowserWindow, Menu, ipcMain, screen } from 'electron' import { app, BrowserWindow, Menu, ipcMain, screen } from 'electron'
import { productName } from '../../package.json'
import Datastore from 'nedb' import Datastore from 'nedb'
require('@electron/remote/main').initialize() if (process.argv.includes('--version')) {
console.log(`v${app.getVersion()}`)
require('electron-context-menu')({ app.exit(0)
showSearchWithGoogle: false,
showSaveImageAs: true,
showCopyImageAddress: true,
prepend: (params, browserWindow) => []
})
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
})
// set app name
app.setName(productName)
// disable electron warning
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
const path = require('path')
const isDev = process.env.NODE_ENV === 'development'
const isDebug = process.argv.includes('--debug')
let mainWindow
let startupUrl
// CORS somehow gets re-enabled in Electron v9.0.4
// This line disables it.
// This line can possible be removed if the issue is fixed upstream
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')
app.commandLine.appendSwitch('enable-accelerated-video-decode')
app.commandLine.appendSwitch('enable-file-cookies')
app.commandLine.appendSwitch('ignore-gpu-blacklist')
// See: https://stackoverflow.com/questions/45570589/electron-protocol-handler-not-working-on-windows
// remove so we can register each time as we run the app.
app.removeAsDefaultProtocolClient('freetube')
// If we are running a non-packaged version of the app && on windows
if (isDev && process.platform === 'win32') {
// Set the path of electron.exe and your app.
// These two additional parameters are only available on windows.
app.setAsDefaultProtocolClient('freetube', process.execPath, [path.resolve(process.argv[1])])
} else { } else {
app.setAsDefaultProtocolClient('freetube') runApp()
} }
// TODO: Uncomment if needed function runApp() {
// only allow single instance of application require('@electron/remote/main').initialize()
if (!isDev) {
const gotTheLock = app.requestSingleInstanceLock()
if (gotTheLock) { require('electron-context-menu')({
app.on('second-instance', (event, commandLine, workingDirectory) => { showSearchWithGoogle: false,
// Someone tried to run a second instance, we should focus our window. showSaveImageAs: true,
if (mainWindow && typeof (commandLine) !== 'undefined') { showCopyImageAddress: true,
if (mainWindow.isMinimized()) mainWindow.restore() prepend: (params, browserWindow) => []
mainWindow.focus() })
const url = getLinkUrl(commandLine) const localDataStorage = app.getPath('userData') // Grabs the userdata directory based on the user's OS
if (url) {
mainWindow.webContents.send('openUrl', url) const settingsDb = new Datastore({
filename: localDataStorage + '/settings.db',
autoload: true
})
// disable electron warning
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
const path = require('path')
const isDev = process.env.NODE_ENV === 'development'
const isDebug = process.argv.includes('--debug')
let mainWindow
let startupUrl
// CORS somehow gets re-enabled in Electron v9.0.4
// This line disables it.
// This line can possible be removed if the issue is fixed upstream
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')
app.commandLine.appendSwitch('enable-accelerated-video-decode')
app.commandLine.appendSwitch('enable-file-cookies')
app.commandLine.appendSwitch('ignore-gpu-blacklist')
// See: https://stackoverflow.com/questions/45570589/electron-protocol-handler-not-working-on-windows
// remove so we can register each time as we run the app.
app.removeAsDefaultProtocolClient('freetube')
// If we are running a non-packaged version of the app && on windows
if (isDev && process.platform === 'win32') {
// Set the path of electron.exe and your app.
// These two additional parameters are only available on windows.
app.setAsDefaultProtocolClient('freetube', process.execPath, [path.resolve(process.argv[1])])
} else {
app.setAsDefaultProtocolClient('freetube')
}
// TODO: Uncomment if needed
// only allow single instance of application
if (!isDev) {
const gotTheLock = app.requestSingleInstanceLock()
if (gotTheLock) {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow && typeof (commandLine) !== 'undefined') {
if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus()
const url = getLinkUrl(commandLine)
if (url) {
mainWindow.webContents.send('openUrl', url)
}
} }
} })
app.on('ready', (event, commandLine, workingDirectory) => {
settingsDb.find({
$or: [
{ _id: 'disableSmoothScrolling' },
{ _id: 'useProxy' },
{ _id: 'proxyProtocol' },
{ _id: 'proxyHostname' },
{ _id: 'proxyPort' }
]
}, function (err, doc) {
if (err) {
app.exit(0)
return
}
let disableSmoothScrolling = false
let useProxy = false
let proxyProtocol = 'socks5'
let proxyHostname = '127.0.0.1'
let proxyPort = '9050'
if (typeof doc === 'object' && doc.length > 0) {
doc.forEach((dbItem) => {
switch (dbItem._id) {
case 'disableSmoothScrolling':
disableSmoothScrolling = dbItem.value
break
case 'useProxy':
useProxy = dbItem.value
break
case 'proxyProtocol':
proxyProtocol = dbItem.value
break
case 'proxyHostname':
proxyHostname = dbItem.value
break
case 'proxyPort':
proxyPort = dbItem.value
break
}
})
}
if (disableSmoothScrolling) {
app.commandLine.appendSwitch('disable-smooth-scrolling')
} else {
app.commandLine.appendSwitch('enable-smooth-scrolling')
}
const proxyUrl = `${proxyProtocol}://${proxyHostname}:${proxyPort}`
createWindow(useProxy, proxyUrl)
if (isDev) {
installDevTools()
}
if (isDebug) {
mainWindow.webContents.openDevTools()
}
})
})
} else {
app.quit()
}
} else {
require('electron-debug')({
showDevTools: !(process.env.RENDERER_REMOTE_DEBUGGING === 'true')
}) })
app.on('ready', (event, commandLine, workingDirectory) => { app.on('ready', () => {
settingsDb.find({ settingsDb.find({
$or: [ $or: [
{ _id: 'disableSmoothScrolling' }, { _id: 'disableSmoothScrolling' },
@ -132,440 +206,371 @@ if (!isDev) {
} }
}) })
}) })
} else {
app.quit()
}
} else {
require('electron-debug')({
showDevTools: !(process.env.RENDERER_REMOTE_DEBUGGING === 'true')
})
app.on('ready', () => {
settingsDb.find({
$or: [
{ _id: 'disableSmoothScrolling' },
{ _id: 'useProxy' },
{ _id: 'proxyProtocol' },
{ _id: 'proxyHostname' },
{ _id: 'proxyPort' }
]
}, function (err, doc) {
if (err) {
app.exit(0)
return
}
let disableSmoothScrolling = false
let useProxy = false
let proxyProtocol = 'socks5'
let proxyHostname = '127.0.0.1'
let proxyPort = '9050'
if (typeof doc === 'object' && doc.length > 0) {
doc.forEach((dbItem) => {
switch (dbItem._id) {
case 'disableSmoothScrolling':
disableSmoothScrolling = dbItem.value
break
case 'useProxy':
useProxy = dbItem.value
break
case 'proxyProtocol':
proxyProtocol = dbItem.value
break
case 'proxyHostname':
proxyHostname = dbItem.value
break
case 'proxyPort':
proxyPort = dbItem.value
break
}
})
}
if (disableSmoothScrolling) {
app.commandLine.appendSwitch('disable-smooth-scrolling')
} else {
app.commandLine.appendSwitch('enable-smooth-scrolling')
}
const proxyUrl = `${proxyProtocol}://${proxyHostname}:${proxyPort}`
createWindow(useProxy, proxyUrl)
if (isDev) {
installDevTools()
}
if (isDebug) {
mainWindow.webContents.openDevTools()
}
})
})
}
async function installDevTools () {
try {
/* eslint-disable */
require('devtron').install()
require('vue-devtools').install()
/* eslint-enable */
} catch (err) {
console.log(err)
}
}
function createWindow (useProxy = false, proxyUrl = '') {
/**
* Initial window options
*/
mainWindow = new BrowserWindow({
backgroundColor: '#fff',
icon: isDev
? path.join(__dirname, '../../_icons/iconColor.png')
/* eslint-disable-next-line */
: `${__dirname}/_icons/iconColor.png`,
autoHideMenuBar: true,
// useContentSize: true,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: false,
webSecurity: false,
backgroundThrottling: false,
enableRemoteModule: true,
contextIsolation: false
},
show: false
})
mainWindow.setBounds({
width: 1200,
height: 800
})
if (useProxy) {
mainWindow.webContents.session.setProxy({
proxyRules: proxyUrl
})
} }
// Set CONSENT cookie on reasonable domains async function installDevTools () {
[ try {
'http://www.youtube.com', /* eslint-disable */
'https://www.youtube.com', require('devtron').install()
'http://youtube.com', require('vue-devtools').install()
'https://youtube.com' /* eslint-enable */
].forEach(url => { } catch (err) {
mainWindow.webContents.session.cookies.set({ console.log(err)
url: url,
name: 'CONSENT',
value: 'YES+'
})
})
settingsDb.findOne({
_id: 'bounds'
}, function (err, doc) {
if (doc === null || err) {
return
} }
}
if (typeof doc !== 'object' || typeof doc.value !== 'object') { function createWindow (useProxy = false, proxyUrl = '') {
return /**
} * Initial window options
*/
mainWindow = new BrowserWindow({
backgroundColor: '#fff',
icon: isDev
? path.join(__dirname, '../../_icons/iconColor.png')
/* eslint-disable-next-line */
: `${__dirname}/_icons/iconColor.png`,
autoHideMenuBar: true,
// useContentSize: true,
webPreferences: {
nodeIntegration: true,
nodeIntegrationInWorker: false,
webSecurity: false,
backgroundThrottling: false,
enableRemoteModule: true,
contextIsolation: false
},
show: false
})
const { maximized, ...bounds } = doc.value mainWindow.setBounds({
const allDisplaysSummaryWidth = screen width: 1200,
.getAllDisplays() height: 800
.reduce((accumulator, { size: { width } }) => accumulator + width, 0) })
if (allDisplaysSummaryWidth >= bounds.x) { if (useProxy) {
mainWindow.setBounds({ mainWindow.webContents.session.setProxy({
x: bounds.x, proxyRules: proxyUrl
y: bounds.y,
width: bounds.width,
height: bounds.height
}) })
} }
if (maximized) {
mainWindow.maximize()
}
})
// eslint-disable-next-line // Set CONSENT cookie on reasonable domains
setMenu() [
'http://www.youtube.com',
// load root file/url 'https://www.youtube.com',
if (isDev) { 'http://youtube.com',
mainWindow.loadURL('http://localhost:9080') 'https://youtube.com'
} else { ].forEach(url => {
/* eslint-disable-next-line */ mainWindow.webContents.session.cookies.set({
mainWindow.loadFile(`${__dirname}/index.html`) url: url,
name: 'CONSENT',
global.__static = path value: 'YES+'
.join(__dirname, '/static') })
.replace(/\\/g, '\\\\') })
}
// Show when loaded
mainWindow.on('ready-to-show', () => {
mainWindow.show()
mainWindow.focus()
})
mainWindow.on('closed', () => {
console.log('closed')
})
ipcMain.on('setBounds', (_e, data) => {
const value = {
...mainWindow.getNormalBounds(),
maximized: mainWindow.isMaximized()
}
settingsDb.findOne({ settingsDb.findOne({
_id: 'bounds' _id: 'bounds'
}, function (err, doc) { }, function (err, doc) {
if (err) { if (doc === null || err) {
return return
} }
if (doc !== null) {
settingsDb.update({ if (typeof doc !== 'object' || typeof doc.value !== 'object') {
_id: 'bounds' return
}, { }
$set: {
value const { maximized, ...bounds } = doc.value
} const allDisplaysSummaryWidth = screen
}, {}) .getAllDisplays()
} else { .reduce((accumulator, { size: { width } }) => accumulator + width, 0)
settingsDb.insert({
_id: 'bounds', if (allDisplaysSummaryWidth >= bounds.x) {
value mainWindow.setBounds({
x: bounds.x,
y: bounds.y,
width: bounds.width,
height: bounds.height
}) })
} }
if (maximized) {
mainWindow.maximize()
}
}) })
})
ipcMain.on('appReady', () => { // eslint-disable-next-line
if (startupUrl) { setMenu()
mainWindow.webContents.send('openUrl', startupUrl)
// load root file/url
if (isDev) {
mainWindow.loadURL('http://localhost:9080')
} else {
/* eslint-disable-next-line */
mainWindow.loadFile(`${__dirname}/index.html`)
global.__static = path
.join(__dirname, '/static')
.replace(/\\/g, '\\\\')
} }
})
ipcMain.on('disableSmoothScrolling', () => { // Show when loaded
app.commandLine.appendSwitch('disable-smooth-scrolling') mainWindow.on('ready-to-show', () => {
mainWindow.close() mainWindow.show()
createWindow() mainWindow.focus()
})
ipcMain.on('enableSmoothScrolling', () => {
app.commandLine.appendSwitch('enable-smooth-scrolling')
mainWindow.close()
createWindow()
})
ipcMain.on('enableProxy', (event, url) => {
console.log(url)
mainWindow.webContents.session.setProxy({
proxyRules: url
}) })
})
ipcMain.on('disableProxy', () => { mainWindow.on('closed', () => {
mainWindow.webContents.session.setProxy({}) console.log('closed')
}) })
}
app.on('window-all-closed', () => { ipcMain.on('setBounds', (_e, data) => {
if (process.platform !== 'darwin') { const value = {
app.quit() ...mainWindow.getNormalBounds(),
maximized: mainWindow.isMaximized()
}
settingsDb.findOne({
_id: 'bounds'
}, function (err, doc) {
if (err) {
return
}
if (doc !== null) {
settingsDb.update({
_id: 'bounds'
}, {
$set: {
value
}
}, {})
} else {
settingsDb.insert({
_id: 'bounds',
value
})
}
})
})
ipcMain.on('appReady', () => {
if (startupUrl) {
mainWindow.webContents.send('openUrl', startupUrl)
}
})
ipcMain.on('disableSmoothScrolling', () => {
app.commandLine.appendSwitch('disable-smooth-scrolling')
mainWindow.close()
createWindow()
})
ipcMain.on('enableSmoothScrolling', () => {
app.commandLine.appendSwitch('enable-smooth-scrolling')
mainWindow.close()
createWindow()
})
ipcMain.on('enableProxy', (event, url) => {
console.log(url)
mainWindow.webContents.session.setProxy({
proxyRules: url
})
})
ipcMain.on('disableProxy', () => {
mainWindow.webContents.session.setProxy({})
})
} }
mainWindow.webContents.session.clearCache() app.on('window-all-closed', () => {
mainWindow.webContents.session.clearStorageData({ if (process.platform !== 'darwin') {
storages: [ app.quit()
'appcache',
'cookies',
'filesystem',
'indexdb',
'shadercache',
'websql',
'serviceworkers',
'cachestorage'
]
})
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
/*
* Callback when processing a freetube:// link (macOS)
*/
app.on('open-url', (event, url) => {
event.preventDefault()
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('openUrl', baseUrl(url))
} else {
startupUrl = baseUrl(url)
}
})
/*
* Check if an argument was passed and send it over to the GUI (Linux / Windows).
* Remove freetube:// protocol if present
*/
const url = getLinkUrl(process.argv)
if (url) {
startupUrl = url
}
function baseUrl(arg) {
return arg.replace('freetube://', '')
}
function getLinkUrl(argv) {
if (argv.length > 1) {
return baseUrl(argv[argv.length - 1])
} else {
return null
}
}
/**
* Auto Updater
*
* Uncomment the following code below and install `electron-updater` to
* support auto updating. Code Signing with a valid certificate is required.
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
*/
/*
import { autoUpdater } from 'electron-updater'
autoUpdater.on('update-downloaded', () => {
autoUpdater.quitAndInstall()
})
app.on('ready', () => {
if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
*/
/* eslint-disable-next-line */
const sendMenuEvent = async data => {
mainWindow.webContents.send('change-view', data)
}
const template = [{
label: 'File',
submenu: [
{
role: 'quit'
} }
]
},
{
label: 'Edit',
submenu: [{
role: 'cut'
},
{
role: 'copy',
accelerator: 'CmdOrCtrl+C',
selector: 'copy:'
},
{
role: 'paste',
accelerator: 'CmdOrCtrl+V',
selector: 'paste:'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
{
role: 'selectall'
}
]
},
{
label: 'View',
submenu: [{
role: 'reload'
},
{
role: 'forcereload',
accelerator: 'CmdOrCtrl+Shift+R'
},
{
role: 'toggledevtools'
},
{
type: 'separator'
},
{
role: 'resetzoom'
},
{
role: 'zoomin'
},
{
role: 'zoomout'
},
{
type: 'separator'
},
{
role: 'togglefullscreen'
}
]
},
{
role: 'window',
submenu: [{
role: 'minimize'
},
{
role: 'close'
}
]
}
]
function setMenu () { mainWindow.webContents.session.clearCache()
if (process.platform === 'darwin') { mainWindow.webContents.session.clearStorageData({
template.unshift({ storages: [
label: app.getName(), 'appcache',
submenu: [ 'cookies',
{ role: 'about' }, 'filesystem',
{ type: 'separator' }, 'indexdb',
{ role: 'services' }, 'shadercache',
{ type: 'separator' }, 'websql',
{ role: 'hide' }, 'serviceworkers',
{ role: 'hideothers' }, 'cachestorage'
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
] ]
}) })
})
template.push({ app.on('activate', () => {
role: 'window' if (mainWindow === null) {
}) createWindow()
}
})
template.push({ /*
role: 'help' * Callback when processing a freetube:// link (macOS)
}) */
app.on('open-url', (event, url) => {
event.preventDefault()
template.push({ role: 'services' }) if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('openUrl', baseUrl(url))
} else {
startupUrl = baseUrl(url)
}
})
/*
* Check if an argument was passed and send it over to the GUI (Linux / Windows).
* Remove freetube:// protocol if present
*/
const url = getLinkUrl(process.argv)
if (url) {
startupUrl = url
} }
const menu = Menu.buildFromTemplate(template) function baseUrl(arg) {
Menu.setApplicationMenu(menu) return arg.replace('freetube://', '')
}
function getLinkUrl(argv) {
if (argv.length > 1) {
return baseUrl(argv[argv.length - 1])
} else {
return null
}
}
/**
* Auto Updater
*
* Uncomment the following code below and install `electron-updater` to
* support auto updating. Code Signing with a valid certificate is required.
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
*/
/*
import { autoUpdater } from 'electron-updater'
autoUpdater.on('update-downloaded', () => {
autoUpdater.quitAndInstall()
})
app.on('ready', () => {
if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
*/
/* eslint-disable-next-line */
const sendMenuEvent = async data => {
mainWindow.webContents.send('change-view', data)
}
const template = [{
label: 'File',
submenu: [
{
role: 'quit'
}
]
},
{
label: 'Edit',
submenu: [{
role: 'cut'
},
{
role: 'copy',
accelerator: 'CmdOrCtrl+C',
selector: 'copy:'
},
{
role: 'paste',
accelerator: 'CmdOrCtrl+V',
selector: 'paste:'
},
{
role: 'pasteandmatchstyle'
},
{
role: 'delete'
},
{
role: 'selectall'
}
]
},
{
label: 'View',
submenu: [{
role: 'reload'
},
{
role: 'forcereload',
accelerator: 'CmdOrCtrl+Shift+R'
},
{
role: 'toggledevtools'
},
{
type: 'separator'
},
{
role: 'resetzoom'
},
{
role: 'zoomin'
},
{
role: 'zoomout'
},
{
type: 'separator'
},
{
role: 'togglefullscreen'
}
]
},
{
role: 'window',
submenu: [{
role: 'minimize'
},
{
role: 'close'
}
]
}
]
function setMenu () {
if (process.platform === 'darwin') {
template.unshift({
label: app.getName(),
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
})
template.push({
role: 'window'
})
template.push({
role: 'help'
})
template.push({ role: 'services' })
}
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
} }