get current tab apis for safari

This commit is contained in:
Kyle Spearrin 2018-01-12 13:22:24 -05:00
parent 9597e76e92
commit bb2b750905
1 changed files with 44 additions and 8 deletions

View File

@ -3,10 +3,14 @@ class BrowserApi {
static isChromeApi: boolean = (typeof chrome !== 'undefined');
static async getTabFromCurrentWindowId(): Promise<any> {
if (BrowserApi.isChromeApi) {
return await BrowserApi.tabsQueryFirst({
active: true,
windowId: chrome.windows.WINDOW_ID_CURRENT,
});
} else if (BrowserApi.isSafariApi) {
return await BrowserApi.getTabFromCurrentWindow();
}
}
static async getTabFromCurrentWindow(): Promise<any> {
@ -17,11 +21,43 @@ class BrowserApi {
}
static tabsQuery(options: any): Promise<any[]> {
if (BrowserApi.isChromeApi) {
return new Promise((resolve) => {
chrome.tabs.query(options, (tabs: any[]) => {
resolve(tabs);
});
});
} else if (BrowserApi.isSafariApi) {
let win: any = null;
if (options.currentWindow) {
win = safari.application.activeBrowserWindow;
}
if (!win || !win.tabs || !win.tabs.length) {
return Promise.resolve([]);
}
const tabs: any[] = [];
if (options.active && win.activeTab) {
tabs.push(win.activeTab);
}
const returnedTabs: any[] = [];
tabs.forEach((tab: any) => {
const winIndex = safari.application.browserWindows.indexOf(tab.browserWindow);
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);
}
}
static async tabsQueryFirst(options: any): Promise<any> {