Keavon
Keavon

Reputation: 7483

Persistent page action icon

I want to display a notification for the user that will remain constantly visible in the form of a page action until the user does something. I am using this code right now:

chrome.tabs.getSelected(null, function (tab) {
    chrome.pageAction.show(tab.id);
});

But that only creates a page action icon on the active tab when the extension is loaded. Instead, I want the icon to show all the time no matter what page or tab the user is on. It also needs to go away when the user does what is necessary to deal with the notification.

I was thinking of two ideas. The first was looping through and adding a page action to every tab, then hooking the new tab and navigation events and adding it to each of those. My second idea was hooking the active tab change event and adding it to the active tab then removing it from the former tab when changing tabs next.

But I thought that there's still probably a better way I didn't think of or didn't know about. So what's the best way to accomplish this?

Upvotes: 0

Views: 72

Answers (2)

Xan
Xan

Reputation: 77482

You need to hook into the onActivated event if you want to get notified of tab changes.

However, that would not be enough, since the page action will reset on navigation. So you'll need to hook into almost every tabs API event to ensure your logic. Also, think of the cleanup required afterwards.

That really does seem like a poor job for a page action. There is also an important consideration that this UI element is not associated, by a typical user, with something that needs attention. Have you considered using notifications instead?

You could use chrome.notifications Rich Notifications together with the priority trick, or just web notifications. In either case it'll be something displayed to the user in a way that is appropriate for "something needs your attention". You can then hook into its onclick event.

If you do want a button, browserAction is totally appropriate. You can dynamically change picture, add a text badge to the icon to attract attention, or just plain disable the button (not hide, but grey out) when there's nothing to do.

Upvotes: 2

Kristiyan Kostadinov
Kristiyan Kostadinov

Reputation: 3712

According to the documentation, page actions are supposed to be used only for single pages. If you want something to show up on all pages, you should use a browserAction.

Alternatively you can try and set "<all_urls>" in the permissions, but I haven't tested if it actually works.

Upvotes: 1

Related Questions