0

I need to set a text at top of icon buts coming down as of now. I am using using setbadgetext in chrome extension as follows

chrome.tabs.executeScript(tabId, {code: chrome.browserAction.setBadgeText ( { text: 'hi' } )}, function() {
});

So the text hi appear to the bottom right of extension icon.i want it at the top.

Please suggest.Any js hack or chrome additions welcome.

sachleen
  • 30,730
  • 8
  • 78
  • 73
Nitin
  • 1
  • 1
  • 6

1 Answers1

2

You can't adjust the position of the text in the badge.

You will need to draw an icon on the fly (canvas) with text in the proper place and set it dynamically.

From the chrome.browserAction docs:

ImageDataType

( imagedata ) Pixel data for an image. Must be an ImageData object (for example, from a canvas element).

Example from Dynamic Chrome Extension Icons:

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
// ...draw to the canvas...
var imageData = context.getImageData(0, 0, 19, 19);
chrome.browserAction.setIcon({
  imageData: imageData
});

Also see Drawing text using a canvas

sachleen
  • 30,730
  • 8
  • 78
  • 73
  • Nopes.i need to set a text as a dynamic count of unread notifications.Wont work in above codes. – Nitin Mar 05 '13 at 10:41
  • Have you tried it? It will work... the icon has text written on it. You have to think outside the box; using the given API's won't work. – sachleen Mar 05 '13 at 17:35
  • I have a working extension with notification count getting updated dynamically at bottom of extension icon.just want to on top ! – Nitin Mar 06 '13 at 03:44
  • You can't. Not with the standard APIs. But what you **can** do is abandon the `setBadgeText` call and *draw* the text on the actual icon, and then set the icon. That's what my answer describes. – sachleen Mar 06 '13 at 03:47