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

209 lines
6.7 KiB
TypeScript
Raw Normal View History

2019-08-13 17:40:54 +02:00
import { SafariApp } from './safariApp';
import { Utils } from 'jslib-common/misc/utils';
2018-01-20 20:05:20 +01:00
export class BrowserApi {
static isWebExtensionsApi: boolean = (typeof browser !== 'undefined');
static isSafariApi: boolean = navigator.userAgent.indexOf(' Safari/') !== -1 &&
navigator.userAgent.indexOf(' Chrome/') === -1 &&
navigator.userAgent.indexOf(' Chromium/') === -1;
2018-01-16 03:59:25 +01:00
static isChromeApi: boolean = !BrowserApi.isSafariApi && (typeof chrome !== 'undefined');
2018-08-15 03:29:32 +02:00
static isFirefoxOnAndroid: boolean = navigator.userAgent.indexOf('Firefox/') !== -1 &&
navigator.userAgent.indexOf('Android') !== -1;
2018-01-12 05:04:47 +01:00
static async getTabFromCurrentWindowId(): Promise<chrome.tabs.Tab> | null {
return await BrowserApi.tabsQueryFirst({
active: true,
windowId: chrome.windows.WINDOW_ID_CURRENT,
});
}
static async getTabFromCurrentWindow(): Promise<chrome.tabs.Tab> | null {
return await BrowserApi.tabsQueryFirst({
active: true,
currentWindow: true,
});
}
static async getActiveTabs(): Promise<chrome.tabs.Tab[]> {
2018-03-04 04:48:38 +01:00
return await BrowserApi.tabsQuery({
active: true,
});
}
static async tabsQuery(options: chrome.tabs.QueryInfo): Promise<chrome.tabs.Tab[]> {
2021-02-10 16:40:15 +01:00
return new Promise(resolve => {
chrome.tabs.query(options, (tabs: any[]) => {
resolve(tabs);
});
});
}
static async tabsQueryFirst(options: chrome.tabs.QueryInfo): Promise<chrome.tabs.Tab> | null {
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);
}
2019-08-16 18:51:06 +02:00
static async tabSendMessage(tab: any, obj: any, options: any = null): Promise<any> {
if (!tab || !tab.id) {
return;
}
return new Promise<void>(resolve => {
chrome.tabs.sendMessage(tab.id, obj, options, () => {
if (chrome.runtime.lastError) {
// Some error happened
}
resolve();
2017-12-07 21:36:24 +01:00
});
});
2017-12-07 21:36:24 +01:00
}
2018-01-12 05:04:47 +01:00
static getBackgroundPage(): any {
return chrome.extension.getBackgroundPage();
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 {
return chrome.runtime.getManifest().version;
2018-01-12 16:05:30 +01:00
}
2018-01-12 17:09:30 +01:00
2019-08-20 19:54:10 +02:00
static async isPopupOpen(): Promise<boolean> {
return Promise.resolve(chrome.extension.getViews({ type: 'popup' }).length > 0);
2018-01-12 17:09:30 +01:00
}
static createNewTab(url: string, extensionPage: boolean = false, active: boolean = true) {
chrome.tabs.create({ url: url, active: active });
2018-01-12 17:09:30 +01:00
}
static messageListener(name: string, callback: (message: any, sender: any, response: any) => void) {
chrome.runtime.onMessage.addListener((msg: any, sender: any, response: any) => {
callback(msg, sender, response);
});
}
2018-01-12 21:53:37 +01:00
static async closeLoginTab() {
const tabs = await BrowserApi.tabsQuery({
active: true,
title: 'Bitwarden',
windowType: 'normal',
currentWindow: true,
});
if (tabs.length === 0) {
return;
}
const tabToClose = tabs[tabs.length - 1].id;
chrome.tabs.remove(tabToClose);
}
static async focusSpecifiedTab(tabId: number) {
chrome.tabs.update(tabId, { active: true, highlighted: true });
}
2018-01-12 22:42:47 +01:00
static closePopup(win: Window) {
if (BrowserApi.isWebExtensionsApi && BrowserApi.isFirefoxOnAndroid) {
2018-08-15 03:32:13 +02:00
// Reactivating the active tab dismisses the popup tab. The promise final
// condition is only called if the popup wasn't already dismissed (future proofing).
// ref: https://bugzilla.mozilla.org/show_bug.cgi?id=1433604
2018-08-15 03:29:32 +02:00
browser.tabs.update({ active: true }).finally(win.close);
} else {
2018-01-12 22:42:47 +01:00
win.close();
}
}
2018-01-15 16:01:25 +01:00
static downloadFile(win: Window, blobData: any, blobOptions: any, fileName: string) {
2018-01-15 17:04:28 +01:00
if (BrowserApi.isSafariApi) {
const type = blobOptions != null ? blobOptions.type : null;
let data: string = null;
if (type === 'text/plain' && typeof (blobData) === 'string') {
data = blobData;
} else {
data = Utils.fromBufferToB64(blobData);
}
2019-08-22 02:45:23 +02:00
SafariApp.sendMessageToApp('downloadFile', JSON.stringify({
2019-08-23 15:55:59 +02:00
blobData: data,
blobOptions: blobOptions,
fileName: fileName,
2019-08-22 02:45:23 +02:00
}), true);
2018-01-15 16:01:25 +01:00
} else {
const blob = new Blob([blobData], blobOptions);
2018-01-15 17:04:28 +01:00
if (navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName);
} else {
const a = win.document.createElement('a');
a.href = URL.createObjectURL(blob);
2018-01-15 17:04:28 +01:00
a.download = fileName;
win.document.body.appendChild(a);
a.click();
win.document.body.removeChild(a);
}
}
}
2018-01-26 16:48:32 +01:00
static gaFilter() {
return process.env.ENV !== 'production';
2018-01-26 16:48:32 +01:00
}
2018-04-11 20:52:49 +02:00
static getUILanguage(win: Window) {
return chrome.i18n.getUILanguage();
2018-04-11 20:52:49 +02:00
}
2019-03-05 21:05:04 +01:00
static reloadExtension(win: Window) {
if (win != null) {
2019-03-05 21:05:04 +01:00
return win.location.reload(true);
} else {
2019-03-05 21:05:04 +01:00
return chrome.runtime.reload();
}
}
static reloadOpenWindows() {
const views = chrome.extension.getViews() as Window[];
2021-02-10 16:40:15 +01:00
views.filter(w => w.location.href != null).forEach(w => {
w.location.reload();
});
}
static connectNative(application: string): browser.runtime.Port | chrome.runtime.Port {
if (BrowserApi.isWebExtensionsApi) {
return browser.runtime.connectNative(application);
} else if (BrowserApi.isChromeApi) {
return chrome.runtime.connectNative(application);
}
}
static requestPermission(permission: any) {
if (BrowserApi.isWebExtensionsApi) {
return browser.permissions.request(permission);
}
return new Promise((resolve, reject) => {
chrome.permissions.request(permission, resolve);
});
}
static getPlatformInfo(): Promise<browser.runtime.PlatformInfo | chrome.runtime.PlatformInfo> {
if (BrowserApi.isWebExtensionsApi) {
return browser.runtime.getPlatformInfo();
}
return new Promise(resolve => {
chrome.runtime.getPlatformInfo(resolve);
});
}
}