# Change label in the contextmenu before the opening

**URL:** <https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620>\
**Category:** Development\
**Created:** [June 22, 2017, 7:37pm UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620 "2017-06-22T19:37:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mte90](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mte90/32/37835_2.png) [@Mte90](https://discourse.mozilla.org/u/Mte90)\
**Post date:** [June 22, 2017, 7:37pm UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/1 "2017-06-22T19:37:13Z")

</div>

I done (another) extension and this time I got it a problem.  
The extension is very simple [https://github.com/Mte90/Gulpease](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?

---

<div class="post-metadata">

**Author:** ![asamuzaK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/asamuzak/32/18266_2.png) [@asamuzaK](https://discourse.mozilla.org/u/asamuzaK)\
**Post date:** [June 23, 2017, 2:35am UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/2 "2017-06-23T02:35:51Z")

</div>

See [Implement onBeforeShow event](https://bugzilla.mozilla.org/show_bug.cgi?id=1215376 "1215376 - Implement onBeforeShow event")

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

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![Mte90](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mte90/32/37835_2.png) [@Mte90](https://discourse.mozilla.org/u/Mte90)\
**Post date:** [June 23, 2017, 10:35am UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/3 "2017-06-23T10:35:25Z")

</div>

I implemented that but the problem persist :-/

---

<div class="post-metadata">

**Author:** ![asamuzaK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/asamuzak/32/18266_2.png) [@asamuzaK](https://discourse.mozilla.org/u/asamuzaK)\
**Post date:** [June 23, 2017, 11:00am UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/4 "2017-06-23T11:00:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![asamuzaK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/asamuzak/32/18266_2.png) [@asamuzaK](https://discourse.mozilla.org/u/asamuzaK)\
**Post date:** [June 24, 2017, 8:14am UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/5 "2017-06-24T08:14:49Z")

</div>

Your function has a problem.  
I got this TypeError.

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

```

If I fixed to

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

```

Context menu item got updated.

---

<div class="post-metadata">

**Author:** ![asamuzaK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/asamuzak/32/18266_2.png) [@asamuzaK](https://discourse.mozilla.org/u/asamuzaK)\
**Post date:** [June 24, 2017, 8:19am UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/6 "2017-06-24T08:19:12Z")

</div>

Also it’s better not to remove

```auto
evt.button === 2

```

---

<div class="post-metadata">

**Author:** ![Mte90](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mte90/32/37835_2.png) [@Mte90](https://discourse.mozilla.org/u/Mte90)\
**Post date:** [June 25, 2017, 1:52pm UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/7 "2017-06-25T13:52:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![asamuzaK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/asamuzak/32/18266_2.png) [@asamuzaK](https://discourse.mozilla.org/u/asamuzaK)\
**Post date:** [June 26, 2017, 6:19am UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/8 "2017-06-26T06:19:07Z")

</div>

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

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

```

or, make `frasi` non-nullable.

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

```

And `evt.button` can be integrated with other conditions

```auto
if (evt.button === 2 && evt.target.tagName && evt.target.tagName.toLowerCase() === "textarea") {
  gulpease(evt.target.textContent);
}

```

---

<div class="post-metadata">

**Author:** ![Mte90](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mte90/32/37835_2.png) [@Mte90](https://discourse.mozilla.org/u/Mte90)\
**Post date:** [June 27, 2017, 5:30pm UTC](https://discourse.mozilla.org/t/change-label-in-the-contextmenu-before-the-opening/16620/9 "2017-06-27T17:30:40Z")

</div>

Thanks now is working!
