/**
 * StopFlash
 *
 * https://github.com/JWhile/StopFlash
 *
 * background.js
 */

// class BackgroundFlashCollection
function BackgroundFlashCollection(background, id, port)
{
    this.background = background; // :StopFlashBackground

    this.id = id; // :int

    this.data = []; // :Array<Object>

    this.port = port; // :chrome.runtime.Port

    var self = this;

    port.onMessage.addListener(function(msg)
    {
        if(msg['stopflashDataUpdate'] && msg['stopflashData'])
        {
            self.data = msg.stopflashData;

            self.background.sendToPopup();
        }
    });

    port.onDisconnect.addListener(function()
    {
        self.port = null;
    });
}

// class StopFlashBackground
function StopFlashBackground(id)
{
    this.id = id; // :int

    this.collectionId = 0; // :int

    this.collections = []; // :Array<BackgroundFlashCollection>
}
// function getCollection(int id):BackgroundFlashCollection
StopFlashBackground.prototype.getCollection = function(id)
{
    for(var i = 0; i < this.collections.length; ++i)
    {
        if(this.collections[i].id === id)
        {
            return this.collections[i];
        }
    }

    return null;
};
// function addContentScript(chrome.runtime.Port contentPort):void
StopFlashBackground.prototype.addContentScript = function(contentPort)
{
    var id = ++this.collectionId;

    this.collections.push(new BackgroundFlashCollection(this, id, contentPort));

    contentPort.postMessage({'stopflashWhitelist': whitelist, 'stopflashCollectionId': id});
};
// function sendToPopup():void
StopFlashBackground.prototype.sendToPopup = function()
{
    if(popupPort != null)
    {
        var data = [];

        for(var i = 0; i < this.collections.length; ++i)
        {
            Array.prototype.unshift.apply(data, this.collections[i].data);
        }

        popupPort.postMessage({'stopflashData': data});
    }
};
// function sendToContent(Object msg):void
StopFlashBackground.prototype.sendToContent = function(msg)
{
    for(var i = 0; i < this.collections.length; ++i)
    {
        this.collections[i].port.postMessage(msg);
    }
};
// function clear():void
StopFlashBackground.prototype.clear = function()
{
    if(this.collections.length > 0)
    {
        for(var i = 0; i < this.collections.length; ++i)
        {
            if(this.collections[i].port != null)
            {
                this.collections[i].port.disconnect();
            }
        }
    }

    this.collections = [];
};

var backgrounds = []; // :Array<StopFlashBackground>
var whitelist = []; // :Array<String>
var popupPort = null; // :chrome.runtime.Port

// function getBackground(int id):StopFlashBackground
var getBackground = function(id)
{
    for(var i = 0; i < backgrounds.length; ++i)
    {
        if(backgrounds[i].id === id)
        {
            return backgrounds[i];
        }
    }

    return null;
};

// main
chrome.runtime.onConnect.addListener(function(port)
{
    if(port.name === 'stopflashContentScript' && port.sender.tab != null)
    {
        var data = null;

        port.onMessage.addListener(function(rep)
        {
            if(rep['stopflashInit'])
            {
                data = getBackground(port.sender.tab.id);

                if(data != null)
                {
                    if(rep['stopflashIsMainFrame'])
                    {
                        data.clear();
                    }
                }
                else
                {
                    data = new StopFlashBackground(port.sender.tab.id);

                    backgrounds.push(data);
                }

                data.addContentScript(port);
            }
        });
    }
    else if(port.name === 'stopflashPopup')
    {
        var data = null;

        port.onMessage.addListener(function(rep)
        {
            if(rep['stopflashInit'])
            {
                data = getBackground(rep['stopflashInit']);

                if(data != null)
                {
                    popupPort = port;

                    port.onDisconnect.addListener(function()
                    {
                        popupPort = null;
                    });

                    data.sendToPopup();
                }
            }

            if(rep['stopflashBlock'])
            {
                var collection = data.getCollection(rep['stopflashBlock'].collection);

                if(collection != null)
                {
                    collection.port.postMessage(rep);
                }
            }

            if(rep['stopflashUnblock'])
            {
                var collection = data.getCollection(rep['stopflashUnblock'].collection);

                if(collection != null)
                {
                    collection.port.postMessage(rep);
                }
            }
        });
    }
});

@@ -1554,26 +1554,31 @@
 ion 
-sendToPopup():void
+getData():Array%3CObject%3E
 %0ASto
@@ -1604,27 +1604,23 @@
 ototype.
-sendToPopup
+getData
  = funct
@@ -1631,44 +1631,8 @@
 )%0A%7B%0A
-    if(popupPort != null)%0A    %7B%0A    
     
@@ -1647,20 +1647,16 @@
  = %5B%5D;%0A%0A
-    
     for(
@@ -1692,36 +1692,32 @@
 ns.length; ++i)%0A
-    
     %7B%0A          
@@ -1714,20 +1714,16 @@
         
-    
 Array.pr
@@ -1777,30 +1777,166 @@
 .data);%0A    
+%7D%0A%0A
     
-%7D%0A
+return data;%0A%7D;%0A// function sendToPopup():void%0AStopFlashBackground.prototype.sendToPopup = function()%0A%7B%0A    if(popupPort != null)%0A    %7B
 %0A        pop
@@ -1972,20 +1972,30 @@
 hData': 
-d
+this.getD
 ata
+()
 %7D);%0A    

Add StopFlashBackground::getData() & use