0

I have an iframe in popup.html,having id = Receiver, which receives and posts messages.

The below code is from popup.js:

self.port.on("show", function(title, url) {
    myApp.initialize(title, url);
});


var arr = new Array();
var myApp = {
    initialize: function (url,title){
        arr = [];
        arr.push(url);
        arr.push(title);

        var receiver = document.getElementById('Receiver').contentWindow;
        receiver.postMessage(arr, "*");
    },
    sendDetails : function(){
        alert("arr :"+arr);
    },
    closeIt : function(){
        self.port.emit("close-it");
    }
}  

window.addEventListener("message" , receiveMessageOnce, false);
function receiveMessageOnce(event){
    myApp.closeIt();
}

The code from main.js is :

main_panel.on("show", function() {
    main_panel.port.emit("show", UrlActiveTab, TitleActiveTab);
});

Now, I have 2 questions:
1.)Whenever, it receives the message, myApp.CloseIt() is fired. But the console says that self.port is undefined. I have tried using addon.port that also gives error.
2.) If the myApp.sendDetails() is called, it alerts the value of 'arr' as blank, despite being a global array. Why so?

EDITED: Panel constructor code :

var { ToggleButton } = require('sdk/ui/button/toggle');
const panel = require('sdk/panel');
var main_panel = panel.Panel({
    contentURL: data.url("popup.html"),
    contentScriptFile: [
        data.url('js/jquery-1.10.2.min.js'),
        data.url("js/popup.js")
    ],
    width: 350,
    height: 400
});

var button = ToggleButton({
    id: "myaddon_beta",
    label: "My Addon",
    icon: {
        "16": "./img/icon_main_16.png",
        "32": "./img/icon_main_32.png",
        "64": "./img/icon_main_64.png"
    },
    onChange : handleChange
});

function handleChange(state){
    currentUrl = tabs.activeTab.url;
    currentTitle = tabs.activeTab.title;
    if(state.checked){
        main_panel.show({
            position : button
        });

        button.state("window", {
            checked: false
        });
    }
}
ignVinayak
  • 50
  • 8
  • If self.port is undefined, then initialize is never called, so postMessage will never be called, so receiveMessageOnce wouldn't be called either. If those are being called, as you stated, then self.port must be defined. I agree with @paa below that you should be using addon.port, but as you state you've tried that, I don't see where the problem is. Can we see your panel constructor code? – willlma Jun 09 '14 at 18:02
  • @willlma : Hey added the constructor code. The initialize is working, at first it's all working. But when the 'closeIt()' is fired, then it says, it is undefined – ignVinayak Jun 10 '14 at 04:57
  • Could it be that the `window` context that the `CloseIt` function is being run in has a different `self` object? Try defining a global `var port = self.port`, then making `closeIt` run `port.emit("close-it")`. – willlma Jun 10 '14 at 07:04
  • @willlma : I tried making a variable global, and using that instead of self.port, but that too returns as undefined. Even though it performs the required action, but returns undefined. – ignVinayak Jun 10 '14 at 08:22
  • So main.js is receiving the `"close-it"` event, but if you do `console.log(port)` inside `closeIt()`, it prints `undefined`? That would be extremely weird. Can you update your code to show me where you're logging to the console? Also, you're running this script within the extension environment and not just testing it against an HTML page, right? – willlma Jun 10 '14 at 09:05
  • @willlma: see this: http://s30.postimg.org/xobju3yb5/Snap008.png This error comes when the closeIt is fired. Yes, m running script within extension only. To be more clear, using addon sdk-1.16 – ignVinayak Jun 10 '14 at 09:56
  • Very weird. Not sure what the problem is. If you're willing to send me an xpi, I could try debugging it on my end. – willlma Jun 10 '14 at 18:34
  • @willlma : I'm afraid I cant send you that.Sorry. Will surely contact you if needed more help. Thanks :) – ignVinayak Jun 11 '14 at 05:35

1 Answers1

2

Panels are considered trusted content, because you own them. A side effect of this is that the messaging API is available through the addon global, instead of self as is the case for plain content scripts.

paa
  • 5,048
  • 1
  • 18
  • 22