Question

I am working on a safari extension in which I need to parse a particular array element to each instance of a tab that is created. I, however, need to be able to iterate through the array so that each tab receives a different element to work with in an injected script. I using the receive and send message structure to do this, but I cannot for the life of me figure out how to iterate through the array elements. I tried creating an array that would act as an index, and then incrementing it each time the message responder function was fired, but this didn't work for some reason. I also tried simply shifting the array each time an element was pulled from it, but I believe this didn't work because the function is fired too quickly as tabs are created.

I want to be able to use some sort of enumerator function on each injected script instance to figure out the tab number and then parse that with the message to the global page to return the proper element in the array.

Thanks so much for any and all help.

Was it helpful?

Solution

I think you are trying to iterate through all tabs in all windows -- please clarify which array you are having trouble with. In order to iterate through all of them, you should be able to do it like this, first through the windows in the application, then through the tabs:

var bWindows = safari.application.browserWindows;
for(i=0;i<bWindows.length;i++){
    var tabs = bWindows[i].tabs;
    for(j=0;j<tabs.length;j++){
         var tab = tabs[j];
         //Do something in each tab.
         tab.page.dispatchMessage('message', data);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top