如何使用Addon SDK将消息从main发送到page-worker

时间:2015-08-12 19:54:39

标签: firefox-addon-sdk

这是我的main.js

fillMode

和我的background.js

override func finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes: CellLayoutAttributes = super.finalLayoutAttributesForDisappearingItemAtIndexPath(itemIndexPath) as! CellLayoutAttributes
    // Default is 0, if I set it to 1.0 you don't see anything happen..'
    attributes.alpha = 1
    let endX = -CGRectGetWidth(self.collectionView!.frame)
    var endTransform: CATransform3D = CATransform3DMakeTranslation(endX, 0, 0)
    attributes.transform3D = endTransform

    return attributes
} 

如何从 main.js->发送消息? background.js 以及 background.js-> main.js

(port?postMessage?):任何可用的样本?

1 个答案:

答案 0 :(得分:2)

<强> main.js

// keep track of your active workers
var workers = [];

require("sdk/page-mod").PageMod({
    include: "*",
    contentScriptWhen:'start',
    contentScriptFile: ['./jquery.min.js','./socket.io1.js', './background.js'],
    onAttach: function(worker){

        workers.push(worker);

        worker.on("message", function(aData){
            //messages from background.js
        });

        //remove inactive workers
        worker.on("detach", function(){
            workers.splice(workers.indexOf(worker), 1);
        });

        //messages to background.js 
        worker.postMessage({
            johnny : "oh, hi mark", 
            mark   : "oh, hi johnny"
        });
    }
});

<强> background.js

self.on("message", function(msg){
    //messages from main.js 
});

//messages to main.js   
function post(msg){
    self.postMessage(msg);
}