Context-menu's self.postMessage does not always work with some selectors

RESOLVED INCOMPLETE

Status

P2
normal
RESOLVED INCOMPLETE
6 years ago
a year ago

People

(Reporter: wbamberg, Unassigned)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

6 years ago
This was reported on the forum: https://forums.mozilla.org/addons/viewtopic.php?f=27&t=14005.

There's an example in the context-menu documentation like:

var numClicks = 0;
var myItem = require("sdk/context-menu").Item({
  label: "Click Me: " + numClicks,
  contentScript: 'self.on("click", self.postMessage);',
  onMessage: function () {
    numClicks++;
    this.label = "Click Me: " + numClicks;
    // Setting myItem.label is equivalent.
  }
});

This example works, but if you add a SelectorContext that selects "<textarea>":

var script = 'self.on("click", self.postMessage);'

var myItem = cm.Item({
  label: "Click Me, textarea",
  context: cm.SelectorContext("textarea"),
  contentScript: script,
  onMessage: function () {
    console.log("clicked");
  }
});

...then you see an error in the Error Console like:

Timestamp: 13-03-15 1:49:27 PM
Error: SecurityError: The operation is insecure.
Source File: resource://gre/modules/XPIProvider.jsm -> jar:file:///var/folders/me/me2B1lFDE0WZCgd33s2OTE+++TU/-Tmp-/tmpbt2TDn.mozrunner/extensions/jid1-RFG19SWltJ0qTQ@jetpack.xpi!/bootstrap.js -> resource://jid1-rfg19swltj0qtq-at-jetpack/addon-sdk/lib/toolkit/loader.js -> resource://jid1-rfg19swltj0qtq-at-jetpack/addon-sdk/lib/sdk/loader/sandbox.js -> resource://jid1-rfg19swltj0qtq-at-jetpack/addon-sdk/lib/sdk/content/content-worker.js
Line: 81

...and the main.js does not receive the message.

You *don't* get this error if you use a diferent type of SelectorContext (for example, "<img>".

You also don't get this error if your content script calls self.postMessage() from inside an anonymous function, like this:

var script = 'self.on("click", function() {' +
             '  self.postMessage();' +
             '});';
I bet postMessage is making assumptions about the value of |this| which your example script changes. How about if you try with:

var script = 'self.on("click", self.postMessage.bind(self));'
I was looking over Will's shoulder and it looks like `click` event object or any of its attribute (event.target?) can't be serialized to the addon.
The fun part is that it only fails for textarea and not images...
Priority: -- → P2
https://bugzilla.mozilla.org/show_bug.cgi?id=1399562
Status: NEW → RESOLVED
Last Resolved: a year ago
Resolution: --- → INCOMPLETE
You need to log in before you can comment on or make changes to this bug.