Giter Club home page Giter Club logo

Comments (4)

sindresorhus avatar sindresorhus commented on June 15, 2024

The issue is that to enable this, the definition needs to happen in the content script

Why does it have to happen in the content script?

from webext-options-sync.

fregante avatar fregante commented on June 15, 2024

Why does it have to happen in the content script?

If the defaults are defined in the background script, we'd have to set up messaging. Also all set calls would also have to be sent to the background script to see if they need to be filtered.

A simpler solution: we put the defaults in the storage itself, so they can will be accessible anytime anywhere — but then we need another solution for filters.

Note: I described the wrong type of filter earlier, they should be pre-save filters, not pre-get.

However pre-get filters would be easier to implement since the filters can just live in the content, not necessarily in the original define call:

/* background.js */
new OptionsSync().define({
  defaults: {
    name: "User"
  }
});

/* content.js */
const options = new OptionsSync().define({
  filters: {
    name: name => name.trim()
  }
});

options.getAll().__defaults__ === {name: "User"}; // Stored at define() time

options.getAll().name === 'filtered User'; // Filtered at getAll() time

from webext-options-sync.

fregante avatar fregante commented on June 15, 2024

How about this: instead of calling new OptionsSync() in every file (definition in background, usage in content, change in options), the user should have a single options-storage file that gets included everywhere.

Example:

// options-storage.js
export default new OptionsSync({
  defaults: {
    name: "User"
  },
  filters: {
    name: name => `Hello, ${name}`
  }
});
// content or background
import options from './options-storage';

await options.getAll();
// options.html
import options from "./options-storage";

options.syncForm("#options-form");

Or without webpack:

	"background": {
		"scripts": [
			"webext-options-sync.js",
			"options-storage.js",
			"background.js"
		],
		"persistent": false
	},
	"content_scripts": [
		{
			"matches": [
				"https://github.com/*",
			],
			"js": [
				"webext-options-sync.js",
				"options-storage.js",
				"content.js"
			]
		}

I think this is the right way, I just need to find a reliable background.js detection so migrations are only run there.

This also helps TypeScript definitions because the type of defaults gets automatically passed as .getAll's ReturnType

from webext-options-sync.

sindresorhus avatar sindresorhus commented on June 15, 2024

I think that's a good solution. It's how I recommend using electron-store too, so people get the same in the main and renderer process for Electron.

from webext-options-sync.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.