0

I am working on a chrome extension that used to work perfectly, and now there seems to be a new chrome extension behavior regarding badge text truncation. The extension shows your window width as you resize directly in the badge. When your window is smaller than 1000 the numbers render correctly. I have learned the following.

  • If you pass 4 letters as a string the 4 letters render correctly.
  • If I send 3 numbers it renders correctly.
  • If I send 4 numbers it truncates.

I need the 4 numbers to render, and I am not able to find any solid answer as to what/why this behavior has changed.

Here is the background.js code.

updateBadge();

function updateBadge() {
 chrome.windows.getCurrent(function(w) {
  currentWidth = w.width;
  chrome.browserAction.setBadgeBackgroundColor({ color: [100, 100, 100, 255] });
  chrome.browserAction.setBadgeText({ text: currentWidth.toString() });
 });
}

chrome.commands.onCommand.addListener(function(command) {
 chrome.windows.getCurrent(function(w) {
  currentWidth = w.width;
  currentHeight = w.height;

  switch(command) {
   case 'width_minus_1':
    var newWidth = currentWidth - 1;
    break;
   case 'width_minus_10':
    var newWidth = currentWidth - 10;
    break;
   case 'width_plus_1':
    var newWidth = currentWidth + 1;
    break;
   case 'width_plus_10':
    var newWidth = currentWidth + 10;
    break;
   case 'height_minus_1':
    var newHeight = currentHeight - 1;
    break;
   case 'height_minus_10':
    var newHeight = currentHeight - 10;
    break;
   case 'height_plud_1':
    var newHeight = currentHeight + 1;
    break;
   case 'height_plus_10':
    var newHeight = currentHeight + 10;
    break;
  }

  if (typeof newWidth == 'number') {
   chrome.windows.update(chrome.windows.WINDOW_ID_CURRENT, { width: newWidth });
   chrome.browserAction.setBadgeText({ text: newWidth.toString() });
  }

  if (typeof newHeight == 'number') {
   chrome.windows.update(chrome.windows.WINDOW_ID_CURRENT, { height: newHeight });
   chrome.browserAction.setBadgeText({ text: newHeight.toString() });
  }
 });
});

chrome.extension.onMessage.addListener(function(m) {
 if (m.action == 'resized') {
  updateBadge();
 }
});

Thanks for taking the time to lend a hand.

Blumed
  • 95
  • 1
  • 6
  • I think it is up to chrome to decide how many characters are allowed in the badge. I too had come across this question some time ago, but didn't find anything that allowed developer to control the badge length. – Nisarg Shah Aug 15 '17 at 03:55
  • I found this https://stackoverflow.com/a/5759182/4321339 stackoverflow ansewer but it is the only place I found with any specific detail to this specific behavior. Looked everywhere in chrome's documentation and can't find any info on this specific behavior. Again, the 4 numbers used to work about 6 months ago and now it truncates. – Blumed Aug 15 '17 at 04:02
  • The behavior changed because the browser UI was redesigned. Why? Well, just because. The number truncation sounds like a bug though, consider reporting on https://crbug.com – wOxxOm Aug 15 '17 at 04:06
  • @wOxxOm True, I agree that it sounds like a bug. I thought I would do my best here first before reporting. I looked at http://crbug.com and didn't find anyone reporting it. Maybe today is the day :) – Blumed Aug 15 '17 at 04:07

0 Answers0