I have a problem to safe an state from popUp to the local storage in chrome extension. I am doing a checkbox and I would like that when I click on it, changes the status on storage and informs in content. Also I would like that when I open a new tab it keeps the state of the checkbox, taking the information from the storage, and if i close the browser and i open, i would like that keep the information of the last time it was open.
this is my Popup:
const PopUp = () => {
const [checked, setchecked] = useState(false);
chrome.storage.sync.get('checked', storage => {
setchecked(storage.checked);
});
const handleChange = () => {
setchecked(status => !status);
chrome.storage.sync.set({ checked });
};
return (
<div>
<input type='checkbox' checked={checked} onChange={handleChange} />
</div>
);
};
and this is my content.js
(() => {
chrome.storage.sync.get('checked', storage => {
toggle(storage.checked);
});
chrome.storage.onChanged.addisExtensionActiveListener(storage => {
toggle(storage.checked);
});
const toggle = (state) {
if (state) {
//do something
}
}
})();
in the background I have that, if can help:
const getSomethingFromContent = () => {
const getValue = activeInfo => {
chrome.tabs.get(activeInfo.tabId, () => {
chrome.tabs.executeScript({ file: 'content.js' });
});
};
chrome.tabs.onActivated.addListener(getValue);
chrome.extension.onActivated.removeListener(getValue);
};
getSomethingFromContent();
thanks in advance