better coding :)

This commit is contained in:
cutls 2019-11-18 01:07:44 +09:00
parent af38681293
commit b01497e391

View File

@ -1,296 +1,327 @@
var dirname = __dirname; var dirname = __dirname
var dir = "file://" + __dirname; var dir = 'file://' + __dirname
var base = dir + "/view/"; var base = dir + '/view/'
// Electronのモジュール // Electronのモジュール
const electron = require("electron"); const electron = require('electron')
const fs = require("fs"); const fs = require('fs')
const language = require("./main/language.js"); const language = require('./main/language.js')
const css = require("./main/css.js"); const css = require('./main/css.js')
const dl = require("./main/dl.js"); const dl = require('./main/dl.js')
const img = require("./main/img.js"); const img = require('./main/img.js')
const np = require("./main/np.js"); const np = require('./main/np.js')
const systemFunc = require("./main/system.js"); const systemFunc = require('./main/system.js')
const Menu = electron.Menu; const Menu = electron.Menu
const join = require("path").join; const join = require('path').join
// アプリケーションをコントロールするモジュール // アプリケーションをコントロールするモジュール
const app = electron.app; const app = electron.app
// ウィンドウを作成するモジュール // ウィンドウを作成するモジュール
const BrowserWindow = electron.BrowserWindow; const BrowserWindow = electron.BrowserWindow
// メインウィンドウはGCされないようにグローバル宣言 // メインウィンドウはGCされないようにグローバル宣言
let mainWindow; let mainWindow
// アプリが多重起動しないようにする // アプリが多重起動しないようにする
const gotTheLock = app.requestSingleInstanceLock(); const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) { if (!gotTheLock) {
app.quit(); app.quit()
} else { } else {
app.on("second-instance", () => { app.on('second-instance', () => {
// 多重起動を試みた場合、既に存在するウィンドウにフォーカスを移す // 多重起動を試みた場合、既に存在するウィンドウにフォーカスを移す
// Someone tried to run a second instance, we should focus our window. // Someone tried to run a second instance, we should focus our window.
if (mainWindow) { if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore(); if (mainWindow.isMinimized()) mainWindow.restore()
mainWindow.focus(); mainWindow.focus()
} }
}); })
} }
if (process.argv.indexOf("--dev") === -1) { if (process.argv.indexOf('--dev') === -1) {
var packaged = true; var packaged = true
} else { } else {
var packaged = false; var packaged = false
console.log("||\\\\\\ \n" + "|||| \\\\\\\\ \n" + "|||| \\\\\\\\ \n" + "|||| Am I a \\\\\\\\ \n" + "|||| cat? ^ ^ \\\\\\\\\\ _____ _ ____ _ \n" + "|||| (.-.) \\\\\\\\\\ |_ _| |__ ___| _ \\ ___ ___| | __\n" + "|||| ___> ) ||||| | | | '_ \\ / _ \\ | | |/ _ \\/ __| |/ /\n" + "|||| < _ _) ////// | | | | | | __/ |_| | __/__ \\ < \n" + "|||| |_||_| ///// |_| |_| |_|\\___|____/ \\___||___/_|\\_\\ \n" + "|||| ///// \n" + "|||| /////\n" + "|||| /////\n" + "||||//////"); console.log(
console.log("Welcome!"); '||\\\\\\ \n' +
'|||| \\\\\\\\ \n' +
'|||| \\\\\\\\ \n' +
'|||| Am I a \\\\\\\\ \n' +
'|||| cat? ^ ^ \\\\\\\\\\ _____ _ ____ _ \n' +
'|||| (.-.) \\\\\\\\\\ |_ _| |__ ___| _ \\ ___ ___| | __\n' +
"|||| ___> ) ||||| | | | '_ \\ / _ \\ | | |/ _ \\/ __| |/ /\n" +
'|||| < _ _) ////// | | | | | | __/ |_| | __/__ \\ < \n' +
'|||| |_||_| ///// |_| |_| |_|\\___|____/ \\___||___/_|\\_\\ \n' +
'|||| ///// \n' +
'|||| /////\n' +
'|||| /////\n' +
'||||//////'
)
console.log('Welcome!')
} }
var info_path = join(app.getPath("userData"), "window-size.json"); var info_path = join(app.getPath('userData'), 'window-size.json')
var max_info_path = join(app.getPath("userData"), "max-window-size.json"); var max_info_path = join(app.getPath('userData'), 'max-window-size.json')
var lang_path = join(app.getPath("userData"), "language"); var lang_path = join(app.getPath('userData'), 'language')
var ha_path = join(app.getPath("userData"), "hardwareAcceleration"); var ha_path = join(app.getPath('userData'), 'hardwareAcceleration')
var ua_path = join(app.getPath("userData"), "useragent"); var ua_path = join(app.getPath('userData'), 'useragent')
try { try {
fs.readFileSync(ha_path, "utf8"); fs.readFileSync(ha_path, 'utf8')
app.disableHardwareAcceleration(); app.disableHardwareAcceleration()
if (!packaged) console.log("disabled: Hardware Acceleration"); if (!packaged) console.log('disabled: Hardware Acceleration')
} catch { } catch {
if (!packaged) console.log("enabled: Hardware Acceleration"); if (!packaged) console.log('enabled: Hardware Acceleration')
} }
var window_size; var window_size
try { try {
window_size = JSON.parse(fs.readFileSync(info_path, "utf8")); window_size = JSON.parse(fs.readFileSync(info_path, 'utf8'))
} catch (e) { } catch (e) {
window_size = { window_size = {
width: 1000, width: 1000,
height: 750 height: 750
}; // デフォルトバリュー } // デフォルトバリュー
} }
var max_window_size; var max_window_size
try { try {
max_window_size = JSON.parse(fs.readFileSync(max_info_path, "utf8")); max_window_size = JSON.parse(fs.readFileSync(max_info_path, 'utf8'))
} catch (e) { } catch (e) {
max_window_size = { max_window_size = {
width: "string", width: 'string',
height: "string", height: 'string',
x: "string", x: 'string',
y: "string" y: 'string'
}; // デフォルトバリュー } // デフォルトバリュー
} }
function isFile(file) { function isFile(file) {
try { try {
fs.statSync(file); fs.statSync(file)
return true; return true
} catch (err) { } catch (err) {
if (err.code === "ENOENT") return false; if (err.code === 'ENOENT') return false
} }
} }
// 全てのウィンドウが閉じたら終了 // 全てのウィンドウが閉じたら終了
app.on("window-all-closed", function() { app.on('window-all-closed', function() {
electron.session.defaultSession.clearCache(() => {}); electron.session.defaultSession.clearCache(() => {})
app.quit(); app.quit()
}); })
function createWindow() { function createWindow() {
if (isFile(lang_path)) { if (isFile(lang_path)) {
var lang = fs.readFileSync(lang_path, "utf8"); var lang = fs.readFileSync(lang_path, 'utf8')
} else { } else {
var langs = app.getLocale(); var langs = app.getLocale()
console.log(langs); console.log(langs)
if (~langs.indexOf("ja")) { if (~langs.indexOf('ja')) {
lang = "ja"; lang = 'ja'
} else if (~langs.indexOf("de")) { } else if (~langs.indexOf('de')) {
lang = "de"; lang = 'de'
} else if (~langs.indexOf("cs")) { } else if (~langs.indexOf('cs')) {
lang = "cs"; lang = 'cs'
} else if (~langs.indexOf("bg")) { } else if (~langs.indexOf('bg')) {
lang = "bg"; lang = 'bg'
} else { } else {
lang = "en"; lang = 'en'
} }
fs.mkdir(app.getPath("userData"), function(err) { fs.mkdir(app.getPath('userData'), function(err) {
fs.writeFileSync(lang_path, lang); fs.writeFileSync(lang_path, lang)
}); })
} }
if (!packaged) console.log("your lang:" + app.getLocale()); if (!packaged) console.log('your lang:' + app.getLocale())
if (!packaged) console.log("launch:" + lang); if (!packaged) console.log('launch:' + lang)
// メイン画面の表示。ウィンドウの幅、高さを指定できる // メイン画面の表示。ウィンドウの幅、高さを指定できる
var platform = process.platform; var platform = process.platform
var bit = process.arch; var bit = process.arch
if (platform == "linux") { if (platform == 'linux') {
var arg = { var arg = {
webPreferences: { webPreferences: {
webviewTag: true, webviewTag: true,
nodeIntegration: false, nodeIntegration: false,
contextIsolation: true, contextIsolation: true,
preload: join(__dirname, "js", "platform", "preload.js") preload: join(__dirname, 'js', 'platform', 'preload.js')
}, },
width: window_size.width, width: window_size.width,
height: window_size.height, height: window_size.height,
x: window_size.x, x: window_size.x,
y: window_size.y, y: window_size.y,
icon: __dirname + "/desk.png", icon: __dirname + '/desk.png',
show: false show: false
};
} else if (platform == "win32") {
var arg = {
webPreferences: {
webviewTag: true,
nodeIntegration: false,
contextIsolation: true,
preload: join(__dirname, "js", "platform", "preload.js")
},
width: window_size.width,
height: window_size.height,
x: window_size.x,
y: window_size.y,
simpleFullscreen: true,
show: false
};
} else if (platform == "darwin") {
var arg = {
webPreferences: {
webviewTag: true,
nodeIntegration: false,
contextIsolation: true,
preload: join(__dirname, "js", "platform", "preload.js")
},
width: window_size.width,
height: window_size.height,
x: window_size.x,
y: window_size.y,
simpleFullscreen: true,
show: false
};
} }
mainWindow = new BrowserWindow(arg); } else if (platform == 'win32') {
mainWindow.once("page-title-updated", () => { var arg = {
mainWindow.show(); webPreferences: {
webviewTag: true,
nodeIntegration: false,
contextIsolation: true,
preload: join(__dirname, 'js', 'platform', 'preload.js')
},
width: window_size.width,
height: window_size.height,
x: window_size.x,
y: window_size.y,
simpleFullscreen: true,
show: false
}
} else if (platform == 'darwin') {
var arg = {
webPreferences: {
webviewTag: true,
nodeIntegration: false,
contextIsolation: true,
preload: join(__dirname, 'js', 'platform', 'preload.js')
},
width: window_size.width,
height: window_size.height,
x: window_size.x,
y: window_size.y,
simpleFullscreen: true,
show: false
}
}
mainWindow = new BrowserWindow(arg)
mainWindow.once('page-title-updated', () => {
mainWindow.show()
if (window_size.max) { if (window_size.max) {
mainWindow.maximize(); mainWindow.maximize()
} }
}); })
if (!packaged) mainWindow.toggleDevTools(); if (!packaged) mainWindow.toggleDevTools()
electron.session.defaultSession.clearCache(() => {}); electron.session.defaultSession.clearCache(() => {})
if (process.argv) { if (process.argv) {
if (process.argv[1]) { if (process.argv[1]) {
var m = process.argv[1].match(/([a-zA-Z0-9]+)\/\?[a-zA-Z-0-9]+=(.+)/); var m = process.argv[1].match(/([a-zA-Z0-9]+)\/\?[a-zA-Z-0-9]+=(.+)/)
if (m) { if (m) {
var mode = m[1]; var mode = m[1]
var code = m[2]; var code = m[2]
var plus = "?mode=" + mode + "&code=" + code; var plus = '?mode=' + mode + '&code=' + code
} else { } else {
var plus = ""; var plus = ''
} }
} else { } else {
var plus = ""; var plus = ''
} }
} else { } else {
var plus = ""; var plus = ''
} }
var ua; var ua
try { try {
ua = fs.readFileSync(ua_path, "utf8"); ua = fs.readFileSync(ua_path, 'utf8')
} catch (e) { } catch (e) {
//default UA Example: //default UA Example:
// Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) thedesk/18.11.3 Chrome/76.0.3809.146 Electron/6.0.12 Safari/537.36 // Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) thedesk/18.11.3 Chrome/76.0.3809.146 Electron/6.0.12 Safari/537.36
const crypto = require("crypto"); const crypto = require('crypto')
const N = 100; const N = 100
var ua = var ua =
"Mastodon client: " + 'Mastodon client: ' +
crypto crypto
.randomBytes(N) .randomBytes(N)
.toString("base64") .toString('base64')
.substring(0, N); .substring(0, N)
} }
mainWindow.loadURL(base + lang + "/index.html" + plus, { userAgent: ua }); mainWindow.loadURL(base + lang + '/index.html' + plus, { userAgent: ua })
if (!window_size.x && !window_size.y) { if (!window_size.x && !window_size.y) {
mainWindow.center(); mainWindow.center()
} }
// ウィンドウが閉じられたらアプリも終了 // ウィンドウが閉じられたらアプリも終了
mainWindow.on("closed", function() { mainWindow.on('closed', function() {
electron.ipcMain.removeAllListeners(); electron.ipcMain.removeAllListeners()
mainWindow = null; mainWindow = null
}); })
closeArg = false; closeArg = false
mainWindow.on("close", function(e, arg) { mainWindow.on('close', function(e, arg) {
writePos(mainWindow); writePos(mainWindow)
if (!closeArg) { if (!closeArg) {
e.preventDefault(); e.preventDefault()
} }
const promise = new Promise(function(resolve) { const promise = new Promise(function(resolve) {
mainWindow.webContents.send("asReadEnd", ""); mainWindow.webContents.send('asReadEnd', '')
setTimeout(function() { setTimeout(function() {
resolve(); resolve()
}, 3000); }, 3000)
}); })
promise.then(function(response) { promise.then(function(response) {
closeArg = true; closeArg = true
mainWindow.close(); mainWindow.close()
}); })
}); })
electron.ipcMain.on("sendMarkersComplete", function(e, arg) { electron.ipcMain.on('sendMarkersComplete', function(e, arg) {
closeArg = true; closeArg = true
mainWindow.close(); mainWindow.close()
}); })
function writePos(mainWindow) { function writePos(mainWindow) {
if (max_window_size.width == mainWindow.getBounds().width && max_window_size.height == mainWindow.getBounds().height && max_window_size.x == mainWindow.getBounds().x && max_window_size.y == mainWindow.getBounds().y) { if (
var size = { width: mainWindow.getBounds().width, height: mainWindow.getBounds().height, x: mainWindow.getBounds().x, y: mainWindow.getBounds().y, max: true }; max_window_size.width == mainWindow.getBounds().width &&
max_window_size.height == mainWindow.getBounds().height &&
max_window_size.x == mainWindow.getBounds().x &&
max_window_size.y == mainWindow.getBounds().y
) {
var size = {
width: mainWindow.getBounds().width,
height: mainWindow.getBounds().height,
x: mainWindow.getBounds().x,
y: mainWindow.getBounds().y,
max: true
}
} else { } else {
var size = { width: mainWindow.getBounds().width, height: mainWindow.getBounds().height, x: mainWindow.getBounds().x, y: mainWindow.getBounds().y }; var size = {
width: mainWindow.getBounds().width,
height: mainWindow.getBounds().height,
x: mainWindow.getBounds().x,
y: mainWindow.getBounds().y
} }
fs.writeFileSync(info_path, JSON.stringify(size));
} }
mainWindow.on("maximize", function() { fs.writeFileSync(info_path, JSON.stringify(size))
writePos(mainWindow); }
fs.writeFileSync(max_info_path, JSON.stringify(mainWindow.getBounds())); mainWindow.on('maximize', function() {
}); writePos(mainWindow)
mainWindow.on("minimize", function() { fs.writeFileSync(max_info_path, JSON.stringify(mainWindow.getBounds()))
writePos(mainWindow); })
mainWindow.webContents.send("asRead", ""); mainWindow.on('minimize', function() {
}); writePos(mainWindow)
mainWindow.webContents.send('asRead', '')
})
var platform = process.platform; var platform = process.platform
var bit = process.arch; var bit = process.arch
Menu.setApplicationMenu(Menu.buildFromTemplate(language.template(lang, mainWindow, packaged, dir, dirname))); Menu.setApplicationMenu(
Menu.buildFromTemplate(language.template(lang, mainWindow, packaged, dir, dirname))
)
//CSS //CSS
css.css(mainWindow); css.css(mainWindow)
//アップデータとダウンロード //アップデータとダウンロード
dl.dl(mainWindow, lang_path, base, dirname); dl.dl(mainWindow, lang_path, base, dirname)
//画像選択と画像処理 //画像選択と画像処理
img.img(mainWindow, dir); img.img(mainWindow, dir)
//NowPlaying //NowPlaying
np.TheDeskNowPlaying(mainWindow); np.TheDeskNowPlaying(mainWindow)
//その他system //その他system
systemFunc.system(mainWindow, dir, lang, dirname); systemFunc.system(mainWindow, dir, lang, dirname)
setInterval(function() { setInterval(function() {
mouseTrack(mainWindow); mouseTrack(mainWindow)
}, 1000); }, 1000)
} }
var x = 0; var x = 0
var y = 0; var y = 0
var unchanged = 0; var unchanged = 0
var locked = false; var locked = false
function mouseTrack(mainWindow) { function mouseTrack(mainWindow) {
let mousePos = electron.screen.getCursorScreenPoint(); let mousePos = electron.screen.getCursorScreenPoint()
let xNow = mousePos.x; let xNow = mousePos.x
let yNow = mousePos.x; let yNow = mousePos.x
if (x != xNow || y != yNow) { if (x != xNow || y != yNow) {
unchanged = 0; unchanged = 0
locked = false; locked = false
} else { } else {
unchanged++; unchanged++
if (unchanged > 60 && !locked) { if (unchanged > 60 && !locked) {
unchanged = 0; unchanged = 0
locked = true; locked = true
mainWindow.webContents.send("asRead", ""); mainWindow.webContents.send('asRead', '')
} }
} }
x = xNow; x = xNow
y = yNow; y = yNow
} }
// Electronの初期化完了後に実行 // Electronの初期化完了後に実行
app.on("ready", createWindow); app.on('ready', createWindow)
var onError = function(err, response) { var onError = function(err, response) {
console.error(err, response); console.error(err, response)
}; }
app.setAsDefaultProtocolClient("thedesk"); app.setAsDefaultProtocolClient('thedesk')