Keep state of browser action

1,266 views
Skip to first unread message

shiweiwei

unread,
Jun 5, 2011, 12:00:54 AM6/5/11
to Chromium-extensions
Hi,

It looks like the popup html page is reloaded each time when user
clicks the browser action icon. So everthing is reset when browser
action is closed and reopened. Is it possible to keep the state of
browser action so user can have a continuous experience?

Thanks,
Weiwei

Arne Roomann-Kurrik

unread,
Jun 6, 2011, 2:31:51 PM6/6/11
to shiweiwei, Chromium-extensions
You could write any state to the background page or localStorage and fetch it when the popup opens.

~Arne



--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Gildas

unread,
Jun 6, 2011, 6:28:05 PM6/6/11
to Chromium-extensions

shiweiwei

unread,
Jun 6, 2011, 9:17:53 PM6/6/11
to Chromium-extensions
That is an option and it can solve most of my problems.
Since the data is read locally the popup reloading will be pretty
fast.

But still not an ideal solution because I can store everything in
localstorage.
e.g. user enters some text in a input box, or scroll to some position
of the popup
or clicks a button which in turn makes an ajax call.

Thanks,
Weiwei

On Jun 7, 2:31 am, Arne Roomann-Kurrik <kur...@chromium.org> wrote:
> You could write any state to the background page or localStorage and fetch
> it when the popup opens.
>
> ~Arne
>
>
>
>
>
>
>
> On Sat, Jun 4, 2011 at 9:00 PM, shiweiwei <shiweiwe...@gmail.com> wrote:
> > Hi,
>
> > It looks like the popup html page is reloaded each time when user
> > clicks the browser action icon. So everthing is reset when browser
> > action is closed and reopened. Is it possible to keep the state of
> > browser action so user can have a continuous experience?
>
> > Thanks,
> > Weiwei
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org.

shiweiwei

unread,
Jun 6, 2011, 9:24:27 PM6/6/11
to Chromium-extensions

I am not sure I understand this correctly but the final conclusion is
the "magic ifram" didn't solve the problem.

I don't know if anyone made the request before.
But I think it does make sense to add a "persist" option to browser
action.
If "persist" is set to true, the popup is treated like another normal
tab, open/close the popup just means switching between the current tab
and the popup tab.

Thanks,
Weiwei

On Jun 7, 6:28 am, Gildas <gildas.lorm...@gmail.com> wrote:
> There's also a discussion about "magic iframe" trick (not tested)
> which may help to keep popup state:
>
> https://groups.google.com/a/chromium.org/group/chromium-extensions/br...

shiweiwei

unread,
Jun 6, 2011, 9:26:00 PM6/6/11
to Chromium-extensions
sorry, "I can store everything" should be "I cannot store everything"

On Jun 7, 9:17 am, shiweiwei <shiweiwe...@gmail.com> wrote:
> That is an option and it can solve most of my problems.
> Since the data is read locally the popup reloading will be pretty
> fast.
>
> But still not an ideal solution because I can store everything in
> localstorage.
> e.g. user enters some text in a input box, or scroll to some position
> of the popup
> or clicks a button which in turn makes an ajax call.
>
> Thanks,
> Weiwei
>
> On Jun 7, 2:31 am, Arne Roomann-Kurrik <kur...@chromium.org> wrote:
>
>
>
>
>
>
>
> > You could write any state to the background page or localStorage and fetch
> > it when the popup opens.
>
> > ~Arne
>
> > On Sat, Jun 4, 2011 at 9:00 PM, shiweiwei <shiweiwe...@gmail.com> wrote:
> > > Hi,
>
> > > It looks like the popup html page is reloaded each time when user
> > > clicks thebrowseractionicon. So everthing is reset whenbrowser
> > >actionis closed and reopened. Is it possible to keep the state of
> > >browseractionso user can have a continuous experience?

Arne Roomann-Kurrik

unread,
Jun 7, 2011, 12:33:55 PM6/7/11
to shiweiwei, Chromium-extensions
In this case, I'd say you should just create a new window and allow the user to switch back and forth from that. 

~Arne


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Gildas

unread,
Jun 7, 2011, 4:07:24 PM6/7/11
to Chromium-extensions
On Jun 7, 3:24 am, shiweiwei <shiweiwe...@gmail.com> wrote:
> I am not sure I understand this correctly but the final conclusion is
> the "magic ifram" didn't solve the problem.

Actually, it seems to work for me. Try this extension code for
example :

manifest.json:
{
"name": "SavePopupContent",
"version": "0.0.1",
"description": "POC for Saving a popup content",
"background_page" : "background.html",
"browser_action": {
"default_popup": "popup.html"
}
}

background.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Background page</title>
</head>
<body></body>
</html>

popup.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Popup page</title>
</head>
<body>
<em>Randoms!</em>
<hr>
<script src="popup.js"></script>
</body>
</html>


popup.js:
var bgPage = chrome.extension.getBackgroundPage();

// listeners to save/restore document.body
window.addEventListener("unload", function(event) {
chrome.extension.getBackgroundPage().docBody = document.body;
});

window.addEventListener("load", function(event) {
var
docBody = document.body,
lastDocBody = bgPage.docBody;
if (lastDocBody)

docBody.parentElement.replaceChild(document.importNode(lastDocBody,true),
docBody);
});

// code example
window.addEventListener("load", function(event) {
document.body.innerHTML += Math.floor(Math.random() * 10) + "<br>";
});

Each time you will open the popup, a new random number will be added
into the "persistent" popup.

If you want to keep variables then all you have to do is to store
variables in the "bgPage" variable.
Reply all
Reply to author
Forward
0 new messages