bitwarden-estensione-browser/src/browser/browserApi.ts

197 lines
5.9 KiB
TypeScript
Raw Normal View History

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');
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();
}
}
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);
});
});
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) => {
2018-01-12 21:53:37 +01:00
returnedTabs.push(BrowserApi.makeTabObject(tab));
2018-01-12 19:22:24 +01:00
});
return Promise.resolve(returnedTabs);
}
}
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
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;
}
return BrowserApi.tabSendMessage(tab, obj);
}
static tabSendMessage(tab: any, obj: any, options: any = null): Promise<any> {
if (!tab || !tab.id) {
return;
}
if (BrowserApi.isChromeApi) {
return new Promise((resolve) => {
chrome.tabs.sendMessage(tab.id, obj, options, () => {
if (chrome.runtime.lastError) {
// Some error happened
}
resolve();
});
2017-12-07 21:36:24 +01:00
});
} else if (BrowserApi.isSafariApi) {
const win = safari.application.activeBrowserWindow;
if (safari.application.browserWindows.indexOf(win) !== tab.windowId) {
return Promise.reject('Window not found.');
}
2018-01-12 21:20:19 +01:00
if (win.tabs.length < tab.index + 1) {
return Promise.reject('Tab not found.');
}
2018-01-12 21:20:19 +01:00
const t = win.tabs[tab.index];
if (t.page) {
t.page.dispatchMessage('bitwarden', obj);
2018-01-12 22:33:36 +01:00
}
return Promise.resolve();
}
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;
}
}
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) {
2018-01-12 21:29:01 +01:00
safari.application.addEventListener('message', async (msgEvent: any) => {
callback(msgEvent.message, {
2018-01-12 21:53:37 +01:00
tab: BrowserApi.makeTabObject(msgEvent.target),
2018-01-12 21:29:01 +01:00
frameId: null,
}, () => { /* No responses in Safari */ });
}, false);
}
}
2018-01-12 21:53:37 +01:00
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',
};
}
}
2018-01-12 17:09:30 +01:00
export { BrowserApi };
(window as any).BrowserApi = BrowserApi;