From c03d7521f3d12543769ed3041a11f76627d9c2da Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Mon, 15 Apr 2024 13:50:56 +0800 Subject: [PATCH] prepare to move strings to the /scripts folder --- scripts/AI/gemini.js | 4 ++-- scripts/strings/read.js | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 scripts/strings/read.js diff --git a/scripts/AI/gemini.js b/scripts/AI/gemini.js index 85909ea..2393b4d 100644 --- a/scripts/AI/gemini.js +++ b/scripts/AI/gemini.js @@ -2,7 +2,7 @@ // Import the file module. // import file from `./net.js`; -const texts = (await import(chrome.runtime.getURL("scripts/read.js"))).default; +const texts = (await import(chrome.runtime.getURL("scripts/strings/read.js"))).default; // Don't forget to set the class as export default. export default class gemini { @@ -17,7 +17,7 @@ export default class gemini { */ constructor (key, model, version = {"API": "v1beta"}) { if ((key) ? (((typeof key).includes(`str`)) ? !(key.trim()) : true) : true) { - throw new ReferenceError(texts.localized(`error_msg_APImissing`)); + throw new ReferenceError((new texts(`error_msg_APImissing`)).localized); }; // Register the API key privately. diff --git a/scripts/strings/read.js b/scripts/strings/read.js new file mode 100644 index 0000000..9cc66b6 --- /dev/null +++ b/scripts/strings/read.js @@ -0,0 +1,53 @@ +/* read_universal + Read a file stored in the universal strings. */ + +let messages = {}; +let message = ""; + +export default class texts { + /* This reads the message from its source. This is a fallback for the content scripts, who doesn't appear to read classes. + + @param {string} message the message name + @param {boolean} autofill fill in the message with the template name when not found + @param {list} params the parameters + @return {string} the message + */ + + constructor(message_name, autofill = false, params = []) { + if (params && (params ? params.length > 0 : false)) { + this.localized = chrome.i18n.getMessage(message_name, params); + } else { + this.localized = chrome.i18n.getMessage(message_name); + } + + // When the message is not found, return the temporary text. + if (!this.localized && autofill) { + this.localized = message_name; + } else if (!this.localized && !autofill) { + delete this.localized; + }; + } + + static localized(message_name, autofill = false, params = []) { + if (params && (params ? params.length > 0 : false)) { + message = chrome.i18n.getMessage(message_name, params); + } else { + message = chrome.i18n.getMessage(message_name); + } + + // When the message is not found, return the temporary text. + if (!message && autofill) { + message = message_name; + } + + return message; + } +} + +export function read(message_name, autofill, params) { + let message; + + message = texts.localized(message_name, autofill, params); + + return message; +}