Created
May 22, 2023 12:40
-
-
Save lexisother/b247598c458945fb992b3499c9218f81 to your computer and use it in GitHub Desktop.
Migrating Dark Reader settings from one place to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To use any of the code here, inspect the background view from your browser's extension page | |
// Chrome: | |
// 1. chrome://extensions | |
// 2. "Inspect views background/index.html" | |
// Firefox: | |
// 1. about:debugging#/runtime/this-firefox | |
// 2. Inspect button | |
// To look at any options you may want to carry over, log all settings | |
chrome.storage.sync.get(null, console.log); | |
// Get what you need from the logged object | |
// Necessary setup {{{ | |
function prepareSyncStorage(values) { | |
for (const key in values) { | |
const value = values[key]; | |
const string = JSON.stringify(value); | |
const totalLength = string.length + key.length; | |
if (totalLength > chrome.storage.sync.QUOTA_BYTES_PER_ITEM) { | |
const maxLength = | |
chrome.storage.sync.QUOTA_BYTES_PER_ITEM - key.length - 1 - 2; | |
const minimalKeysNeeded = Math.ceil(string.length / maxLength); | |
for (let i = 0; i < minimalKeysNeeded; i++) { | |
values[`${key}_${i.toString(36)}`] = string.substring( | |
i * maxLength, | |
(i + 1) * maxLength | |
); | |
} | |
values[key] = { | |
__meta_split_count: minimalKeysNeeded, | |
}; | |
} | |
} | |
return values; | |
} | |
async function writeSyncStorage(values) { | |
return new Promise(async (resolve, reject) => { | |
const packaged = prepareSyncStorage(values); | |
chrome.storage.sync.set(packaged, () => { | |
if (chrome.runtime.lastError) { | |
reject(chrome.runtime.lastError); | |
return; | |
} | |
resolve(); | |
}); | |
}); | |
} | |
async function saveSyncSetting(sync) { | |
const obj = {syncSettings: sync}; | |
try { | |
await writeSyncStorage(obj); | |
} catch (err) { | |
console.error( | |
"Settings synchronization was disabled due to error:", | |
chrome.runtime.lastError | |
); | |
} | |
} | |
// }}} | |
// Now, you can simply call `writeSyncStorage` with your settings object | |
writeSyncStorage({ siteList: ["google.com"] }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment