diff --git a/packages/crx-monkey/src/node/config.ts b/packages/crx-monkey/src/node/config.ts index 2ea96d7..36e6871 100644 --- a/packages/crx-monkey/src/node/config.ts +++ b/packages/crx-monkey/src/node/config.ts @@ -20,6 +20,10 @@ const defaultConfig: CrxMonkeyConfig = { userscriptInjectPage: [], }; +/** + * Get the path to crx-monkey.config.js. + * @returns + */ async function getConfigPath(): Promise { return await new Promise((resolve, reject) => { let dir = process.cwd(); @@ -47,6 +51,11 @@ async function getConfigPath(): Promise { }); } +/** + * Find config file. + * @param dir + * @returns + */ async function search(dir: string): Promise { return await new Promise((resolve) => { void promises.readdir(dir + '/').then((files) => { @@ -63,6 +72,12 @@ async function search(dir: string): Promise { }); } +/** + * [Type guard] Is the key contained object?? + * @param obj + * @param key + * @returns + */ function isKeyof(obj: object, key: T | string): key is T { let result = false; Object.keys(obj).forEach((objkey) => { @@ -73,6 +88,11 @@ function isKeyof(obj: object, key: T | string): key is T { return result; } +/** + * If value in config is undefined, that value will be default value. + * @param config LKoaded config data. + * @returns + */ function setDefaultConfig(config: Record) { const newConf: CrxMonkeyConfig = { ...defaultConfig, @@ -89,8 +109,16 @@ function setDefaultConfig(config: Record) { return newConf; } +/** + * Loaded config data. + * It is null when before use loadConfig() use. + */ let configCahce: null | CrxMonkeyConfig = null; +/** + * Load config file in project. + * @returns + */ export async function loadConfig(): Promise { return await new Promise((resolve) => { void getConfigPath() @@ -111,6 +139,11 @@ export async function loadConfig(): Promise { }); } +/** + * Get config that had loaded. + * Must run loadConfig() before use! + * @returns + */ export function getConfig() { if (configCahce !== null) { return configCahce; diff --git a/packages/crx-monkey/src/node/handlers/build/Build.ts b/packages/crx-monkey/src/node/handlers/build/Build.ts index 449ef14..e16ac53 100644 --- a/packages/crx-monkey/src/node/handlers/build/Build.ts +++ b/packages/crx-monkey/src/node/handlers/build/Build.ts @@ -19,6 +19,13 @@ export class Build { this.config = config; } + /** + * Build multiple js files. + * @param jsFilePaths pathes. + * @param onBuild + * @param overrideOptions + * @param overridePlugins + */ protected async buildByJsFilePaths( jsFilePaths: string[], onBuild: (result: BuildResult, jsFilePath: string) => void, diff --git a/packages/crx-monkey/src/node/handlers/build/BuildContentScript.ts b/packages/crx-monkey/src/node/handlers/build/BuildContentScript.ts index 0046e56..041d042 100644 --- a/packages/crx-monkey/src/node/handlers/build/BuildContentScript.ts +++ b/packages/crx-monkey/src/node/handlers/build/BuildContentScript.ts @@ -30,6 +30,10 @@ export class BuildContentScript extends Build implements BuildImplements { } } + /** + * Copy css files to dist. + * @param cssFilePaths + */ private copyCssFiles(cssFilePaths: string[]) { cssFilePaths.forEach((cssFilePath, index) => { const fileName = path.basename(cssFilePath); diff --git a/packages/crx-monkey/src/node/handlers/build/BuildPopup.ts b/packages/crx-monkey/src/node/handlers/build/BuildPopup.ts index 9bb5190..bcae369 100644 --- a/packages/crx-monkey/src/node/handlers/build/BuildPopup.ts +++ b/packages/crx-monkey/src/node/handlers/build/BuildPopup.ts @@ -21,6 +21,10 @@ export class BuildPopup extends Build implements BuildImplements { } } + /** + * Build scripts that loaded in popup html. + * @param popupPath + */ private async buildLocalScripts(popupPath: string) { await Promise.all( Object.keys(this.requestLocalScripts).map(async (src) => { @@ -48,6 +52,11 @@ export class BuildPopup extends Build implements BuildImplements { ); } + /** + * Get Html parser instance. + * @param htmlPath + * @returns + */ private getParser(htmlPath: string) { const content = fse.readFileSync(htmlPath).toString(); const root = parse(content); @@ -55,6 +64,10 @@ export class BuildPopup extends Build implements BuildImplements { return root; } + /** + * Load paths of local script loaded by popup html. + * @param root + */ private loadRequestLocalResources(root: HTMLElement) { const scriptElems = root.querySelectorAll('script'); @@ -63,6 +76,7 @@ export class BuildPopup extends Build implements BuildImplements { scriptElems.forEach((elem) => { const src = elem.getAttribute('src'); if (src !== undefined && src !== null) { + // Except the script href that start http. if (src.match('^http.*') === null) { requestLocalScripts[src] = elem; } @@ -72,6 +86,10 @@ export class BuildPopup extends Build implements BuildImplements { this.requestLocalScripts = requestLocalScripts; } + /** + * Output html data in parser instance to dist. + * @param parserRoot + */ private outputHTML(parserRoot: HTMLElement) { fse.outputFile( path.join(this.config.chromeOutputDir!, 'popup/popup.html'), diff --git a/packages/crx-monkey/src/node/handlers/build/BuildUserScript.ts b/packages/crx-monkey/src/node/handlers/build/BuildUserScript.ts index 92f99c2..e9d1a9c 100644 --- a/packages/crx-monkey/src/node/handlers/build/BuildUserScript.ts +++ b/packages/crx-monkey/src/node/handlers/build/BuildUserScript.ts @@ -30,12 +30,19 @@ export class BuildUserScript extends Build implements BuildImplements { this.headerFactory = headerFactory; } + /** + * Build userscript + */ public async build() { if (this.config.devServer === undefined) { throw new Error('Dev Server is not enabled'); } + /** + * Build and output content scripts. + */ const contentScripts = this.manifest.content_scripts; + if (contentScripts !== undefined) { const { jsFiles, cssFiles } = getAllJsAndCSSByContentScripts(contentScripts); @@ -60,26 +67,46 @@ export class BuildUserScript extends Build implements BuildImplements { } } + /** + * Register meta data to userscript header factory. + * @param allMatches + * @param unsafeWindow + */ private async headerRegister(allMatches: string[], unsafeWindow: boolean) { + /** + * Set all match to header. + */ allMatches.forEach((match) => { this.headerFactory.push('@match', match); }); + /** + * Set version designation by manifest to header. + */ this.headerFactory.push('@version', this.manifest.version); if (this.manifest.run_at !== undefined) { this.headerFactory.push('@run-at', convertChromeRunAtToUserJsRunAt(this.manifest.run_at)); } + /** + * Set name. + * If can not found locale message, even if language key is not en, it will be en. + */ const names = await geti18nMessages(this.manifest.name); Object.keys(names).forEach((lang) => { if (lang === 'en') { + // default is en. this.headerFactory.push('@name', names[lang]); } else { this.headerFactory.push(`@name:${lang}`, names[lang]); } }); + /** + * Set description. + * If can not found locale message, even if language key is not en, it will be en. + */ if (this.manifest.description !== undefined) { const descriptions = await geti18nMessages(this.manifest.description); Object.keys(descriptions).forEach((lang) => { @@ -94,9 +121,11 @@ export class BuildUserScript extends Build implements BuildImplements { const configHeader = this.config.userScriptHeader; if (configHeader !== undefined) { configHeader.forEach((configHeaderItem) => { + // If key is not @grant and already exists it in header, it replace value into configHeaderItem[1]. if (this.headerFactory.exist(configHeaderItem[0]) && configHeaderItem[0] !== '@grant') { this.headerFactory.replace(configHeaderItem[0], configHeaderItem[1]); } else { + // If key is @grant and already exists it in header, add an add additional it. this.headerFactory.push(configHeaderItem[0], configHeaderItem[1]); // If already included unsafewindow in grant, after no need include. @@ -108,9 +137,13 @@ export class BuildUserScript extends Build implements BuildImplements { } if (unsafeWindow) { + // If not already included unsafewindow, run it. this.headerFactory.push('@grant', 'unsafeWindow'); } + /** + * Add icon of 48size that converted to base64 in manifest.json to userscript. + */ if (this.config.importIconToUsercript) { const icons = this.manifest.icons; @@ -143,7 +176,7 @@ export class BuildUserScript extends Build implements BuildImplements { } /** - * Build content scripts for each match and generate code to restrict execution for each match using if + * Build content scripts for each match and generate code to restrict execution for each match using the if syntax. * @param matchMap * @param jsBuildResultStore * @param cssResultStore @@ -154,13 +187,18 @@ export class BuildUserScript extends Build implements BuildImplements { jsBuildResultStore: Record, cssResultStore: Record, ) { + // script result tmp. let scriptContent = ''; + Object.keys(matchMap).forEach((filePath) => { const matches = matchMap[filePath]; + // Start conditional statement of if for branch of href. scriptContent = scriptContent + 'if ('; + // Does this contentscript have multiple match href? let isOr = false; + matches.forEach((matchPattern) => { scriptContent = scriptContent + `${isOr ? ' ||' : ''}location.href.match('${matchPattern}') !== null`; @@ -168,18 +206,24 @@ export class BuildUserScript extends Build implements BuildImplements { isOr = true; }); + // End conditional statement. scriptContent = scriptContent + ') {\n'; if (jsBuildResultStore[filePath] !== undefined) { const buildResultText = new TextDecoder().decode(jsBuildResultStore[filePath]); if (this.config.userscriptInjectPage.includes(filePath)) { + // Inject script using DOM. scriptContent = scriptContent + generateInjectScriptCode(buildResultText); } else { + // Run script in userscript sandbox. scriptContent = scriptContent + buildResultText; } } + /** + * Inject style using DOM. + */ if (cssResultStore[filePath] !== undefined) { const cssText = cssResultStore[filePath].toString(); scriptContent = @@ -191,12 +235,17 @@ export class BuildUserScript extends Build implements BuildImplements { ].join('\n'); } + // End if. scriptContent = scriptContent + '}\n\n'; }); return scriptContent; } + /** + * Marge userscript header, content script code and css inject code and output it. + * @param matchMap + */ private outputFile(matchMap: Record) { const contentScriptcode = this.generateContentScriptcode( matchMap, @@ -213,6 +262,10 @@ export class BuildUserScript extends Build implements BuildImplements { } } + /** + * Load content of css file selected by manifest.json and store it Buffer to this.cssResultStore. + * @param cssFilePaths + */ private loadContentCssFiles(cssFilePaths: string[]) { cssFilePaths.forEach((cssFilePath, index) => { const fileName = path.basename(cssFilePath); @@ -224,6 +277,11 @@ export class BuildUserScript extends Build implements BuildImplements { }); } + /** + * Does "userscriptInjectPage" in config contain even one js files? + * @param jsFiles js file paths. + * @returns + */ private isIncludedInjectScripts(jsFiles: string[]) { let result = false; diff --git a/packages/crx-monkey/src/node/handlers/dev/Watch.ts b/packages/crx-monkey/src/node/handlers/dev/Watch.ts index 02ec67a..e357512 100644 --- a/packages/crx-monkey/src/node/handlers/dev/Watch.ts +++ b/packages/crx-monkey/src/node/handlers/dev/Watch.ts @@ -34,6 +34,15 @@ export class Watch { }); } + /** + * Watch multiple js files. + * @param jsFilePaths + * @param onBuild + * @param overrideOptions + * @param onFirstBuild + * @param overridePlugins + * @returns + */ protected async watchByJsFilePaths( jsFilePaths: string[], onBuild: (result: BuildResult, jsFilePath: string) => void, diff --git a/packages/crx-monkey/src/node/handlers/dev/WatchContentScripts.ts b/packages/crx-monkey/src/node/handlers/dev/WatchContentScripts.ts index 193cf99..233202f 100644 --- a/packages/crx-monkey/src/node/handlers/dev/WatchContentScripts.ts +++ b/packages/crx-monkey/src/node/handlers/dev/WatchContentScripts.ts @@ -48,6 +48,10 @@ export class WatchContentScripts extends Watch implements WatchImplements { } } + /** + * Copy css files to dist. + * @param cssFilePaths + */ private copyCssFiles(cssFilePaths: string[]) { cssFilePaths.forEach((cssFilePath, index) => { const fileName = path.basename(cssFilePath); diff --git a/packages/crx-monkey/src/node/handlers/dev/WatchPopup.ts b/packages/crx-monkey/src/node/handlers/dev/WatchPopup.ts index 1e3760d..75d5b37 100644 --- a/packages/crx-monkey/src/node/handlers/dev/WatchPopup.ts +++ b/packages/crx-monkey/src/node/handlers/dev/WatchPopup.ts @@ -29,6 +29,9 @@ export class WatchPopup extends Watch implements WatchImplements { const root = this.getParser(popupHtml); this.loadRequestLocalResources(root); + /** + * Dispose watch of removed file. + */ const removed = this.removeResourcesCheck(); removed.forEach((removedFile) => { @@ -46,6 +49,10 @@ export class WatchPopup extends Watch implements WatchImplements { } } + /** + * Watch scripts that loaded in popup html. + * @param popupPath + */ private async watchLocalScripts(popupPath: string) { await Promise.all( Object.keys(this.requestLocalScripts).map(async (src) => { @@ -84,6 +91,11 @@ export class WatchPopup extends Watch implements WatchImplements { ); } + /** + * Get Html parser instance. + * @param htmlPath + * @returns + */ private getParser(htmlPath: string) { const content = fse.readFileSync(htmlPath).toString(); const root = parse(content); @@ -91,6 +103,10 @@ export class WatchPopup extends Watch implements WatchImplements { return root; } + /** + * Load paths of local script loaded by popup html. + * @param root + */ private loadRequestLocalResources(root: HTMLElement) { const scriptElems = root.querySelectorAll('script'); @@ -99,6 +115,7 @@ export class WatchPopup extends Watch implements WatchImplements { scriptElems.forEach((elem) => { const src = elem.getAttribute('src'); if (src !== undefined && src !== null) { + // Except the script href that start http. if (src.match('^http.*') === null) { requestLocalScripts[src] = elem; } @@ -113,6 +130,10 @@ export class WatchPopup extends Watch implements WatchImplements { consola.info(`Popup script updated. | ${jsFilePath}`); } + /** + * Output html data in parser instance to dist. + * @param parserRoot + */ private outputHTML(parserRoot: HTMLElement) { fse.outputFile( path.join(this.config.chromeOutputDir!, 'popup/popup.html'), @@ -120,6 +141,10 @@ export class WatchPopup extends Watch implements WatchImplements { ); } + /** + * Check to removed resouces. + * @returns + */ private removeResourcesCheck() { const removedResources: string[] = []; diff --git a/packages/crx-monkey/src/node/handlers/dev/WatchServiceWorker.ts b/packages/crx-monkey/src/node/handlers/dev/WatchServiceWorker.ts index afccc7b..2397bdf 100644 --- a/packages/crx-monkey/src/node/handlers/dev/WatchServiceWorker.ts +++ b/packages/crx-monkey/src/node/handlers/dev/WatchServiceWorker.ts @@ -41,6 +41,11 @@ export class WatchServiceWorker extends Watch implements WatchImplements { } } +/** + * Plugin outputing code that marge service worker code and build result. + * @param config + * @returns + */ function devServiceWorkerPlugin(config: CrxMonkeyConfig) { const devSwPlugin: Plugin = { name: 'dev-sw-plugin', @@ -67,6 +72,11 @@ function devServiceWorkerPlugin(config: CrxMonkeyConfig) { return devSwPlugin; } +/** + * Get static service worker code. + * @param param0 + * @returns + */ function generateDevSwCode({ host, websocket }: { port: number; host: string; websocket: number }) { const code = loadStaticFile(path.join(import.meta.dirname, './static/swDev.js'), { host, diff --git a/packages/crx-monkey/src/node/handlers/dev/WatchUserScript.ts b/packages/crx-monkey/src/node/handlers/dev/WatchUserScript.ts index f378f50..eda92e0 100644 --- a/packages/crx-monkey/src/node/handlers/dev/WatchUserScript.ts +++ b/packages/crx-monkey/src/node/handlers/dev/WatchUserScript.ts @@ -64,26 +64,46 @@ export class WatchUserScript extends Watch implements WatchImplements { } } + /** + * Register meta data to userscript header factory. + * @param allMatches + * @param unsafeWindow + */ private async headerRegister(allMatches: string[]) { + /** + * Set all match to header. + */ allMatches.forEach((match) => { this.headerFactory.push('@match', match); }); + /** + * Set version designation by manifest to header. + */ this.headerFactory.push('@version', this.manifest.version); if (this.manifest.run_at !== undefined) { this.headerFactory.push('@run-at', convertChromeRunAtToUserJsRunAt(this.manifest.run_at)); } + /** + * Set name. + * If can not found locale message, even if language key is not en, it will be en. + */ const names = await geti18nMessages(this.manifest.name); Object.keys(names).forEach((lang) => { if (lang === 'en') { + // default is en. this.headerFactory.push('@name', names[lang]); } else { this.headerFactory.push(`@name:${lang}`, names[lang]); } }); + /** + * Set description. + * If can not found locale message, even if language key is not en, it will be en. + */ if (this.manifest.description !== undefined) { const descriptions = await geti18nMessages(this.manifest.description); Object.keys(descriptions).forEach((lang) => { @@ -98,14 +118,19 @@ export class WatchUserScript extends Watch implements WatchImplements { const configHeader = this.config.userScriptHeader; if (configHeader !== undefined) { configHeader.forEach((configHeaderItem) => { + // If key is not @grant and already exists it in header, it replace value into configHeaderItem[1]. if (this.headerFactory.exist(configHeaderItem[0]) && configHeaderItem[0] !== '@grant') { this.headerFactory.replace(configHeaderItem[0], configHeaderItem[1]); } else { + // If key is @grant and already exists it in header, add an add additional it. this.headerFactory.push(configHeaderItem[0], configHeaderItem[1]); } }); } + /** + * Add icon of 48size that converted to base64 in manifest.json to userscript. + */ if (this.config.importIconToUsercript) { const icons = this.manifest.icons; @@ -149,13 +174,18 @@ export class WatchUserScript extends Watch implements WatchImplements { jsBuildResultStore: Record, cssResultStore: Record, ) { + // script result tmp. let scriptContent = ''; + Object.keys(matchMap).forEach((filePath) => { const matches = matchMap[filePath]; + // Start conditional statement of if for branch of href. scriptContent = scriptContent + 'if ('; + // Does this contentscript have multiple match href? let isOr = false; + matches.forEach((matchPattern) => { scriptContent = scriptContent + `${isOr ? ' ||' : ''}location.href.match('${matchPattern}') !== null`; @@ -163,6 +193,7 @@ export class WatchUserScript extends Watch implements WatchImplements { isOr = true; }); + // End conditional statement. scriptContent = scriptContent + ') {\n'; if (jsBuildResultStore[filePath] !== undefined) { @@ -171,6 +202,9 @@ export class WatchUserScript extends Watch implements WatchImplements { scriptContent = scriptContent + buildResultText; } + /** + * Inject style using DOM. + */ if (cssResultStore[filePath] !== undefined) { const cssText = cssResultStore[filePath].toString(); scriptContent = @@ -182,12 +216,17 @@ export class WatchUserScript extends Watch implements WatchImplements { ].join('\n'); } + // End if. scriptContent = scriptContent + '}\n\n'; }); return scriptContent; } + /** + * Marge userscript header, content script code and css inject code and output it. + * @param matchMap + */ private outputFile(matchMap: Record) { const contentScriptcode = this.generateContentScriptcode( matchMap, @@ -204,6 +243,10 @@ export class WatchUserScript extends Watch implements WatchImplements { } } + /** + * Load content of css file selected by manifest.json and store it Buffer to this.cssResultStore. + * @param cssFilePaths + */ private loadContentCssFiles(cssFilePaths: string[]) { cssFilePaths.forEach((cssFilePath, index) => { const fileName = path.basename(cssFilePath); diff --git a/packages/crx-monkey/src/node/handlers/dev/index.ts b/packages/crx-monkey/src/node/handlers/dev/index.ts index 3cdc5e6..bd3721c 100644 --- a/packages/crx-monkey/src/node/handlers/dev/index.ts +++ b/packages/crx-monkey/src/node/handlers/dev/index.ts @@ -58,7 +58,6 @@ export default async function handleDev() { await hostingServer.start(); - // console.clear(); consola.box( [ `${chalk.cyan.bold('CRX-MONKEY')} ${chalk.green(`v${pkg.version}`)}`, diff --git a/packages/crx-monkey/src/node/handlers/dev/server/reloadServer.ts b/packages/crx-monkey/src/node/handlers/dev/server/reloadServer.ts index 9cf7d57..93af5bf 100644 --- a/packages/crx-monkey/src/node/handlers/dev/server/reloadServer.ts +++ b/packages/crx-monkey/src/node/handlers/dev/server/reloadServer.ts @@ -1,10 +1,18 @@ import { WebSocketServer } from 'ws'; +/** + * The websocket server of manage auto reload. + */ export class ReloadServer { private readonly wserver: WebSocketServer; private readonly host: string; private readonly port: number; + /** + * Start and setup server. + * @param host + * @param port + */ constructor(host: string, port: number) { this.host = host; this.port = port; @@ -16,6 +24,10 @@ export class ReloadServer { this.setup(); } + /** + * Send reload signal to websocket client. + * @param token + */ public reload(token: ReloadTokens) { this.wserver.clients.forEach((client) => { client.send(token); diff --git a/packages/crx-monkey/src/node/handlers/dev/server/scriptHostingServer.ts b/packages/crx-monkey/src/node/handlers/dev/server/scriptHostingServer.ts index f4a7d65..cffc65f 100644 --- a/packages/crx-monkey/src/node/handlers/dev/server/scriptHostingServer.ts +++ b/packages/crx-monkey/src/node/handlers/dev/server/scriptHostingServer.ts @@ -3,6 +3,9 @@ import { getConfig } from 'src/node/config'; import path from 'path'; import fse from 'fs-extra'; +/** + * The server of send to script code. + */ export class ScriptHostingServer { private readonly app: express.Express; private readonly host: string; @@ -14,6 +17,10 @@ export class ScriptHostingServer { this.port = port; } + /** + * Start server. + * @returns + */ public async start() { this.setup(); return new Promise((resolve) => { diff --git a/packages/crx-monkey/src/node/handlers/dev/utils.ts b/packages/crx-monkey/src/node/handlers/dev/utils.ts index 60a88db..48f5ba6 100644 --- a/packages/crx-monkey/src/node/handlers/dev/utils.ts +++ b/packages/crx-monkey/src/node/handlers/dev/utils.ts @@ -12,6 +12,11 @@ export function getDevelopDir() { } } +/** + * Generate code of injecting script using DOM. + * @param scriptContent + * @returns + */ export function generateInjectScriptCode(scriptContent: string) { return [ `const inject = ()=>{${scriptContent}}`, diff --git a/packages/crx-monkey/src/node/handlers/utils.ts b/packages/crx-monkey/src/node/handlers/utils.ts index 1f820a8..5f7fd3e 100644 --- a/packages/crx-monkey/src/node/handlers/utils.ts +++ b/packages/crx-monkey/src/node/handlers/utils.ts @@ -2,6 +2,9 @@ import { getConfig } from '../config'; import path from 'path'; import fse from 'fs-extra'; +/** + * Copy the locales dir to dist. + */ export function copyLocales() { const localesPath = getlocalesPath(); const config = getConfig(); @@ -11,6 +14,10 @@ export function copyLocales() { } } +/** + * If selected the path, using it. + * But not selected, this function return the path of _locales dir in parent dir of manifest.json. + */ export function getlocalesPath() { const config = getConfig(); @@ -23,6 +30,9 @@ export function getlocalesPath() { } } +/** + * Copy the public dir to dist. + */ export function copyPublic() { const config = getConfig(); const publicDir = path.join(process.cwd(), config.publicDir); diff --git a/packages/crx-monkey/src/node/manifest-factory/index.ts b/packages/crx-monkey/src/node/manifest-factory/index.ts index 8bc6a95..dc6cc53 100644 --- a/packages/crx-monkey/src/node/manifest-factory/index.ts +++ b/packages/crx-monkey/src/node/manifest-factory/index.ts @@ -10,6 +10,10 @@ export class ManifestFactory { this.workspace = structuredClone(this.originalManifest); } + /** + * Output the current manifest data. + * @returns + */ public getWorkspace() { return this.workspace; } diff --git a/packages/crx-monkey/src/node/manifest-factory/utils.ts b/packages/crx-monkey/src/node/manifest-factory/utils.ts index e1ddd4d..1fd037a 100644 --- a/packages/crx-monkey/src/node/manifest-factory/utils.ts +++ b/packages/crx-monkey/src/node/manifest-factory/utils.ts @@ -4,7 +4,7 @@ import path from 'path'; import { getlocalesPath } from '../handlers/utils'; /** - * Enumerate all js and css paths from manifestjson conetnt_scripts + * Enumerate all js and css paths from conetnt_scripts in manifestjson * @param contentScripts * @param jsFiles * @param cssFiles @@ -87,6 +87,11 @@ export function getAllJsAndCSSByContentScripts( }; } +/** + * Get messages in locale each language. + * @param key + * @returns + */ export async function geti18nMessages(key: string) { const result: Record = { en: key }; const match = key.match(/__MSG_(.*)__/); @@ -109,6 +114,11 @@ export async function geti18nMessages(key: string) { return result; } +/** + * Get the message in locale. + * @param key + * @returns + */ async function getMessage(langKey: string, key: string) { const localesPath = getlocalesPath(); const messagesJsonPath = path.resolve(localesPath, langKey, 'messages.json'); @@ -127,6 +137,12 @@ async function getMessage(langKey: string, key: string) { } } +/** + * Get avaiable language. + * Must include en in locale!! + * @param localesPath + * @returns + */ function getEnableLangs(localesPath: string) { const langs = fs.readdirSync(localesPath); diff --git a/packages/crx-monkey/src/node/static/main.ts b/packages/crx-monkey/src/node/static/main.ts index 9528986..c7c0d31 100644 --- a/packages/crx-monkey/src/node/static/main.ts +++ b/packages/crx-monkey/src/node/static/main.ts @@ -1,5 +1,11 @@ import fse from 'fs-extra'; +/** + * Get static file to load for chrome extension in develop mode. + * @param filePath Static file path. + * @param vars Variables to inject into static code. + * @returns + */ export function loadStaticFile(filePath: string, vars: Record = {}) { const buffer = fse.readFileSync(filePath); let contents = buffer.toString(); diff --git a/packages/crx-monkey/src/node/userscript-header-factory/index.ts b/packages/crx-monkey/src/node/userscript-header-factory/index.ts index 548eabb..4fc0a75 100644 --- a/packages/crx-monkey/src/node/userscript-header-factory/index.ts +++ b/packages/crx-monkey/src/node/userscript-header-factory/index.ts @@ -3,10 +3,19 @@ import { UserScriptHeader, UserScriptHeaderProps } from '../types'; export class UserscriptHeaderFactory { private detail: UserScriptHeader = []; + /** + * Push header item. + * @param key + * @param value + */ public push(key: keyof UserScriptHeaderProps, value: string) { this.detail.push([key, value]); } + /** + * Output created header. + * @returns Userscript header string. + */ public create() { const header: string[] = []; @@ -21,6 +30,11 @@ export class UserscriptHeaderFactory { return header.join('\n'); } + /** + * Replace header item. + * @param key + * @param value + */ public replace(key: keyof UserScriptHeaderProps, value: string) { this.detail.forEach(([detailKey], index) => { if (key === detailKey) { @@ -29,6 +43,11 @@ export class UserscriptHeaderFactory { }); } + /** + * Is exist the key in created header?? + * @param key + * @returns + */ public exist(key: keyof UserScriptHeaderProps) { this.detail.forEach(([detailKey]) => { if (key === detailKey) { diff --git a/packages/crx-monkey/src/node/userscript-header-factory/utils.ts b/packages/crx-monkey/src/node/userscript-header-factory/utils.ts index 5bcb3e4..10081e0 100644 --- a/packages/crx-monkey/src/node/userscript-header-factory/utils.ts +++ b/packages/crx-monkey/src/node/userscript-header-factory/utils.ts @@ -1,5 +1,10 @@ import fs from 'fs'; +/** + * Run_at in chrome extension manifest convert to runAt in userscript. + * @param chromeRunAt + * @returns + */ export function convertChromeRunAtToUserJsRunAt( chromeRunAt: 'document_start' | 'document_end' | 'document_idle', ): 'document-start' | 'document-end' | 'document-idle' { @@ -20,6 +25,11 @@ export function convertChromeRunAtToUserJsRunAt( } } +/** + * Convert image to base64 string. + * @param imgPath Local image file path. + * @returns + */ export function convertImgToBase64(imgPath: string) { const icon = fs.readFileSync(imgPath); const buf = Buffer.from(icon).toString('base64');