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 https://github.com/Mte90/Gulpease, 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?


#2

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) {
    browser.runtime.sendMessage();
  }
}, true);
window.addEventListener("keydown", evt => {
  if (evt.shiftKey && evt.key === "F10" || evt.key === "ContextMenu") {
    browser.runtime.sendMessage();
  }
}, true);

WebExtensions dynamic context menu
(Daniele Scasciafratte) #3

I implemented that but the problem persist :-/


#4

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


#5

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.


#6

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!


#8

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 && evt.target.tagName && evt.target.tagName.toLowerCase() === "textarea") {
  gulpease(evt.target.textContent);
}

(Daniele Scasciafratte) #9

Thanks now is working!