eslint changes

This commit is contained in:
SpikeHD 2022-05-02 22:44:57 -07:00
parent f0210cdbfe
commit eae6cc087b
12 changed files with 818 additions and 786 deletions

29
.eslintrc.json Normal file
View File

@ -0,0 +1,29 @@
{
"env": {
"browser": true,
"commonjs": true,
"es2021": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 13
},
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
}

View File

@ -9,5 +9,8 @@
"build-win": ".\\build_win.cmd", "build-win": ".\\build_win.cmd",
"build-linux": "./build.sh", "build-linux": "./build.sh",
"build": "echo !! Run build-win or build-linux to build for your platform !!\n" "build": "echo !! Run build-win or build-linux to build for your platform !!\n"
},
"devDependencies": {
"eslint": "^8.14.0"
} }
} }

View File

@ -9,37 +9,37 @@ async function displayRegisterAlert(message, type, cooldown = null) {
} }
function displayAlert(message, type, cooldown, name) { function displayAlert(message, type, cooldown, name) {
const elm = document.getElementById(`${name}Alert`); const elm = document.getElementById(`${name}Alert`)
const text = document.getElementById(`${name}AlertText`); const text = document.getElementById(`${name}AlertText`)
elm.style.removeProperty('display'); elm.style.removeProperty('display')
// Remove classification classes // Remove classification classes
elm.classList.remove('error'); elm.classList.remove('error')
elm.classList.remove('success'); elm.classList.remove('success')
elm.classList.remove('warn'); elm.classList.remove('warn')
switch(type) { switch(type) {
case 'error': case 'error':
elm.classList.add('error'); elm.classList.add('error')
break; break
case 'success': case 'success':
elm.classList.add('success'); elm.classList.add('success')
break; break
case 'warn': case 'warn':
default: default:
elm.classList.add('warn'); elm.classList.add('warn')
break; break
} }
text.innerText = message; text.innerText = message
clearTimeout(alertTimeout) clearTimeout(alertTimeout)
// Disappear after cooldown // Disappear after cooldown
alertTimeout = setTimeout(() => { alertTimeout = setTimeout(() => {
elm.style.display = 'none'; elm.style.display = 'none'
}, cooldown || alertCooldown) }, cooldown || alertCooldown)
} }

View File

@ -1,5 +1,5 @@
async function clearGCInstallation() { async function clearGCInstallation() {
Neutralino.os.execCommand(`del /s /q "./gc"`) Neutralino.os.execCommand('del /s /q "./gc"')
} }
async function setDownloadButtonsToLoading() { async function setDownloadButtonsToLoading() {

View File

@ -1,6 +1,6 @@
Neutralino.init(); Neutralino.init()
let localeObj; let localeObj
const filesystem = Neutralino.filesystem const filesystem = Neutralino.filesystem
const createCmdWindow = async (command) => { const createCmdWindow = async (command) => {
Neutralino.os.execCommand(`cmd.exe /c start "" ${command}`, { background: true }) Neutralino.os.execCommand(`cmd.exe /c start "" ${command}`, { background: true })
@ -46,7 +46,7 @@ async function handleGameNotSet() {
document.querySelector('#gamePath').innerHTML = localeObj.folderNotSet document.querySelector('#gamePath').innerHTML = localeObj.folderNotSet
// Set official server background to default // Set official server background to default
document.querySelector('#firstPanel').style.backgroundImage = `url("../bg/private/default.png")` document.querySelector('#firstPanel').style.backgroundImage = 'url("../bg/private/default.png")'
const offBtn = document.querySelector('#playOfficial') const offBtn = document.querySelector('#playOfficial')
const privBtn = document.querySelector('#playPrivate') const privBtn = document.querySelector('#playPrivate')
@ -106,7 +106,7 @@ async function setBackgroundImage() {
const servImage = servImages[Math.floor(Math.random() * servImages.length)].entry const servImage = servImages[Math.floor(Math.random() * servImages.length)].entry
// Set default image, it will change if the bg folder exists // Set default image, it will change if the bg folder exists
document.querySelector('#firstPanel').style.backgroundImage = `url("https://webstatic.hoyoverse.com/upload/event/2020/11/04/7fd661b5184e1734f91f628b6f89a31f_7367318474207189623.png")` document.querySelector('#firstPanel').style.backgroundImage = 'url("https://webstatic.hoyoverse.com/upload/event/2020/11/04/7fd661b5184e1734f91f628b6f89a31f_7367318474207189623.png")'
// Set the private background image // Set the private background image
document.querySelector('#secondPanel').style.backgroundImage = `url("../bg/private/${privImage}")` document.querySelector('#secondPanel').style.backgroundImage = `url("../bg/private/${privImage}")`
@ -318,7 +318,7 @@ async function openLogin() {
const config = await getCfg() const config = await getCfg()
const useHttps = config.useHttps const useHttps = config.useHttps
const url = `${useHttps ? 'https' : 'http'}://${ip}:${port}`; const url = `${useHttps ? 'https' : 'http'}://${ip}:${port}`
// Check if we even need to authenticate // Check if we even need to authenticate
try { try {
@ -380,10 +380,10 @@ async function checkForUpdatesAndShow() {
// Version mismatch? Update! // Version mismatch? Update!
if (manifest?.version !== NL_APPVERSION) { if (manifest?.version !== NL_APPVERSION) {
subtitle.innerHTML = "New update available!" subtitle.innerHTML = 'New update available!'
updateBtn.classList.remove('disabled') updateBtn.classList.remove('disabled')
} else { } else {
subtitle.innerHTML = "You are on the latest version! :)" subtitle.innerHTML = 'You are on the latest version! :)'
updateBtn.classList.add('disabled') updateBtn.classList.add('disabled')
} }
} }
@ -411,7 +411,7 @@ async function setGameExe() {
] ]
}) })
if (!gameExe[0]) return; if (!gameExe[0]) return
// Set the folder in our configuration // Set the folder in our configuration
const config = await getCfg() const config = await getCfg()
@ -434,7 +434,7 @@ async function setGrasscutterFolder() {
] ]
}) })
if (!folder[0]) return; if (!folder[0]) return
// Set the folder in our configuration // Set the folder in our configuration
const config = await getCfg() const config = await getCfg()

View File

@ -2,60 +2,60 @@
* Toggle the login section * Toggle the login section
*/ */
async function setLoginSection() { async function setLoginSection() {
const title = document.getElementById('loginSectionTitle'); const title = document.getElementById('loginSectionTitle')
const altTitle = document.getElementById('registerSectionTitle'); const altTitle = document.getElementById('registerSectionTitle')
const loginSection = document.getElementById('loginPopupContentBody'); const loginSection = document.getElementById('loginPopupContentBody')
const registerSection = document.getElementById('registerPopupContentBody'); const registerSection = document.getElementById('registerPopupContentBody')
title.classList.add('selectedTitle') title.classList.add('selectedTitle')
altTitle.classList.remove('selectedTitle') altTitle.classList.remove('selectedTitle')
loginSection.style.removeProperty('display'); loginSection.style.removeProperty('display')
registerSection.style.display = 'none'; registerSection.style.display = 'none'
} }
/** /**
* Toggle the register section * Toggle the register section
*/ */
async function setRegisterSection(fromLogin = false) { async function setRegisterSection(fromLogin = false) {
const title = document.getElementById('registerSectionTitle'); const title = document.getElementById('registerSectionTitle')
const altTitle = document.getElementById('loginSectionTitle'); const altTitle = document.getElementById('loginSectionTitle')
const loginSection = document.getElementById('loginPopupContentBody'); const loginSection = document.getElementById('loginPopupContentBody')
const registerSection = document.getElementById('registerPopupContentBody'); const registerSection = document.getElementById('registerPopupContentBody')
title.classList.add('selectedTitle') title.classList.add('selectedTitle')
altTitle.classList.remove('selectedTitle') altTitle.classList.remove('selectedTitle')
loginSection.style.display = 'none'; loginSection.style.display = 'none'
registerSection.style.removeProperty('display'); registerSection.style.removeProperty('display')
if (fromLogin) { if (fromLogin) {
// Take the values from the login section and put them in the register section // Take the values from the login section and put them in the register section
const loginUsername = document.getElementById('loginUsername').value; const loginUsername = document.getElementById('loginUsername').value
const loginPassword = document.getElementById('loginPassword').value; const loginPassword = document.getElementById('loginPassword').value
document.getElementById('registerUsername').value = loginUsername; document.getElementById('registerUsername').value = loginUsername
document.getElementById('registerPassword').value = loginPassword; document.getElementById('registerPassword').value = loginPassword
} }
} }
function parseJwt(token) { function parseJwt(token) {
const base64Url = token.split('.')[1]; const base64Url = token.split('.')[1]
const base64 = base64Url.replace('-', '+').replace('_', '/'); const base64 = base64Url.replace('-', '+').replace('_', '/')
return JSON.parse(window.atob(base64)); return JSON.parse(window.atob(base64))
} }
/** /**
* Attempt login and launch game * Attempt login and launch game
*/ */
async function login() { async function login() {
const username = document.getElementById('loginUsername').value; const username = document.getElementById('loginUsername').value
const password = document.getElementById('loginPassword').value; const password = document.getElementById('loginPassword').value
const ip = document.getElementById('ip').value; const ip = document.getElementById('ip').value
const port = document.getElementById('port').value || '443'; const port = document.getElementById('port').value || '443'
const config = await getCfg(); const config = await getCfg()
const useHttps = config.useHttps; const useHttps = config.useHttps
const url = `${useHttps ? 'https' : 'http'}://${ip}:${port}`; const url = `${useHttps ? 'https' : 'http'}://${ip}:${port}`
const reqBody = { const reqBody = {
username, username,
@ -66,35 +66,35 @@ async function login() {
switch(data.message) { switch(data.message) {
case 'INVALID_ACCOUNT': case 'INVALID_ACCOUNT':
displayLoginAlert(localeObj.alertInvalid || 'Invalid username or password', 'error'); displayLoginAlert(localeObj.alertInvalid || 'Invalid username or password', 'error')
break; break
case 'NO_PASSWORD': case 'NO_PASSWORD':
// No account password, create one with change password // No account password, create one with change password
displayLoginAlert(localeObj.alertNoPass || 'No password set, please change password', 'warn'); displayLoginAlert(localeObj.alertNoPass || 'No password set, please change password', 'warn')
break; break
case 'UNKNOWN': case 'UNKNOWN':
// Unknown error, contact server owner // Unknown error, contact server owner
displayLoginAlert(localeObj.alertUnknown || 'Unknown error, contact server owner', 'error'); displayLoginAlert(localeObj.alertUnknown || 'Unknown error, contact server owner', 'error')
break; break
case undefined: case undefined:
case null: case null:
case 'AUTH_DISABLED': case 'AUTH_DISABLED':
// Authentication is disabled, we can just connect the user // Authentication is disabled, we can just connect the user
displayLoginAlert(localeObj.alertAuthNoLogin || 'Authentication is disabled, no need to log in!', 'warn'); displayLoginAlert(localeObj.alertAuthNoLogin || 'Authentication is disabled, no need to log in!', 'warn')
launchPrivate(); launchPrivate()
break; break
default: default:
// Success! Copy the JWT token to their clipboard // Success! Copy the JWT token to their clipboard
const tkData = parseJwt(data.jwt) const tkData = parseJwt(data.jwt)
await Neutralino.clipboard.writeText(tkData.token) await Neutralino.clipboard.writeText(tkData.token)
displayLoginAlert(localeObj.alertLoginSuccess || 'Login successful! Token copied to clipboard. Paste this token into the username field of the game to log in.', 'success', 8000); displayLoginAlert(localeObj.alertLoginSuccess || 'Login successful! Token copied to clipboard. Paste this token into the username field of the game to log in.', 'success', 8000)
launchPrivate() launchPrivate()
break; break
} }
} }
@ -102,14 +102,14 @@ async function login() {
* Attempt registration, do not launch game * Attempt registration, do not launch game
*/ */
async function register() { async function register() {
const username = document.getElementById('registerUsername').value; const username = document.getElementById('registerUsername').value
const password = document.getElementById('registerPassword').value; const password = document.getElementById('registerPassword').value
const password_confirmation = document.getElementById('registerPasswordConfirm').value; const password_confirmation = document.getElementById('registerPasswordConfirm').value
const ip = document.getElementById('ip').value; const ip = document.getElementById('ip').value
const port = document.getElementById('port').value || '443'; const port = document.getElementById('port').value || '443'
const config = await getCfg(); const config = await getCfg()
const useHttps = config.useHttps; const useHttps = config.useHttps
const url = `${useHttps ? 'https' : 'http'}://${ip}:${port}`; const url = `${useHttps ? 'https' : 'http'}://${ip}:${port}`
const reqBody = { const reqBody = {
username, username,
@ -122,33 +122,33 @@ async function register() {
switch(data.message) { switch(data.message) {
case 'USERNAME_TAKEN': case 'USERNAME_TAKEN':
// Username is taken // Username is taken
displayRegisterAlert(localeObj.alertUserTaken || 'Username is taken', 'error'); displayRegisterAlert(localeObj.alertUserTaken || 'Username is taken', 'error')
break; break
case 'PASSWORD_MISMATCH': case 'PASSWORD_MISMATCH':
// The password and password confirmation do not match // The password and password confirmation do not match
displayRegisterAlert(localStorage.alertPassMismatch || 'Password and password confirmation do not match', 'error'); displayRegisterAlert(localStorage.alertPassMismatch || 'Password and password confirmation do not match', 'error')
break; break
case 'UNKNOWN': case 'UNKNOWN':
// Unknown error, contact server owner // Unknown error, contact server owner
displayRegisterAlert(localeObj.alertUnknown || 'Unknown error, contact server owner', 'error'); displayRegisterAlert(localeObj.alertUnknown || 'Unknown error, contact server owner', 'error')
break; break
case undefined: case undefined:
case null: case null:
case 'AUTH_DISABLED': case 'AUTH_DISABLED':
// Authentication is disabled, we can just connect the user // Authentication is disabled, we can just connect the user
displayRegisterAlert(localeObj.alertAuthNoRegister || 'Authentication is disabled, no need to register!', 'warn'); displayRegisterAlert(localeObj.alertAuthNoRegister || 'Authentication is disabled, no need to register!', 'warn')
break; break
default: default:
// Success!! Bring them to the login screen and auto-input their username // Success!! Bring them to the login screen and auto-input their username
const loginUsername = document.getElementById('loginUsername'); const loginUsername = document.getElementById('loginUsername')
loginUsername.value = username; loginUsername.value = username
setLoginSection(); setLoginSection()
displayLoginAlert(localeObj.alertRegisterSuccess || 'Registration successful!', 'success', 5000); displayLoginAlert(localeObj.alertRegisterSuccess || 'Registration successful!', 'success', 5000)
break; break
} }
} }

View File

@ -4,10 +4,10 @@
* should be done here to ensure DOM contents are loaded. * should be done here to ensure DOM contents are loaded.
*/ */
document.addEventListener('DOMContentLoaded', async () => { document.addEventListener('DOMContentLoaded', async () => {
displayUpdate(); displayUpdate()
setBackgroundImage(); setBackgroundImage()
displayGameFolder(); displayGameFolder()
displayServerFolder(); displayServerFolder()
// Set title version // Set title version
document.querySelector('#version').innerHTML = NL_APPVERSION document.querySelector('#version').innerHTML = NL_APPVERSION
@ -35,7 +35,7 @@
} }
// Exit favorites list and settings panel when clicking outside of it // Exit favorites list and settings panel when clicking outside of it
window.addEventListener("click", function(e) { window.addEventListener('click', function(e) {
const favList = document.querySelector('#ipList') const favList = document.querySelector('#ipList')
const settingsPanel = document.querySelector('#settingsPanel') const settingsPanel = document.querySelector('#settingsPanel')
const downloadPanel = document.querySelector('#downloadPanel') const downloadPanel = document.querySelector('#downloadPanel')
@ -74,7 +74,7 @@
downloadPanel.style.display = 'none' downloadPanel.style.display = 'none'
} }
} }
}); })
// Ensure we do the translation at the very end, after everything else has loaded // Ensure we do the translation at the very end, after everything else has loaded
await doTranslation() await doTranslation()

View File

@ -1,25 +1,25 @@
// https://stackoverflow.com/questions/67971689/positioning-the-borderless-window-in-neutralino-js // https://stackoverflow.com/questions/67971689/positioning-the-borderless-window-in-neutralino-js
// had to use this since the in-built function breaks the close and minimize buttons // had to use this since the in-built function breaks the close and minimize buttons
let dragging = false, ratio = 1, posX, posY; let dragging = false, ratio = 1, posX, posY
let draggable; let draggable
document.addEventListener('DOMContentLoaded', async () => { document.addEventListener('DOMContentLoaded', async () => {
draggable = document.getElementById('controlBar'); draggable = document.getElementById('controlBar')
// Listen to hovers // Listen to hovers
draggable.onmousedown = function (e) { draggable.onmousedown = function (e) {
ratio = window.devicePixelRatio ratio = window.devicePixelRatio
posX = e.pageX * ratio, posY = e.pageY * ratio; posX = e.pageX * ratio, posY = e.pageY * ratio
dragging = true; dragging = true
} }
// Patch for monitors with scaling enabled, allows them to detach from the titlebar anywhere // Patch for monitors with scaling enabled, allows them to detach from the titlebar anywhere
window.onmouseup = function (e) { window.onmouseup = function (e) {
dragging = false; dragging = false
} }
document.onmousemove = function (e) { document.onmousemove = function (e) {
if (dragging) Neutralino.window.move(e.screenX * ratio - posX, e.screenY * ratio - posY); if (dragging) Neutralino.window.move(e.screenX * ratio - posX, e.screenY * ratio - posY)
} }
}) })