diff --git a/.eslintrc.json b/.eslintrc.json index 925516b..a0de808 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -69,7 +69,16 @@ { "afterColon": true } + ], + "no-else-return": "error", + "camelcase": "error", + "brace-style": "error", + "no-var": "error", + "no-multiple-empty-lines": [ + "error", + {"max": 1} ] + }, "extends": [ "plugin:jsonc/recommended-with-json5" diff --git a/package-lock.json b/package-lock.json index 1862614..716d703 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "better-japanese", - "version": "1.2.3", + "version": "1.2.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "better-japanese", - "version": "1.2.3", + "version": "1.2.4", "license": "ISC", "devDependencies": { "chokidar": "^3.5.3", diff --git a/package.json b/package.json index 3521caf..d16b3c8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "better-japanese", - "version": "1.2.3", + "version": "1.2.4", "description": "", "main": "index.js", "scripts": { diff --git a/src/main.js b/src/main.js index cb0a29c..1f19a2d 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,4 @@ -var betterJapanese = { +const betterJapanese = { name: 'betterJapanese', apiUrl: { release: 'https://pages.yukineko.me/better-japanese/api/release.json', @@ -38,6 +38,14 @@ var betterJapanese = { betterJapanese.origins.updateMenu = Game.UpdateMenu betterJapanese.origins.sayTime = Game.sayTime betterJapanese.origins.beautify = Beautify + betterJapanese.origins.parseLoc = parseLoc + + // 翻訳対象の文章の末尾に%が付いている場合に消えてしまう問題を修正 + parseLoc = function(str, params) { + let baseStr = betterJapanese.origins.parseLoc(str, params) + if (typeof str === 'string' && str.endsWith('%')) baseStr += '%' + return baseStr + } // メニューに独自ボタンを実装 Game.UpdateMenu = function() { @@ -75,15 +83,15 @@ var betterJapanese = { // 設定によって日本語単位を使用するように変更、同時にカンマ区切りも場合によって変更 Beautify = function(val, floats) { - var negative = (val < 0) - var decimal = '' - var fixed = val.toFixed(floats) + let negative = (val < 0) + let decimal = '' + let fixed = val.toFixed(floats) if (floats > 0 && Math.abs(val) < 1000 && Math.floor(fixed) != fixed) decimal = '.' + (fixed.toString()).split('.')[1] val = Math.floor(Math.abs(val)) if (floats > 0 && fixed == val + 1) val++ let format = Game.prefs.format ? 2 : betterJapanese.config.numberJP ? 3 : 1 - var formatter = numberFormatters[format] - var output = (val.toString().indexOf('e+') != -1 && format == 2) ? val.toPrecision(3).toString() : formatter(val).toString() + let formatter = numberFormatters[format] + let output = (val.toString().indexOf('e+') != -1 && format == 2) ? val.toPrecision(3).toString() : formatter(val).toString() if (Game.prefs.format || (betterJapanese.config.numberJP && betterJapanese.config.secondFormatJP)) { output = output.replace(/\B(?=(\d{3})+(?!\d))/g, ',') } else { @@ -100,7 +108,7 @@ var betterJapanese = { } // カスタムCSSを適用 - var customStyle = document.createElement('style') + let customStyle = document.createElement('style') customStyle.innerHTML = ` .framed q:before { display:inline-block; @@ -124,14 +132,14 @@ var betterJapanese = { // 在庫市場のquoteを実装 while (!Game.Objects['Bank'].hasOwnProperty('minigame')) await new Promise(resolve => setTimeout(resolve, 1000)) - var M = Game.Objects['Bank'].minigame + let M = Game.Objects['Bank'].minigame M.goodTooltip = function(id) { return function() { - var me = M.goodsById[id] - var delta = M.goodDelta(id) - var val = M.getGoodPrice(me) + let me = M.goodsById[id] + let delta = M.goodDelta(id) + let val = M.getGoodPrice(me) icon = me.icon || [0, 0] - var str = '
' + '
' + '
' + me.name + ' (' + loc('from %1', '' + me.company + '') + ') ' + me.symbol + ' ' + (delta + '' + (delta == Math.floor(delta) ? '.00' : (delta * 10) == Math.floor(delta * 10) ? '0' : '') + '%') + '
' + '
' + '' + loc(me.desc) + '' + '
' + loc('%1: currently worth $%2 per unit.', [me.name, Beautify(val, 2)]) + '
• ' + loc('You currently own %1 (worth $%2).', ['
' + Beautify(me.stock) + 'x ' + me.name, Beautify(val * me.stock, 2)]) + '
• ' + loc('Your warehouses can store up to %1.', '
' + Beautify(M.getGoodMaxStock(me)) + 'x ' + me.name) + '
• ' + loc('You may increase your storage space by upgrading your offices and by buying more %1. You also get %2 extra storage space per %3 level (currently: +%4).', ['
' + me.building.plural, 10, me.building.single, (me.building.level * 10)]) + '
• ' + loc('The average worth of this stock and how high it can peak depends on the building it is tied to, along with the level of your %1.', '
' + Game.Objects['Bank'].plural) + '
' + '
' + loc('%1 the hide button to toggle all other stocks.', loc('Shift-click')) + '
' + '
' + let str = '
' + '
' + '
' + me.name + ' (' + loc('from %1', '' + me.company + '') + ') ' + me.symbol + ' ' + (delta + '' + (delta == Math.floor(delta) ? '.00' : (delta * 10) == Math.floor(delta * 10) ? '0' : '') + '%') + '
' + '
' + '' + loc(me.desc) + '' + '
' + loc('%1: currently worth $%2 per unit.', [me.name, Beautify(val, 2)]) + '
• ' + loc('You currently own %1 (worth $%2).', ['
' + Beautify(me.stock) + 'x ' + me.name, Beautify(val * me.stock, 2)]) + '
• ' + loc('Your warehouses can store up to %1.', '
' + Beautify(M.getGoodMaxStock(me)) + 'x ' + me.name) + '
• ' + loc('You may increase your storage space by upgrading your offices and by buying more %1. You also get %2 extra storage space per %3 level (currently: +%4).', ['
' + me.building.plural, 10, me.building.single, (me.building.level * 10)]) + '
• ' + loc('The average worth of this stock and how high it can peak depends on the building it is tied to, along with the level of your %1.', '
' + Game.Objects['Bank'].plural) + '
' + '
' + loc('%1 the hide button to toggle all other stocks.', loc('Shift-click')) + '
' + '
' return str } } @@ -142,15 +150,12 @@ var betterJapanese = { let logIndex = '' let logResult = [] let logId = 0 - while (typeof (logIndex = FindLocStringByPart(`Update notes ${logId}`)) === 'string' && typeof (logResult = loc(logIndex)) === 'object' && logResult.length > 1) - { + while (typeof (logIndex = FindLocStringByPart(`Update notes ${logId}`)) === 'string' && typeof (logResult = loc(logIndex)) === 'object' && logResult.length > 1) { logPerUpdate = `
` logPerUpdate += `
${logResult[0]}
` logResult.shift() - for (let str of logResult) - { - if(str.indexOf('[Update Log General Names]') >= 0) - { + for (let str of logResult) { + if(str.indexOf('[Update Log General Names]') >= 0) { str = str.replaceAll('[Update Log General Names]', choose(loc('[Update Log General Names]'))) } logPerUpdate += `
${str}
` @@ -158,8 +163,7 @@ var betterJapanese = { logUpdates = `${logPerUpdate}
${logUpdates}` logId++ } - if(logUpdates.length > 0) - { + if(logUpdates.length > 0) { betterJapanese.origins.updateLog = Game.updateLog Game.updateLog = Game.updateLog.substring(0, Game.updateLog.search(/
/)) Game.updateLog = Game.updateLog.substring(0, Game.updateLog.lastIndexOf('
')) @@ -170,15 +174,12 @@ var betterJapanese = { let upgrade = Game.Upgrades['Endless book of prose'] upgrade.desc = loc('%1 are twice as efficient.', cap(upgrade.buildingTie1.plural)) upgrade.originDescFunc = upgrade.descFunc - upgrade.descFunc = function() - { - - var str = loc(FindLocStringByPart(`Upgrade quote ${this.id}`), Game.bakeryName) - var n = 26 - var i = Math.floor(Game.T * 0.1) + upgrade.descFunc = function() { + let str = loc(FindLocStringByPart(`Upgrade quote ${this.id}`), Game.bakeryName) + let n = 26 + let i = Math.floor(Game.T * 0.1) let originDesc = this.originDescFunc() - if(originDesc.indexOf('') >= 0) - { + if(originDesc.indexOf('') >= 0) { originDesc = originDesc.substring(0, originDesc.indexOf('')) } return `${originDesc}${str.substr(i % str.length, n) + (i % str.length > (str.length - n) ? str.substr(0, i % str.length - (str.length - n)) : '')}` @@ -187,38 +188,33 @@ var betterJapanese = { // マウス達をクリックするマウス達のフレーバーテキスト翻訳 upgrade = Game.Upgrades['Mice clicking mice'] upgrade.desc = betterJapanese.createSynergyUpgradeDesc(upgrade) - upgrade.descFunc = function() - { + upgrade.descFunc = function() { Math.seedrandom(Game.seed + '-blasphemouse') - if (Math.random() < 0.3) - { + if (Math.random() < 0.3) { Math.seedrandom() return `${this.desc}${loc(FindLocStringByPart(`Upgrade quote ${this.id}`))}` } - else - { - Math.seedrandom() - return `${this.desc}${loc('Mice clicking mice (Absolutely blasphemouse!)')}` - } + + Math.seedrandom() + return `${this.desc}${loc('Mice clicking mice (Absolutely blasphemouse!)')}` } // 富くじ演算のフレーバーテキスト翻訳 upgrade = Game.Upgrades['Tombola computing'] upgrade.desc = betterJapanese.createSynergyUpgradeDesc(upgrade) - upgrade.descFunc = function() - { + upgrade.descFunc = function() { Math.seedrandom(Game.seed + '-tombolacomputing') let str = loc(FindLocStringByPart(`Upgrade quote ${this.id}`), [ - Math.floor(Math.random() * 100), - Math.floor(Math.random() * 100), - Math.floor(Math.random() * 100), - Math.floor(Math.random() * 100), - parseLoc(choose(loc('Tombola computing (Base)')), [ - Math.floor(Math.random() * 5 + 2), - choose(loc('Tombola computing (Color)')), - choose(loc('Tombola computing (Living)')) - ]) + Math.floor(Math.random() * 100), + Math.floor(Math.random() * 100), + Math.floor(Math.random() * 100), + Math.floor(Math.random() * 100), + parseLoc(choose(loc('Tombola computing (Base)')), [ + Math.floor(Math.random() * 5 + 2), + choose(loc('Tombola computing (Color)')), + choose(loc('Tombola computing (Living)')) ]) + ]) Math.seedrandom() return `${this.desc}${str}` } @@ -229,22 +225,17 @@ var betterJapanese = { // 猫の場合「購入済み」タグが変化することを翻訳にも反映 betterJapanese.origins.crateTooltip = Game.crateTooltip - Game.crateTooltip = function(me, context) - { + Game.crateTooltip = function(me, context) { let tooptipText = betterJapanese.origins.crateTooltip(me, context) - if(Game.sesame) - { + if(Game.sesame) { tooptipText = Game.substring(0, '
') tooptipText += `
ID : ${me.id} | 順序 : ${Math.floor(me.order)}${me.tier ? ` | ティア : ${me.tier}` : ''}
` } - if(me.type == 'upgrade' && me.bought > 0 && me.pool != 'tech' && me.kitten) - { + if(me.type == 'upgrade' && me.bought > 0 && me.pool != 'tech' && me.kitten) { return tooptipText.replace(`
${loc('Purchased')}
`, `
${loc('[Tag]Purrchased')}
`) } - else - { - return tooptipText - } + + return tooptipText } // hookを削除 @@ -261,7 +252,7 @@ var betterJapanese = { }, load: function() { - var conf = localStorage.getItem('BJPConfig') + let conf = localStorage.getItem('BJPConfig') if (conf) this.config = JSON.parse(conf) }, @@ -285,17 +276,17 @@ var betterJapanese = { // 本家のWritePrefButtonとほぼ同じ // ボタンを追加する先の要素を指定 (デフォルトはmonospaceButton) - var targetElement = l(targetElementName) + let targetElement = l(targetElementName) // 仕様の都合上、最初に改行タグを追加 targetElement.parentNode.insertBefore(document.createElement('br'), targetElement.previousElementSibling) // ボタンを生成 - var elementButton = document.createElement('a') + let elementButton = document.createElement('a') elementButton.className = `smallFancyButton prefButton option ${this.config[targetProp] ? 'on' : 'off'}` elementButton.id = buttonId - var onclickStr = `betterJapanese.toggleButton('${buttonId}', '${targetProp}', '${desc}');` + let onclickStr = `betterJapanese.toggleButton('${buttonId}', '${targetProp}', '${desc}');` // Callbackが存在し、なおかつ与えられた引数がfunctionであればCallbackを追加 if (callback && typeof callback === 'function') onclickStr += `(${callback.toString()})()` @@ -307,14 +298,14 @@ var betterJapanese = { // ラベルがあれば生成 if (label) { - var elementLabel = document.createElement('label') + let elementLabel = document.createElement('label') elementLabel.innerText = `(${label})` targetElement.parentNode.insertBefore(elementLabel, targetElement.previousElementSibling) } }, toggleButton: function(buttonId, targetProp, desc) { - var button = l(buttonId) + let button = l(buttonId) betterJapanese.config[targetProp] = !betterJapanese.config[targetProp] button.className = `smallFancyButton prefButton option ${this.config[targetProp] ? 'on' : 'off'}` button.innerText = `${desc} ${this.config[targetProp] ? loc('ON') : loc('OFF')}` @@ -322,7 +313,7 @@ var betterJapanese = { }, addDevButton: function() { - var element = document.createElement('div') + let element = document.createElement('div') element.innerHTML = '' document.body.append(element) }, @@ -331,7 +322,7 @@ var betterJapanese = { this.log('Checking updates') if (this.isDev) return await this.updateLanguagePack(this.apiUrl.dev) - var res = await fetch(this.apiUrl.release).then(res => res.json()).catch((err) => { + let res = await fetch(this.apiUrl.release).then(res => res.json()).catch((err) => { this.log(`An error occurred while checking for updates: ${err}`) return this.config }) @@ -363,7 +354,7 @@ var betterJapanese = { }, updateLanguagePack: async function(url) { - var base = { + let base = { '': { 'language': 'JA', 'plural-forms': 'nplurals=2;plural=(n!=1);' @@ -371,7 +362,7 @@ var betterJapanese = { } try { - var lang = await fetch(url).then(res => res.json()) + let lang = await fetch(url).then(res => res.json()) localStorage.setItem('BJPLangPack', JSON.stringify(Object.assign(base, lang))) } catch { this.log('Update failed') @@ -385,9 +376,9 @@ var betterJapanese = { formatEveryFourthPower: function() { // 接尾辞挿入の4桁区切り版、secondで第二単位の使用を指定 return function(value) { - var prefixes = betterJapanese.config.shortFormatJP ? betterJapanese.formats.short : betterJapanese.formats.prefix - var suffixes = betterJapanese.config.shortFormatJP ? [''] : betterJapanese.formats.suffixes - var second = betterJapanese.config.secondFormatJP + let prefixes = betterJapanese.config.shortFormatJP ? betterJapanese.formats.short : betterJapanese.formats.prefix + let suffixes = betterJapanese.config.shortFormatJP ? [''] : betterJapanese.formats.suffixes + let second = betterJapanese.config.secondFormatJP // infinityの場合は無限大を返す if (!isFinite(value)) return '無限大'// loc("Infinity") @@ -406,8 +397,8 @@ var betterJapanese = { let sufIndex = Math.floor(numeral / prefixes.length) let dispNum = Math.round(value * 10000 / (10 ** (numeral * 4))) + // 第二単位を付ける if (second) { - // 第二単位を付ける if (!preIndex && !sufIndex) return value let str = Math.floor(dispNum / 10000) + (preIndex ? prefixes[preIndex] : suffixes[sufIndex]) @@ -416,10 +407,10 @@ var betterJapanese = { return str !== 'NaN' ? str : value.toPrecision(3).toString() - } else { - // 第二単位を付けない - return Math.round(value * 10000 / (10 ** (numeral * 4))) / 10000 + prefixes[preIndex] + suffixes[sufIndex] - } + } + + // 第二単位を付けない + return Math.round(value * 10000 / (10 ** (numeral * 4))) / 10000 + prefixes[preIndex] + suffixes[sufIndex] } }, @@ -440,4 +431,4 @@ window.api.receive('fromMain', (msg) => { } }) -betterJapanese.register() \ No newline at end of file +betterJapanese.register()