1

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
});
7
  • Maybe your message is sent before you listen for it? Commented Jan 20, 2016 at 6:10
  • @Aurélien how can I change it to listen after message is sent? Commented Jan 20, 2016 at 6:26
  • 1
    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? Commented Jan 20, 2016 at 6:31
  • @Aurélien but I've tried send this message many times and nothing Commented Jan 20, 2016 at 6:32
  • 1
    Any error/warning in the browser console window (in the Tools>Web development menu) related to your inline script? Commented Jan 20, 2016 at 6:45

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Given that this is how you solved your problem, please accept your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.