Change label in the contextmenu before the opening

(Daniele Scasciafratte) #1

I done (another) extension and this time I got it a problem.
The extension is very simple, the prupose is to show an index of the text quality based on the Gulpease algorithm in the content menu of a textarea.
Actually that value is shown only at the second time that the right menu is showed.

I think that is because the code that I exceute to let communicate the content script with the background happen before the show of the menu, so on the second is showed because was updated before.

I want to use the contextmenu label to show an info on right click in that way I can avoid to inject stuff in the pages that can create design issue but seems that run that code before the opening is like impossible.
Someone has suggestions?


See Implement onBeforeShow event

As a workaround until it’s fixed, listen to “mousedown” and “keydown” events and send message to background.

window.addEventListener("mousedown", evt => {
  if (evt.button === 2) {
}, true);
window.addEventListener("keydown", evt => {
  if (evt.shiftKey && evt.key === "F10" || evt.key === "ContextMenu") {
}, true);

WebExtensions dynamic context menu
(Daniele Scasciafratte) #3

I implemented that but the problem persist :-/


Hmm, it’s working fine in my add-ons.


Your function has a problem.
I got this TypeError.

TypeError: frasi is null  content-script.js:30:7

If I fixed to

  var nF = frasi && frasi.length || 0;

Context menu item got updated.


Also it’s better not to remove

evt.button === 2

(Daniele Scasciafratte) #7

I added that coda but seems not work or me but I will do other tests, thank you for your help!


Your fix is not enough.
frasi is nullable so you need frasi && to make sure it’s not null.

var frasi = text.match(/[\.\:\?\!\;]/g);
var nF = frasi && frasi.length || 0;

or, make frasi non-nullable.

var frasi = text.match(/[\.\:\?\!\;]/g) || [];
var nF = frasi.length;

And evt.button can be integrated with other conditions

if (evt.button === 2 && && === "textarea") {

(Daniele Scasciafratte) #9

Thanks now is working!