Skip to content

Commit

Permalink
fix: json pref parsing error
Browse files Browse the repository at this point in the history
fix: #884
  • Loading branch information
windingwind committed Jul 25, 2024
1 parent 3e7d6e4 commit 2d4b4d5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/modules/defaultPrefs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getService, SERVICES } from "../utils/config";
import { clearPref, getPref, setPref } from "../utils/prefs";
import { clearPref, getPref, getPrefJSON, setPref } from "../utils/prefs";
import { setServiceSecret } from "../utils/secret";

export function setDefaultPrefSettings() {
Expand All @@ -17,7 +17,7 @@ export function setDefaultPrefSettings() {
setPref("targetLanguage", Zotero.locale);
}

const secrets = JSON.parse((getPref("secretObj") as string) || "{}");
const secrets = getPrefJSON("secretObj");
for (const serviceId of servicesIds) {
if (typeof secrets[serviceId] === "undefined") {
secrets[serviceId] = getService(serviceId).defaultSecret || "";
Expand Down
4 changes: 2 additions & 2 deletions src/modules/services/cnki.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { aesEcbEncrypt, base64 } from "../../utils/crypto";
import { getPref, setPref } from "../../utils/prefs";
import { getPref, getPrefJSON, setPref } from "../../utils/prefs";
import { TranslateTaskProcessor } from "../../utils/task";

export default <TranslateTaskProcessor>async function (data) {
Expand Down Expand Up @@ -69,7 +69,7 @@ async function getToken(forceRefresh: boolean = false) {
// Just in case the update fails
let doRefresh = true;
try {
const tokenObj = JSON.parse(getPref("cnkiToken") as string);
const tokenObj = getPrefJSON("cnkiToken");
if (
!forceRefresh &&
tokenObj?.token &&
Expand Down
4 changes: 2 additions & 2 deletions src/modules/services/haici.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getPref, setPref } from "../../utils/prefs";
import { getPref, getPrefJSON, setPref } from "../../utils/prefs";
import { TranslateTaskProcessor } from "../../utils/task";

export default <TranslateTaskProcessor>async function (data) {
Expand Down Expand Up @@ -31,7 +31,7 @@ async function getAppId(forceRefresh: boolean = false) {
// Just in case the update fails
let doRefresh = true;
try {
const appIdObj = JSON.parse(getPref("haiciAppId") as string);
const appIdObj = getPrefJSON("haiciAppId");
if (
!forceRefresh &&
appIdObj &&
Expand Down
9 changes: 9 additions & 0 deletions src/utils/prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ export function setPref(key: string, value: string | number | boolean) {
export function clearPref(key: string) {
return Zotero.Prefs.clear(`${config.prefsPrefix}.${key}`, true);
}

export function getPrefJSON(key: string) {
try {
return JSON.parse(String(getPref(key) || "{}"));
} catch (e) {
setPref(key, "{}");
}
return {};
}
6 changes: 3 additions & 3 deletions src/utils/secret.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SecretValidateResult, getService } from "./config";
import { getPref, setPref } from "./prefs";
import { getPref, getPrefJSON, setPref } from "./prefs";

export function getServiceSecret(serviceId: string) {
try {
return JSON.parse(getPref("secretObj") as string)[serviceId] || "";
return getPrefJSON("secretObj")[serviceId] || "";
} catch (e) {
setPref("secretObj", "{}");
return "";
Expand All @@ -13,7 +13,7 @@ export function getServiceSecret(serviceId: string) {
export function setServiceSecret(serviceId: string, secret: string) {
let secrets;
try {
secrets = JSON.parse(getPref("secretObj") as string) || {};
secrets = getPrefJSON("secretObj");
} catch (e) {
secrets = {};
}
Expand Down

0 comments on commit 2d4b4d5

Please sign in to comment.