mirror of
https://github.com/zen-browser/components.git
synced 2025-07-08 13:39:58 +02:00
Merge pull request #10 from JosueGalRe/main
feature(zenThemesImporter): write saved preferences to dom on startup
This commit is contained in:
commit
6a7d1046fb
1 changed files with 61 additions and 1 deletions
|
@ -36,8 +36,9 @@ var gZenThemeImporter = new (class {
|
||||||
constructor() {
|
constructor() {
|
||||||
console.info('ZenThemeImporter: Initiating Zen theme importer');
|
console.info('ZenThemeImporter: Initiating Zen theme importer');
|
||||||
try {
|
try {
|
||||||
window.SessionStore.promiseInitialized.then(() => {
|
window.SessionStore.promiseInitialized.then(async () => {
|
||||||
this.insertStylesheet();
|
this.insertStylesheet();
|
||||||
|
await this.writeToDom();
|
||||||
});
|
});
|
||||||
console.info('ZenThemeImporter: Zen theme imported');
|
console.info('ZenThemeImporter: Zen theme imported');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -108,9 +109,68 @@ var gZenThemeImporter = new (class {
|
||||||
async updateStylesheet() {
|
async updateStylesheet() {
|
||||||
this.removeStylesheet();
|
this.removeStylesheet();
|
||||||
await this.writeStylesheet();
|
await this.writeStylesheet();
|
||||||
|
await this.writeToDom();
|
||||||
this.insertStylesheet();
|
this.insertStylesheet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_getBrowser() {
|
||||||
|
if (!this.__browser) {
|
||||||
|
this.__browser = Services.wm.getMostRecentWindow("navigator:browser")
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.__browser
|
||||||
|
}
|
||||||
|
|
||||||
|
async _getThemePreferences(theme) {
|
||||||
|
const themePath = PathUtils.join(this.getThemeFolder(theme), 'preferences.json');
|
||||||
|
|
||||||
|
if (!(await IOUtils.exists(themePath)) || !theme.preferences) {
|
||||||
|
return { preferences: [], isLegacyMode: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
let preferences = await IOUtils.readJSON(themePath);
|
||||||
|
|
||||||
|
// skip transformation, we won't be writing old preferences to dom, all of them can only be checkboxes
|
||||||
|
if (typeof preferences === "object" && !Array.isArray(preferences)) {
|
||||||
|
return { preferences: [], areOldPreferences: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { preferences, areOldPreferences: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
async writeToDom() {
|
||||||
|
const browser = this._getBrowser()
|
||||||
|
|
||||||
|
for (const theme of Object.values(await this.getThemes())) {
|
||||||
|
const { preferences, areOldPreferences } = await this._getThemePreferences(theme);
|
||||||
|
|
||||||
|
if (areOldPreferences) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const themePreferences = preferences.filter(({ type }) => type === "dropdown")
|
||||||
|
|
||||||
|
for (const { property } of themePreferences) {
|
||||||
|
const value = Services.prefs.getStringPref(property, "")
|
||||||
|
|
||||||
|
if (value !== "") {
|
||||||
|
let element = browser.document.getElementById(theme.name)
|
||||||
|
|
||||||
|
if (!element) {
|
||||||
|
element = browser.document.createElement("div")
|
||||||
|
|
||||||
|
element.style.display = "none"
|
||||||
|
element.setAttribute("id", theme.name)
|
||||||
|
|
||||||
|
browser.document.body.appendChild(element)
|
||||||
|
}
|
||||||
|
|
||||||
|
element.setAttribute(property, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async writeStylesheet() {
|
async writeStylesheet() {
|
||||||
const themes = [];
|
const themes = [];
|
||||||
this._themes = null;
|
this._themes = null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue