WebExtension - Using '%s' in context menu adds '...' to selection

Hello,

Currently trying to make a simple WebExtension/addon and ran into an issue. I tried the regular forums first but was redirected here.

I’m trying to use ‘%s’ to show the user marked text in the context menu.

This MDN page stated that it would literally show the marked text.

"You can use “%s” in the string. If you do this, and some text is selected in the page when the context menu is shown, then the selected text will be interpolated into the title. For example, if title is “Translate ‘%s’ to Pig Latin” and the user selects the word “cool”, then activates the context menu, then the context menu item’s title will be:

"Translate 'cool' to Pig Latin"."

It, however, adds ‘…’ to the selected text. I’ve already updated the MDN page to reflect my findings: (…)context menu item’s title will be:

"Translate 'cool...' to Pig Latin".

I’ve already tried everything, truncation, string replacement, none of it is working because ‘%s’ in the title field is evaluated last.

My background.js:

browser.contextMenus.create({
  id: "log-selection",
  title: "Log '%s' to the console",
  contexts: ["selection"]
});

browser.contextMenus.onClicked.addListener(function(info, tab) {
  if (info.menuItemId == "log-selection") {
    console.log(info.selectionText);
  }
});

manifest.json:

{

  "manifest_version": 2,
  "name": "Selection Logger",
  "version": "1.0",

  "description": "Add selection to context menu to print with console",

  "background": {
    "scripts": [
      "background.js"
    ]
  },

  "permissions": [
    "contextMenus"
  ],

  "applications": {
    "gecko": {
      "id": "myTest@tester.com"
    }
  }

}

Extra testing
I’ve tried using ‘%s’ in chrome and it works flawlessly there, literal copy of selected text without the ‘…’. Not sure why Firefox is not playing along. Tried both on regular build and on Nightly.

Any ideas why it’s not working? Thanks in advance.

Hm. Firefox truncates the substituted text if the total length of the label would be to long.
What happens if you set ">'%s'<" as the label and select a single letter? (To make sure it is definitely not to long)

Thanks for your reply NilkasG.

If I use:
">'%s'<"
as a tag and select the letter A, it gives:
>'A...'<

:frowning: no dice.
To me it seems as if Firefox itself is somehow adding ... to every value of %s.

I think it is pretty save to das this is a bug. You should file it.
I also don’t think that there is any workaround for this.

1 Like

Thanks, I filed a report here. Leaving this up in case anybody else has an idea for a work around.