update: ztoolkit init

This commit is contained in:
windingwind 2023-07-29 11:06:20 +08:00
parent a5517736fa
commit b48953f405
3 changed files with 21 additions and 17 deletions

View File

@ -83,7 +83,7 @@ function dateFormat(fmt, date) {
if (ret) {
fmt = fmt.replace(
ret[1],
ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"),
);
}
}
@ -108,7 +108,7 @@ function renameLocaleFiles() {
if (localeSubFile.endsWith(".ftl")) {
renameSync(
path.join(localeSubDir, localeSubFile),
path.join(localeSubDir, `${config.addonRef}-${localeSubFile}`)
path.join(localeSubDir, `${config.addonRef}-${localeSubFile}`),
);
}
}
@ -126,13 +126,14 @@ function replaceString() {
const replaceTo = [author, description, homepage, version, buildTime];
replaceFrom.push(
...Object.keys(config).map((k) => new RegExp(`__${k}__`, "g"))
...Object.keys(config).map((k) => new RegExp(`__${k}__`, "g")),
);
replaceTo.push(...Object.values(config));
const optionsAddon = {
files: [
`${buildDir}/addon/**/*.xhtml`,
`${buildDir}/addon/**/*.html`,
`${buildDir}/addon/**/*.json`,
`${buildDir}/addon/prefs.js`,
`${buildDir}/addon/manifest.json`,
@ -159,7 +160,7 @@ function replaceString() {
const prefixedLines = lines.map((line) => {
// https://regex101.com/r/lQ9x5p/1
const match = line.match(
/^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/m
/^(?<message>[a-zA-Z]\S*)([ ]*=[ ]*)(?<pattern>.*)$/m,
);
if (match) {
localeMessage.add(match.groups.message);
@ -180,7 +181,7 @@ function replaceString() {
if (localeMessage.has(match[2])) {
input = input.replace(
match[0],
`${match[1]}="${config.addonRef}-${match[2]}"`
`${match[1]}="${config.addonRef}-${match[2]}"`,
);
} else {
localeMessageMiss.add(match[2]);
@ -196,14 +197,14 @@ function replaceString() {
.filter((f) => f.hasChanged)
.map((f) => `${f.file} : ${f.numReplacements} / ${f.numMatches}`),
replaceResultFlt.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`),
replaceResultXhtml.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`)
replaceResultXhtml.filter((f) => f.hasChanged).map((f) => `${f.file} : OK`),
);
if (localeMessageMiss.size !== 0) {
console.warn(
`[Build] [Warn] Fluent message [${new Array(
...localeMessageMiss
)}] do not exsit in addon's locale files.`
...localeMessageMiss,
)}] do not exsit in addon's locale files.`,
);
}
}
@ -218,7 +219,7 @@ async function esbuild() {
target: "firefox102",
outfile: path.join(
buildDir,
`addon/chrome/content/scripts/${config.addonRef}.js`
`addon/chrome/content/scripts/${config.addonRef}.js`,
),
// Don't turn minify on
// minify: true,
@ -229,7 +230,7 @@ async function main() {
console.log(
`[Build] BUILD_DIR=${buildDir}, VERSION=${version}, BUILD_TIME=${buildTime}, ENV=${[
env.NODE_ENV,
]}`
]}`,
);
clearFolder(buildDir);
@ -238,7 +239,7 @@ async function main() {
if (isPreRelease) {
console.log(
"[Build] [Warn] Running in pre-release mode. update.json will not be replaced."
"[Build] [Warn] Running in pre-release mode. update.json will not be replaced.",
);
} else {
copyFileSync("update-template.json", "update.json");
@ -262,12 +263,12 @@ async function main() {
path.join(buildDir, `${name}.xpi`),
{
ignoreBase: true,
}
},
);
console.log("[Build] Addon pack OK");
console.log(
`[Build] Finished in ${(new Date().getTime() - t.getTime()) / 1000} s.`
`[Build] Finished in ${(new Date().getTime() - t.getTime()) / 1000} s.`,
);
}

View File

@ -29,7 +29,6 @@ async function onMainWindowLoad(win: Window): Promise<void> {
// Create ztoolkit for every window
const _ztoolkit = createZToolkit();
addon.data.ztoolkit = _ztoolkit;
_globalThis.ztoolkit = _ztoolkit;
const popupWin = new ztoolkit.ProgressWindow(config.addonName, {
closeOnClick: true,

View File

@ -12,16 +12,20 @@ if (!basicTool.getGlobal("Zotero")[config.addonInstance]) {
defineGlobal("ZoteroPane");
defineGlobal("Zotero_Tabs");
_globalThis.addon = new Addon();
_globalThis.ztoolkit = addon.data.ztoolkit;
defineGlobal("ztoolkit", () => {
return _globalThis.addon.data.ztoolkit;
});
Zotero[config.addonInstance] = addon;
// Trigger addon hook for initialization
addon.hooks.onStartup();
}
function defineGlobal(name: Parameters<BasicTool["getGlobal"]>[0]) {
function defineGlobal(name: Parameters<BasicTool["getGlobal"]>[0]): void;
function defineGlobal(name: string, getter: () => any): void;
function defineGlobal(name: string, getter?: () => any) {
Object.defineProperty(_globalThis, name, {
get() {
return basicTool.getGlobal(name);
return getter ? getter() : basicTool.getGlobal(name);
},
});
}