2018-01-12 17:09:30 +01:00
|
|
|
class BrowserApi {
|
2018-01-12 05:04:47 +01:00
|
|
|
static isSafariApi: boolean = (typeof safari !== 'undefined');
|
|
|
|
static isChromeApi: boolean = (typeof chrome !== 'undefined');
|
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
static async getTabFromCurrentWindowId(): Promise<any> {
|
2018-01-12 19:22:24 +01:00
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
return await BrowserApi.tabsQueryFirst({
|
|
|
|
active: true,
|
|
|
|
windowId: chrome.windows.WINDOW_ID_CURRENT,
|
|
|
|
});
|
|
|
|
} else if (BrowserApi.isSafariApi) {
|
|
|
|
return await BrowserApi.getTabFromCurrentWindow();
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static async getTabFromCurrentWindow(): Promise<any> {
|
|
|
|
return await BrowserApi.tabsQueryFirst({
|
|
|
|
active: true,
|
|
|
|
currentWindow: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static tabsQuery(options: any): Promise<any[]> {
|
2018-01-12 19:22:24 +01:00
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.tabs.query(options, (tabs: any[]) => {
|
|
|
|
resolve(tabs);
|
|
|
|
});
|
2017-12-07 21:06:37 +01:00
|
|
|
});
|
2018-01-12 19:22:24 +01:00
|
|
|
} 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);
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static async tabsQueryFirst(options: any): Promise<any> {
|
|
|
|
const tabs = await BrowserApi.tabsQuery(options);
|
|
|
|
if (tabs.length > 0) {
|
|
|
|
return tabs[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-12-07 21:36:24 +01:00
|
|
|
|
2018-01-12 17:32:42 +01:00
|
|
|
static tabSendMessageData(tab: any, command: string, data: any = null): Promise<any[]> {
|
2017-12-07 21:36:24 +01:00
|
|
|
const obj: any = {
|
|
|
|
command: command,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (data != null) {
|
|
|
|
obj.data = data;
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:32:42 +01:00
|
|
|
return BrowserApi.tabSendMessage(tab, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static tabSendMessage(tab: any, obj: any): Promise<any[]> {
|
|
|
|
if (!tab || !tab.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.tabs.sendMessage(tab.id, obj, () => {
|
|
|
|
resolve();
|
|
|
|
});
|
2017-12-07 21:36:24 +01:00
|
|
|
});
|
2018-01-12 17:32:42 +01:00
|
|
|
} else if (BrowserApi.isSafariApi) {
|
|
|
|
// TODO
|
|
|
|
}
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
2018-01-12 05:04:47 +01:00
|
|
|
|
|
|
|
static getBackgroundPage(): any {
|
2018-01-12 05:39:16 +01:00
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
return chrome.extension.getBackgroundPage();
|
|
|
|
} else if (BrowserApi.isSafariApi) {
|
|
|
|
return safari.extension.globalPage.contentWindow;
|
|
|
|
} else {
|
|
|
|
return null;
|
2018-01-12 05:04:47 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 16:05:30 +01:00
|
|
|
|
2018-01-12 16:41:01 +01:00
|
|
|
static getApplicationVersion(): string {
|
2018-01-12 16:05:30 +01:00
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
return chrome.runtime.getManifest().version;
|
|
|
|
} else if (BrowserApi.isSafariApi) {
|
2018-01-12 16:07:27 +01:00
|
|
|
return safari.extension.displayVersion;
|
2018-01-12 16:05:30 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 17:09:30 +01:00
|
|
|
|
|
|
|
static isPopupOpen(): boolean {
|
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
return chrome.extension.getViews({ type: 'popup' }).length > 0;
|
|
|
|
} else if (BrowserApi.isSafariApi) {
|
|
|
|
return true; // TODO
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static createNewTab(url: string): void {
|
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
chrome.tabs.create({ url: url });
|
|
|
|
} else if (BrowserApi.isSafariApi) {
|
|
|
|
return; // TODO
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static getAssetUrl(path: string): string {
|
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
return chrome.extension.getURL(path);
|
|
|
|
} else if (BrowserApi.isSafariApi) {
|
|
|
|
return './' + path; // TODO?
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 17:32:42 +01:00
|
|
|
|
|
|
|
static messageListener(callback: (message: any, sender: any, response: any) => void) {
|
|
|
|
if (BrowserApi.isChromeApi) {
|
|
|
|
chrome.runtime.onMessage.addListener((msg: any, sender: any, response: any) => {
|
|
|
|
callback(msg, sender, response);
|
|
|
|
});
|
|
|
|
} else if (BrowserApi.isSafariApi) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
2018-01-12 17:09:30 +01:00
|
|
|
|
|
|
|
export { BrowserApi };
|
|
|
|
(window as any).BrowserApi = BrowserApi;
|