onMessage event is never called

1

So I am working on my next add-on for Firefox, however, while reading the documentation for ContextMenus, it appears that the onMessage event is never being called, here's my code:

exports.main = function(){
    var addonTab = require('addon-page');
    var data = require('self').data;
    var tabs = require('tabs');
    var cm = require("context-menu");

    cm.Item({
        label: "Tag This Image",
        context: cm.URLContext("*"),
        contextSelector: "img",
        contentScript: 
            'self.on("click", function(node, data){' +
            '   self.postMessage(node);' +
            '});',

        // nothing works...
        onMessage: function(node){
            openImageEditor(node.src);
            alert("Message? It worked? No way!"); 
            onTagImage(node);
        }
    });

    var onTagImage = function(node){
        alert("Image tagged!"); 
    };

    tabs.open({url: data.url('index.html'), isPinned:true});
};

So I take a look at the Error's Console, but this probbaly the worst kind of error format I've ever seen ...

Timestamp: 14/07/2012 3:21:44 Error: An exception occurred. Traceback (most recent call last): File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/addon-kit/lib/context-menu.js", line 1310, in CMP_handleEvent this.handleClick(event.target); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/addon-kit/lib/context-menu.js", line 1339, in CMP_handleClick this.browserWin.fireClick(topLevelItem, popupNode, item.data); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/addon-kit/lib/context-menu.js", line 1162, in BW_fireClick worker.fireClick(popupNode, clickedItemData); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/addon-kit/lib/context-menu.js", line 663, in CMW_fireClick this._contentWorker.emitSync("click", popupNode, clickedItemData); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/worker.js", line 71, in emitSync return this._emitToContent(Array.slice(arguments)); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/content-worker.js", line 96, in onChromeEvent return emit.apply(null, args); File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/content-worker.js", line 45, in onEvent results.push(callback.apply(null, args)); File "javascript:self.on("click", function(node, data){
self.postMessage(node);});", line 1, in null File "resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/content-worker.js", line 81, in onEvent let str = JSON.stringify(args, replacer); [Exception... "Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIImageLoadingContent.loadingEnabled]" nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)" location: "JS frame :: resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/cuddlefish.js -> resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/sandbox.js -> resource://jid0-dxglsws2k0cubycbcn7cw5tcyqk-at-jetpack/api-utils/lib/content/content-worker.js :: onEvent :: line 81" data: no]

Here's the actual line, but this isn't making any sense to me:

File "javascript:self.on("click", function(node, data){self.postMessage(node);});", line 1, in null

Question: What does this error mean, or what is exactly wrong with the said line?

javascript
firefox-addon
firefox-addon-sdk
asked on Stack Overflow Jul 14, 2012 by EliotVU • edited Jul 14, 2012 by Wladimir Palant

1 Answer

2

Your context-menu implementation was a little screwy in a few ways, see this fixed example:

https://builder.addons.mozilla.org/addon/1065630/latest/

  1. the postMessage call from the content script cannot send the html node, it can only send JSON data.

  2. don't use alerts in main.js or other modules loaded from ./lib/ - alert is not defined here. Use console.log instead.

  3. the context you need is context: cm.SelectorContext('img')

answered on Stack Overflow Jul 14, 2012 by therealjeffg

User contributions licensed under CC BY-SA 3.0