Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to call setBadgeText on pageAction in a chrome extension?

I'm trying to set the badge text for my chrome extension with the code below:

chrome.pageAction.setBadgeText({"text":'3'})

This throws an error Object #<Object> has no method setBadgeText.

like image 204
vishu Avatar asked Apr 10 '13 05:04

vishu


People also ask

What is Chrome browseraction?

Description. Use browser actions to put icons in the main Google Chrome toolbar, to the right of the address bar. In addition to its icon, a browser action can have a tooltip, a badge, and a popup.

What is browser action icon?

A browser action is a button that your extension adds to the browser's toolbar. The button has an icon, and may optionally have a popup whose content is specified using HTML, CSS, and JavaScript.


2 Answers

I have used the canvas feature to get the same result. Here the code that i used.

window.setInterval(function() {
  chrome.pageAction.setIcon({imageData: draw(10, 0), tabId: tabId});
}, 1000);

function draw(starty, startx) {
  var canvas = document.getElementById('canvas');
  var context = canvas.getContext('2d');
    var img = new Image();
    img.src = "icon_16.png"
    img.onload = function () {
       context.drawImage(img,0,2);
    }
  //context.clearRect(0, 0, canvas.width, canvas.height);
  context.fillStyle = "rgba(255,0,0,1)";
  context.fillRect(startx % 19, starty % 19, 10, 10);
  context.fillStyle = "white";
  context.font = "11px Arial";
  context.fillText("3",0,19);
  return context.getImageData(0, 0, 19, 19);
}

here you can chage the positon and color of text you want to show.

like image 112
vishu Avatar answered Oct 24 '22 16:10

vishu


No, it is not possible with page actions

But it is possible with browser actions.

Excerpt:

page action docs

Parts of the UI Like browser actions, page actions can have an icon, a tooltip, and popup; they can't have badges, however. In addition, page actions can be grayed out. You can find information about icons, tooltips, and popups by reading about the browser action UI.

What you can do is to replace the current image with another that has a badge included in the image.

like image 23
Legends Avatar answered Oct 24 '22 14:10

Legends