// DarkPlasma_AutoHighlight 2.0.2 // Copyright (c) 2022 DarkPlasma // This software is released under the MIT license. // http://opensource.org/licenses/mit-license.php /** * 2025/07/16 2.0.2 正規表現における特殊文字を含む語句をハイライトしようとするとエラーで停止する不具合を修正 * 2024/01/15 2.0.1 ビルド方式を変更 (configをTypeScript化) * 2023/06/02 2.0.0 色設定をMZ1.6.0形式に変更 * ベースプラグインを追加 * TypeScript移行 * 2022/06/08 1.0.1 対象ウィンドウのクラス名がグローバルに展開されていなくても有効にする * 2022/01/02 1.0.0 公開 */ /*: @target MZ @url https://github.com/elleonard/DarkPlasma-MZ-Plugins/tree/release @plugindesc Automatically color specified words @author DarkPlasma @license MIT @help English Help Translator: munokura This is an unofficial English translation of the plugin help, created to support global RPG Maker users. Feedback is welcome to improve translation quality (see: https://github.com/munokura/DarkPlasma-MZ-Plugins ). Original plugin by DarkPlasma. Please check the latest official version at: https://github.com/elleonard/DarkPlasma-MZ-Plugins/tree/release ----- version: 2.0.2 Highlights the specified phrase in the specified color. This plugin requires the following plugin: DarkPlasma_SetColorByCode version: 1.0.0 @param highlightGroups @text Colors and phrases @desc Set the color and phrase to highlight. @type struct[] @default [] @param targetWindows @text Target window @desc Specifies the window class that will be auto-highlighted. @type string[] @default ["Window_Message"] */ /*:ja @plugindesc 指定した語句に自動で色をつける @author DarkPlasma @license MIT @target MZ @url https://github.com/elleonard/DarkPlasma-MZ-Plugins/tree/release @base DarkPlasma_SetColorByCode @param highlightGroups @desc ハイライトする際の色と語句を設定します。 @text 色と語句 @type struct[] @default [] @param targetWindows @desc 自動ハイライトの対象となるウィンドウクラスを指定します。 @text 対象ウィンドウ @type string[] @default ["Window_Message"] @help version: 2.0.2 指定した語句を指定した色でハイライトします。 本プラグインの利用には下記プラグインを必要とします。 DarkPlasma_SetColorByCode version:1.0.0 */ (() => { 'use strict'; const pluginName = document.currentScript.src.replace(/^.*\/(.*).js$/, function () { return arguments[1]; }); const pluginParametersOf = (pluginName) => PluginManager.parameters(pluginName); const pluginParameters = pluginParametersOf(pluginName); const settings = { highlightGroups: pluginParameters.highlightGroups ? JSON.parse(pluginParameters.highlightGroups).map((e) => { return e ? ((parameter) => { const parsed = JSON.parse(parameter); return { title: String(parsed.title || ``), color: parsed.color?.startsWith('#') ? String(parsed.color) : Number(parsed.color || 0), texts: parsed.texts ? JSON.parse(parsed.texts).map((e) => { return String(e || ``); }) : [], skills: parsed.skills ? JSON.parse(parsed.skills).map((e) => { return Number(e || 0); }) : [], items: parsed.items ? JSON.parse(parsed.items).map((e) => { return Number(e || 0); }) : [], }; })(e) : { title: '', color: 0, texts: [], skills: [], items: [] }; }) : [], targetWindows: pluginParameters.targetWindows ? JSON.parse(pluginParameters.targetWindows).map((e) => { return String(e || ``); }) : ['Window_Message'], }; function ColorManagerMixIn(colorManager) { colorManager.convertColorParameter = function (colorParameter) { return typeof colorParameter === 'string' ? colorParameter : this.textColor(colorParameter); }; } function regExpEscape(text) { return text.replaceAll(/[.*+?^${}()|[\]\\]/g, '\\$&'); } ColorManagerMixIn(ColorManager); class HighlightWords { constructor() { this._highlightWords = []; this._sortedWords = null; this._colors = {}; this._needsRefreshCache = false; } getRegExp() { return new RegExp( `(${this.sortedWords() .map((word) => regExpEscape(word)) .join('|')})`, 'gi', ); } /** * 長さ順にソートしたハイライト語句一覧 */ sortedWords() { if (!this._sortedWords || this._needsRefreshCache) { this.refreshCache(); } return this._sortedWords || []; } /** * @param {HighlightWord} highlightWord ハイライトする語句と色 */ add(highlightWord) { this._highlightWords.push(highlightWord); this._needsRefreshCache = true; } refreshCache() { /** * 毎度ソートするのはパフォーマンス的に許容できないため、キャッシュする */ this._sortedWords = this._highlightWords.map((word) => word.word).sort((a, b) => b.length - a.length); this._colors = {}; /** * パフォーマンスに気を使い、ランダムアクセスできるようにキャッシュする */ this._highlightWords.forEach((highlightWord) => { this._colors[highlightWord.word] = highlightWord.color; }); this._needsRefreshCache = false; } /** * ハイライト色を返す * @param {string} word ハイライトする語句 * @return {string|number} */ findColorByWord(word) { if (!this._colors) { this.refreshCache(); } return this._colors[word]; } /** * テキスト内の指定語句をハイライトして返す * @param {string} text ハイライト対象テキスト * @return {string} */ highlightText(text) { return text.replace(this.getRegExp(), (match) => { return `\x1bC[${this.findColorByWord(match)}]${match}\x1bC[0]`; }); } } class HighlightWord { constructor(word, color) { this._word = word; this._color = color; } get word() { return this._word; } get color() { return ColorManager.convertColorParameter(this._color); } } const highlightWords = new HighlightWords(); /** * @param {Scene_Boot.prototype} sceneBoot */ function Scene_Boot_AutoHighlightMixIn(sceneBoot) { const _start = sceneBoot.start; sceneBoot.start = function () { _start.call(this); /** * データベースのロードが完了しているので、ハイライトテキストを設定する。 */ settings.highlightGroups.forEach((highlightGroup) => { highlightGroup.texts.forEach((text) => { if (text) { highlightWords.add(new HighlightWord(text, highlightGroup.color)); } }); highlightGroup.skills.forEach((skillId) => { const skillName = $dataSkills[skillId].name; if (skillName) { highlightWords.add(new HighlightWord(skillName, highlightGroup.color)); } }); highlightGroup.items.forEach((itemId) => { const itemName = $dataItems[itemId].name; if (itemName) { highlightWords.add(new HighlightWord(itemName, highlightGroup.color)); } }); }); }; } Scene_Boot_AutoHighlightMixIn(Scene_Boot.prototype); function Window_AutoHighlightMixIn(windowClass) { const _convertEscapeCharacters = windowClass.convertEscapeCharacters; windowClass.convertEscapeCharacters = function (text) { text = _convertEscapeCharacters.call(this, text); if (this.isHighlightWindow()) { return highlightWords.highlightText(text); } return text; }; windowClass.isHighlightWindow = function () { return settings.targetWindows.some((targetWindow) => this.constructor.name === targetWindow); }; } Window_AutoHighlightMixIn(Window_Base.prototype); })();