Comments (4)
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.
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.
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.
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)
- `syncForm` should trigger event HOT 1
- Migrations/defaults aren't set for developer extensions at launch
- `chrome.storage.sync.set` errors aren't caught
- Add mechanism to show that field was saved HOT 1
- Latest version (0.21.2) causes error "Uncaught (in promise)" HOT 2
- Add config for compressing options before chrome.sync.set HOT 6
- Remove defaults from stored options
- Form values set to `undefined` HOT 3
- Race condition in OptionsSync constructor HOT 1
- Migrations receive the old options + the new defaults
- Sync options to a third party HOT 4
- Add true ES module
- Move from Travis to GitHub Actions HOT 1
- Values from the store are cleared on page refresh (when trying to save `Set`) HOT 4
- Async migrations HOT 1
- Migrations fail on Safari
- Options type does not allow nested object or array keys HOT 3
- Allow post-processing of form data HOT 2
- "Change" event to subscribe to so extension pages can update each other HOT 2
- Use `onContextInvalidated` to add banner inviting the user to reload the page (or just reload it)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from webext-options-sync.