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