Skip to content

Commit

Permalink
Settings rework
Browse files Browse the repository at this point in the history
  • Loading branch information
exdis committed Apr 20, 2024
1 parent 88bb6fd commit e320b51
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/content/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ function getIframe(settings) {
// settings
app.setDarkmode(settings.darkmode);
app.defineAction('getSettings', () => settings);
app.defineAction('setSettings', settings => {
chrome.storage.sync.set(settings);
});

// upload data
app.uploadData(new ReadableStream({
Expand Down
20 changes: 10 additions & 10 deletions src/discovery/pages/settings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { utils } from '@discoveryjs/discovery';
import { flashMessage } from '../flash-messages';

export default host => {
host.view.define('label', function(el, config = {}) {
Expand Down Expand Up @@ -74,8 +75,12 @@ export default host => {
}
].map(content => ({ view: 'fieldset', content }));

host.page.define('settings', function(el, data, context) {
const { settings } = context;
host.page.define('settings', async function(el, data) {
if (!data) {
return;
}

const settings = await host.query('"getSettings".callAction()', data);

host.view.render(el, [
'h1:"JsonDiscovery settings"',
Expand Down Expand Up @@ -103,16 +108,11 @@ export default host => {
const { valid, errors } = validate(settings);

if (valid) {
if (typeof chrome !== 'undefined') {
chrome.storage.sync.set(settings);
} else if (typeof safari !== 'undefined') {
safari.extension.dispatchMessage('setSettings', settings);
}
host.query(`"setSettings".callAction(${JSON.stringify(settings)})`, host.data);

host.context.settings = settings;
host.flashMessage('Options saved.', 'success');
flashMessage('Options saved.', 'success');
} else {
host.flashMessage(errors.join(' '), 'danger');
flashMessage(errors.join(' '), 'danger');
}
}

Expand Down

0 comments on commit e320b51

Please sign in to comment.