GrassClipper/resources/js/helpers.js

170 lines
4.0 KiB
JavaScript
Raw Normal View History

/**
* Get configuration
*
* @returns {Promise<string>}
*/
2022-05-03 05:44:57 +00:00
async function getCfg() {
2022-05-03 05:48:03 +00:00
const defaultConf = {
gameexe: '',
serverFolder: '',
lastConnect: '',
enableKillswitch: false,
serverLaunchPanel: false,
language: 'en',
useHttps: true,
grasscutterBranch: '',
}
const cfgStr = await Neutralino.storage.getData('config').catch(e => {
// The data isn't set, so this is our first time opening
2022-05-03 05:48:03 +00:00
Neutralino.storage.setData('config', JSON.stringify(defaultConf))
2022-05-03 05:48:03 +00:00
// Show the first time notice if there is no config
document.querySelector('#firstTimeNotice').style.display = 'block'
})
2022-05-03 05:48:03 +00:00
const config = cfgStr ? JSON.parse(cfgStr) : defaultConf
2022-05-03 05:48:03 +00:00
return config
}
/**
* Get the list of favorite IPs
*
* @returns {Promise<string[]>}
*/
2022-05-03 05:44:57 +00:00
async function getFavIps() {
2022-05-03 05:48:03 +00:00
const ipStr = await Neutralino.storage.getData('favorites').catch(e => {
// The data isn't set, so this is our first time opening
2022-05-03 05:48:03 +00:00
Neutralino.storage.setData('favorites', JSON.stringify([]))
})
2022-05-03 05:48:03 +00:00
const ipArr = ipStr ? JSON.parse(ipStr) : []
2022-05-03 05:48:03 +00:00
return ipArr
}
async function proxyIsInstalled() {
2022-05-03 05:48:03 +00:00
// Check if the proxy server is installed
const curDirList = await filesystem.readDirectory(NL_CWD)
2022-05-03 05:48:03 +00:00
if (curDirList.find(f => f.entry === 'ext')) {
const extFiles = await filesystem.readDirectory(NL_CWD + '/ext')
2022-05-03 05:48:03 +00:00
if (extFiles.find(f => f.entry === 'mitmdump.exe')) {
return true
}
2022-05-03 05:48:03 +00:00
}
2022-05-03 05:48:03 +00:00
return false
}
2022-04-30 08:56:00 +00:00
async function checkForUpdates() {
2022-05-03 05:48:03 +00:00
const url = 'https://api.github.com/repos/Grasscutters/GrassClipper/releases/latest'
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
const { data } = await axios.get(url)
const latest = data.tag_name
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
return latest
2022-04-30 08:56:00 +00:00
}
async function displayUpdate() {
2022-05-03 05:48:03 +00:00
const latest = await checkForUpdates()
const versionDisplay = document.querySelector('#newestVersion')
const notif = document.querySelector('#downloadNotif')
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
if (latest === `v${NL_APPVERSION}`) return
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
versionDisplay.innerText = latest
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
notif.classList.add('displayed')
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
setTimeout(() => {
notif.classList.remove('displayed')
}, 5000)
2022-04-30 08:56:00 +00:00
}
async function openLatestDownload() {
2022-05-03 05:48:03 +00:00
const downloadLink = 'https://github.com/Grasscutters/GrassClipper/releases/latest/'
2022-04-30 08:56:00 +00:00
2022-05-03 05:48:03 +00:00
Neutralino.os.open(downloadLink)
2022-04-30 08:56:00 +00:00
}
async function openGameFolder() {
2022-05-03 05:48:03 +00:00
const config = await getCfg()
const folder = config.gameexe.match(/.*\\|.*\//g, '')
2022-05-04 04:14:02 +00:00
2022-05-06 00:29:23 +00:00
if (folder.length > 0) openInExplorer(folder[0].replace(/\//g, '\\'))
}
async function openGrasscutterFolder() {
2022-05-03 05:48:03 +00:00
const config = await getCfg()
const folder = config.serverFolder.match(/.*\\|.*\//g, '')
2022-05-06 00:29:23 +00:00
if (folder.length > 0) openInExplorer(folder[0].replace(/\//g, '\\'))
}
2022-05-04 04:06:01 +00:00
// https://www.jimzhao.us/2015/09/javascript-detect-chinese-character.html
function hasChineseChars(str) {
2022-05-04 04:14:02 +00:00
let re1 = new RegExp(/[\u4E00-\uFA29]/) //Chinese character range
let re2 = new RegExp(/[\uE7C7-\uE7F3]/) //non Chinese character range
2022-05-04 04:06:01 +00:00
str = str.replace(/\s/g, '')
if (!re1.test(str) || re2.test(str)) {
return false
}
return true
2022-05-06 01:16:15 +00:00
}
function openDialog(title, message, negBtn = false, affirmBtn = closeDialog) {
const dialog = document.getElementById('miscDialog')
const titleElm = document.getElementById('dialogTitle')
const contents = document.getElementById('dialogContent')
const noBtn = document.getElementById('dialogButtonNeg')
const yesBtn = document.getElementById('dialogButtonAffirm')
2022-05-06 03:25:39 +00:00
if (!negBtn) {
2022-05-06 01:16:15 +00:00
noBtn.style.display = 'none'
} else {
noBtn.style.removeProperty('display')
noBtn.onclick = () => closeDialog()
}
2022-05-06 03:25:39 +00:00
yesBtn.innerText = localeObj.dialogYes || 'OK'
noBtn.innerText = localeObj.dialogNo || 'NO'
2022-05-06 01:16:15 +00:00
yesBtn.onclick = () => {
affirmBtn()
closeDialog()
}
// Set title and message
titleElm.innerText = title
contents.innerText = message
// Show the dialog
dialog.style.display = 'block'
}
function closeDialog() {
const dialog = document.getElementById('miscDialog')
dialog.style.display = 'none'
}
2022-05-04 04:06:01 +00:00
/**
* Minimize the window
*/
2022-05-03 05:44:57 +00:00
function minimizeWin() {
2022-05-03 05:48:03 +00:00
Neutralino.window.minimize()
}
/**
* Close the window
*/
function closeWin() {
2022-05-03 05:48:03 +00:00
Neutralino.app.exit()
2022-05-03 05:48:03 +00:00
window.close()
}