make tab object for message sender

This commit is contained in:
Kyle Spearrin 2018-01-12 15:53:37 -05:00
parent 06c8f17d06
commit 729380c0ec
1 changed files with 23 additions and 13 deletions

View File

@ -44,16 +44,7 @@ class BrowserApi {
const returnedTabs: any[] = []; const returnedTabs: any[] = [];
tabs.forEach((tab: any) => { tabs.forEach((tab: any) => {
const winIndex = safari.application.browserWindows.indexOf(tab.browserWindow); returnedTabs.push(BrowserApi.makeTabObject(tab));
const tabIndex = tab.browserWindow.tabs.indexOf(tab);
returnedTabs.push({
id: winIndex + '_' + tabIndex,
index: tabIndex,
windowId: winIndex,
title: tab.title,
active: tab === tab.browserWindow.activeTab,
url: tab.url || 'about:blank',
});
}); });
return Promise.resolve(returnedTabs); return Promise.resolve(returnedTabs);
@ -169,14 +160,33 @@ class BrowserApi {
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
safari.application.addEventListener('message', async (msgEvent: any) => { safari.application.addEventListener('message', async (msgEvent: any) => {
callback(msgEvent.message, { callback(msgEvent.message, {
tab: { tab: BrowserApi.makeTabObject(msgEvent.target),
id: null, // TODO
},
frameId: null, frameId: null,
}, () => { /* No responses in Safari */ }); }, () => { /* No responses in Safari */ });
}, false); }, false);
} }
} }
private static makeTabObject(tab: any) {
if (BrowserApi.isChromeApi) {
return tab;
}
if (!tab.browserWindow) {
return {};
}
const winIndex = safari.application.browserWindows.indexOf(tab.browserWindow);
const tabIndex = tab.browserWindow.tabs.indexOf(tab);
return {
id: winIndex + '_' + tabIndex,
index: tabIndex,
windowId: winIndex,
title: tab.title,
active: tab === tab.browserWindow.activeTab,
url: tab.url || 'about:blank',
};
}
} }
export { BrowserApi }; export { BrowserApi };