2019-08-13 17:40:54 +02:00
|
|
|
import { SafariApp } from './safariApp';
|
|
|
|
|
2019-08-21 16:29:14 +02:00
|
|
|
import { Utils } from 'jslib/misc/utils';
|
|
|
|
|
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 = navigator.userAgent.indexOf('Firefox/') !== -1 &&
|
|
|
|
navigator.userAgent.indexOf('Android') !== -1;
|
2018-01-12 05:04:47 +01:00
|
|
|
|
2017-12-07 21:06:37 +01:00
|
|
|
static async getTabFromCurrentWindowId(): Promise<any> {
|
2021-02-03 20:36:05 +01:00
|
|
|
return await BrowserApi.tabsQueryFirst({
|
|
|
|
active: true,
|
|
|
|
windowId: chrome.windows.WINDOW_ID_CURRENT,
|
|
|
|
});
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static async getTabFromCurrentWindow(): Promise<any> {
|
|
|
|
return await BrowserApi.tabsQueryFirst({
|
|
|
|
active: true,
|
|
|
|
currentWindow: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-04 04:48:38 +01:00
|
|
|
static async getActiveTabs(): Promise<any[]> {
|
|
|
|
return await BrowserApi.tabsQuery({
|
|
|
|
active: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-16 16:54:19 +02:00
|
|
|
static async tabsQuery(options: any): Promise<any[]> {
|
2021-02-10 16:40:15 +01:00
|
|
|
return new Promise(resolve => {
|
2021-02-03 20:36:05 +01:00
|
|
|
chrome.tabs.query(options, (tabs: any[]) => {
|
|
|
|
resolve(tabs);
|
2017-12-07 21:06:37 +01:00
|
|
|
});
|
2021-02-03 20:36:05 +01:00
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-16 18:51:06 +02:00
|
|
|
static async tabSendMessage(tab: any, obj: any, options: any = null): Promise<any> {
|
2018-01-12 17:32:42 +01:00
|
|
|
if (!tab || !tab.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:40:15 +01:00
|
|
|
return new Promise(resolve => {
|
2021-02-03 20:36:05 +01:00
|
|
|
chrome.tabs.sendMessage(tab.id, obj, options, () => {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
// Some error happened
|
|
|
|
}
|
|
|
|
resolve();
|
2017-12-07 21:36:24 +01:00
|
|
|
});
|
2021-02-03 20:36:05 +01:00
|
|
|
});
|
2017-12-07 21:36:24 +01:00
|
|
|
}
|
2018-01-12 05:04:47 +01:00
|
|
|
|
|
|
|
static getBackgroundPage(): any {
|
2021-02-03 20:36:05 +01:00
|
|
|
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 {
|
2021-02-03 20:36:05 +01:00
|
|
|
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> {
|
2021-02-03 20:36:05 +01:00
|
|
|
return Promise.resolve(chrome.extension.getViews({ type: 'popup' }).length > 0);
|
2018-01-12 17:09:30 +01:00
|
|
|
}
|
|
|
|
|
2021-03-17 22:14:26 +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
|
|
|
}
|
|
|
|
|
2019-08-15 22:36:49 +02:00
|
|
|
static messageListener(name: string, callback: (message: any, sender: any, response: any) => void) {
|
2021-02-03 20:36:05 +01:00
|
|
|
chrome.runtime.onMessage.addListener((msg: any, sender: any, response: any) => {
|
|
|
|
callback(msg, sender, response);
|
|
|
|
});
|
2018-01-12 17:32:42 +01:00
|
|
|
}
|
2018-01-12 21:53:37 +01:00
|
|
|
|
2018-01-12 22:42:47 +01:00
|
|
|
static closePopup(win: Window) {
|
2018-08-15 03:27:55 +02:00
|
|
|
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);
|
2021-02-03 20:36:05 +01:00
|
|
|
} 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) {
|
2019-08-21 16:29:14 +02:00
|
|
|
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');
|
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);
|
|
|
|
}
|
2018-01-14 05:57:58 +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';
|
2018-01-26 16:48:32 +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();
|
2018-04-11 20:52:49 +02:00
|
|
|
}
|
2019-03-05 21:05:04 +01:00
|
|
|
|
2019-04-04 17:50:49 +02:00
|
|
|
static reloadExtension(win: Window) {
|
|
|
|
if (win != null) {
|
2019-03-05 21:05:04 +01:00
|
|
|
return win.location.reload(true);
|
2021-02-03 20:36:05 +01:00
|
|
|
} else {
|
2019-03-05 21:05:04 +01:00
|
|
|
return chrome.runtime.reload();
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 21:49:01 +02:00
|
|
|
|
|
|
|
static reloadOpenWindows() {
|
2021-02-03 20:36:05 +01:00
|
|
|
const views = chrome.extension.getViews() as Window[];
|
2021-02-10 16:40:15 +01:00
|
|
|
views.filter(w => w.location.href != null).forEach(w => {
|
2021-02-03 20:36:05 +01:00
|
|
|
w.location.reload();
|
|
|
|
});
|
2020-08-17 21:49:01 +02:00
|
|
|
}
|
2020-10-09 17:16:15 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2021-03-10 21:27:05 +01:00
|
|
|
|
|
|
|
static requestPermission(permission: any) {
|
|
|
|
if (BrowserApi.isWebExtensionsApi) {
|
|
|
|
return browser.permissions.request(permission);
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
chrome.permissions.request(permission, resolve);
|
|
|
|
});
|
|
|
|
}
|
2021-04-06 23:10:22 +02:00
|
|
|
|
|
|
|
static getPlatformInfo(): Promise<browser.runtime.PlatformInfo | chrome.runtime.PlatformInfo> {
|
|
|
|
if (BrowserApi.isWebExtensionsApi) {
|
|
|
|
return browser.runtime.getPlatformInfo();
|
|
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
chrome.runtime.getPlatformInfo(resolve);
|
|
|
|
});
|
|
|
|
}
|
2017-12-07 21:06:37 +01:00
|
|
|
}
|