Cyrillou
Cyrillou

Reputation: 161

Chrome extension: How to do not lose state of my extension when user changes/reloads the page

I'm currently developing an extension that comes in different parts: - background script (Chrome background.js - content script (Chrome content-script.js) - popup script (popup.js and loaded from the iFrame) - iframe.html (iframe to be loaded when user activate the extension)

It works great except that I lose all the settings/context (used in the popup script to display info in the iframe) when the user change of page or reload the page in the tab.

Technically I want to isolate the extension per tab (each tab can have its own config) but when a user navigate in a same tab the context should be kept.

Question is: what is the best way to achieve that?

I'm thinking about persisting all the context in the content script and reload it when the event chrome.tabs.onUpdated is fired. Is it the right approach?

Upvotes: 1

Views: 2111

Answers (1)

Max Kurtz
Max Kurtz

Reputation: 478

I recommend you to read about chrome.storage. You can store data there, both background and content script have access to it. And probably you will need chrome.runtime.sendMessage and chrome.runtime.onMessage to have conversation between background and content. Each time content script runs in a tab it can request background whether this tab have some existing data or not.

Upvotes: 2

Related Questions