setBadgeText on empty tab gets reset

438 views
Skip to first unread message

Brett Slatkin

unread,
Mar 18, 2010, 10:27:19 PM3/18/10
to Chromium-extensions
Heyo,

I'm trying to set the badge text on my browser action each time a new
window or tab opens or moves between windows. This works fine if the
tab has navigated to an address. But when showing the "new tab" page
(which has an empty location in the url bar) the call to setBadgeText
will cause the browser action to have the badge for a momentary flash
and then it's reset back to having no badge. Is this intentional?

The code:

var allNumbers = 0;

function assignBadge(tabId, windowId) {
var number = allNumbers + 1;
allNumbers++;
chrome.browserAction.setBadgeText({
"text": "" + number,
"tabId": tabId,
});
chrome.browserAction.setTitle({
"title": "Window #" + number,
"tabId": tabId,
});
}

chrome.tabs.onAttached.addListener(function(tabId, attachInfo) {
assignBadge(tabId, attachInfo.newWindowId);
});
chrome.tabs.onCreated.addListener(function(tab) {
assignBadge(tab.id, tab.windowId);
});

Thanks,

-Brett

Aaron Boodman

unread,
Mar 19, 2010, 12:23:44 AM3/19/10
to Brett Slatkin, Chromium-extensions
Hi Brett,

It is somewhat intentional. The "tabId" property to setBadgeText() is
somewhat misnamed. It is really the tabId, but only until a navigation
occurs. At navigation, these tab-specific properties automatically
reset.

What you are seeing is that a tab gets created, and initially its URL
is about:blank (all new tabs initially have this URL). Then shortly
thereafter it changes to chrome://newtab.

What exactly are you trying to do?

- a

> --
> 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.
>
>

Brett Slatkin

unread,
Mar 19, 2010, 2:47:04 PM3/19/10
to Aaron Boodman, Chromium-extensions
Hey Aaron,

Thanks for the quick reply.

On Thu, Mar 18, 2010 at 9:23 PM, Aaron Boodman <a...@google.com> wrote:
> It is somewhat intentional. The "tabId" property to setBadgeText() is
> somewhat misnamed. It is really the tabId, but only until a navigation
> occurs. At navigation, these tab-specific properties automatically
> reset.
>
> What you are seeing is that a tab gets created, and initially its URL
> is about:blank (all new tabs initially have this URL). Then shortly
> thereafter it changes to chrome://newtab.
>
> What exactly are you trying to do?

I'm doing a window/tab manager extension. I'm trying to set the badge
text for each window as it opens. The badge text labels each window
with a number (1, 2, 3, ...) and my extension will allow users to move
tabs between these windows using hot-keys that correspond to the
numbers. If the numbers aren't there then users don't know what
they're doing.

Is it possible to run content scripts on chrome:// URLs? Perhaps I
could just add a document_end content script for setting the badge
text and then it would also activate on the new tab page? Right now
I'm trying to work around this with setTimeout(), but that's kinda
hacky.

Thanks again,

-Brett

Aaron Boodman

unread,
Mar 19, 2010, 2:54:00 PM3/19/10
to Brett Slatkin, Chromium-extensions
On Fri, Mar 19, 2010 at 11:47 AM, Brett Slatkin <bsla...@gmail.com> wrote:
> Hey Aaron,
>
> Thanks for the quick reply.
>
> On Thu, Mar 18, 2010 at 9:23 PM, Aaron Boodman <a...@google.com> wrote:
>> It is somewhat intentional. The "tabId" property to setBadgeText() is
>> somewhat misnamed. It is really the tabId, but only until a navigation
>> occurs. At navigation, these tab-specific properties automatically
>> reset.
>>
>> What you are seeing is that a tab gets created, and initially its URL
>> is about:blank (all new tabs initially have this URL). Then shortly
>> thereafter it changes to chrome://newtab.
>>
>> What exactly are you trying to do?
>
> I'm doing a window/tab manager extension. I'm trying to set the badge
> text for each window as it opens. The badge text labels each window
> with a number (1, 2, 3, ...) and my extension will allow users to move
> tabs between these windows using hot-keys that correspond to the
> numbers. If the numbers aren't there then users don't know what
> they're doing.
>
> Is it possible to run content scripts on chrome:// URLs? Perhaps I
> could just add a document_end content script for setting the badge
> text and then it would also activate on the new tab page? Right now
> I'm trying to work around this with setTimeout(), but that's kinda
> hacky.

Content scripts can't run on chrome:// URLs.

The event you want to use to reset the badge text after it clears is
chrome.tabs.onUpdated, filtering for the case where changeInfo
includes the "url" property.

It sounds like maybe you can keep some internal state in your
extension mapping tabIds (or windowIds?) to badge text, then you just
have to reset it when necessary.

I'm not sure if this will result in flickering though.

Let me know how it goes,

- a

Brett Slatkin

unread,
Mar 19, 2010, 3:42:08 PM3/19/10
to Aaron Boodman, Chromium-extensions
>> I'm doing a window/tab manager extension. I'm trying to set the badge
>> text for each window as it opens. The badge text labels each window
>> with a number (1, 2, 3, ...) and my extension will allow users to move
>> tabs between these windows using hot-keys that correspond to the
>> numbers. If the numbers aren't there then users don't know what
>> they're doing.
>>
>> Is it possible to run content scripts on chrome:// URLs? Perhaps I
>> could just add a document_end content script for setting the badge
>> text and then it would also activate on the new tab page? Right now
>> I'm trying to work around this with setTimeout(), but that's kinda
>> hacky.
>
> Content scripts can't run on chrome:// URLs.
>
> The event you want to use to reset the badge text after it clears is
> chrome.tabs.onUpdated, filtering for the case where changeInfo
> includes the "url" property.
>
> It sounds like maybe you can keep some internal state in your
> extension mapping tabIds (or windowIds?) to badge text, then you just
> have to reset it when necessary.
>
> I'm not sure if this will result in flickering though.

chrome.tabs.onUpdated works when checking for changeInfo.status ==
"complete". It flickers once but that's okay. Thanks!

-Brett

Reply all
Reply to author
Forward
0 new messages