1
votes

I'm trying to communicate between my main Add-on SDK script, index.js, and a script in a page-worker created with sdk/page-worker. Unfortunately, I am having problems.

I am able to communicate from my main script to the page-worker: In the code below, I get an event from socket.on - console.log('new post') fires from the script in the page-worker. However, the main add-on script does not see the message that is sent from the page-worker.

HTML (worker.html):

<html lang="pl">
    <head>
        <meta charset="UTF-8">

        <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
        <script type="text/javascript">
            var socket = io.connect('http://192.168.0.21:8081');
            socket.on('newPost', function(data) {
                console.log("new post");
                //to this everything works ok!!!!!!!!!!!!
                self.port.emit('newPost', "");
            });
        </script>
    </head>
    <body>

    </body>
</html>

to index.js file:

var buttons = require('sdk/ui/button/action');
var functions = require('./module.js');
var pageWorkers = require("sdk/page-worker");
var notifications = require('sdk/notifications');
var self = require("sdk/self");
functions.act.checkMsgsCount();

var pageWorker = pageWorkers.Page({
    contentScriptWhen: "ready",
    contentURL: self.data.url("worker.html")
});

pageWorker.port.on('newPost', function(data) {
    console.log("sent newPost");
    functions.act.newPostNotify();
});

buttons.ActionButton({
    id: "main-bt",
    label: "Start",
    icon: {
        "16" : "./icon-16.png",
        "32" : "./icon-32.png",
        "64" : "./icon-64.png"
    },    
    onClick: functions.act.checkMsgsCount
});
1
Maybe your message is sent before you listen for it? - Aurélien
@Aurélien how can I change it to listen after message is sent? - pawel__86
Maybe by delaying a bit your inline script execution ("on ready")... But before doing this, could you test a console log after your pageWorker.port.on, in order to see if it is fired before "new post" log? - Aurélien
@Aurélien but I've tried send this message many times and nothing - pawel__86
Any error/warning in the browser console window (in the Tools>Web development menu) related to your inline script? - Aurélien

1 Answers

2
votes

In worker.html, I had to change self.port.emit to: addon.port.emit.

Because I used a script in my HTML file within a <script> tag, I have to communicate with the main add-on script through the addon object.