Skip to content
This repository has been archived by the owner on Dec 24, 2024. It is now read-only.

Commit

Permalink
fix sometimes config will lost
Browse files Browse the repository at this point in the history
  • Loading branch information
harytfw committed Jun 1, 2022
1 parent 0082023 commit 52d4eaa
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 21 deletions.
21 changes: 12 additions & 9 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,18 +636,21 @@ browser.runtime.onInstalled.addListener(async (details) => {
let changedflag = false;
const all = await (browser.storage.local.get());

// WARNING: temporary workaround to fix unknown config lost
function assign(target, origin) {
if (typeof target !== 'object' || typeof origin !== 'object') {
return
}
for (const aKey of Object.keys(origin)) {
if (aKey in target) {
if (typeof target[aKey] === "object") {
assign(target[aKey], origin[aKey]);
}
}
else {
$D(aKey, " ", target[aKey], " -> ", origin[aKey]);
target[aKey] = origin[aKey];
// console.log(aKey, origin[aKey]);
if (typeof target[aKey] === "object" && typeof origin[aKey] === 'object') {
assign(target[aKey], origin[aKey]);
} else if (typeof target[aKey] !== typeof origin[aKey]) {
// two side have different type, use default value
target[aKey] = JSON.parse(JSON.stringify(origin[aKey]));
changedflag = true;
} else {
// ignore
// both side have some type
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description": "__MSG_extensionDescription__",
"manifest_version": 2,
"name": "__MSG_extensionName__",
"version": "1.56.9",
"version": "1.56.10",
"homepage_url": "https://github.com/harytfw/GlitterDrag",
"icons": {
"128": "/icon/drag.png"
Expand Down
103 changes: 92 additions & 11 deletions src/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,13 +562,13 @@ class EngineItemWrapper {
return this.nameInput.value;
}
set name(n) {
return this.nameInput.value = n;
this.nameInput.value = n;
}
get url() {
return this.urlInput.value;
}
set url(s) {
return this.urlInput.value = s;
this.urlInput.value = s;
}
get value() {
return {
Expand Down Expand Up @@ -1544,16 +1544,68 @@ class ExcludedRulesWrapper {
function initTabs() {


new ActionsWrapper();
new NewActionsWrapper();
try {
console.info('actions tab')
new ActionsWrapper();
} catch (e) {
console.error(e)
}

try {
console.info('new actions tab')
new NewActionsWrapper();
} catch (e) {
console.error(e)
}

try {
console.info('engine tab')
new EngineWrapper();
} catch (e) {
console.error(e)
}

try {
console.info('general setting tab')
new generalSettingWrapper();
} catch (e) {
console.error(e)
}

new EngineWrapper();
new generalSettingWrapper();
new downloadWrapper();
new styleWrapper();
new PanelWrapper();
new TranslatorWrapper();
new ExcludedRulesWrapper();
try {
console.info('download tab')
new downloadWrapper();
} catch (e) {
console.error(e)
}

try {
console.info('style tab')
new styleWrapper();
} catch (e) {
console.error(e)
}

try {
console.info('panel')
new PanelWrapper();
} catch (e) {
console.error(e)
}

try {
console.info('translator')
new TranslatorWrapper();
} catch (e) {
console.error(e)
}

try {
console.info('excluded rules')
new ExcludedRulesWrapper();
} catch (e) {
console.error(e)
}

doI18n();

Expand Down Expand Up @@ -1629,6 +1681,33 @@ function initButtons() {
}
})
}



// WARNING: temporary workaround to fix unknown config lost
async function assignDefaultConfig() {
const storage = await LStorage.get()
const assign = (target, origin) => {
if (typeof target !== 'object' || typeof origin !== 'object') {
return
}
for (const aKey of Object.keys(origin)) {
if (typeof target[aKey] === "object" && typeof origin[aKey] === 'object') {
assign(target[aKey], origin[aKey]);
} else if (typeof target[aKey] !== typeof origin[aKey]) {
// two side have different type, use default value
target[aKey] = JSON.parse(JSON.stringify(origin[aKey]));
} else {
// ignore
// both side have some type
}
}
}

assign(storage, DEFAULT_CONFIG)
await LStorage.set(storage)
}

var browserMajorVersion = 52;
browser.runtime.getBrowserInfo().then(async info => {
browserMajorVersion = info.version.split(".")[0];
Expand Down Expand Up @@ -1656,6 +1735,8 @@ browser.runtime.getBrowserInfo().then(async info => {
configurable: false,
})

await assignDefaultConfig()

initButtons();
initTabs();
});

0 comments on commit 52d4eaa

Please sign in to comment.