more safari APIs
This commit is contained in:
parent
6b507c9071
commit
2b6c2bd4e2
|
@ -1,4 +1,7 @@
|
||||||
export default class BrowserApi {
|
export default class BrowserApi {
|
||||||
|
static isSafariApi: boolean = (typeof safari !== 'undefined');
|
||||||
|
static isChromeApi: boolean = (typeof chrome !== 'undefined');
|
||||||
|
|
||||||
static async getTabFromCurrentWindowId(): Promise<any> {
|
static async getTabFromCurrentWindowId(): Promise<any> {
|
||||||
return await BrowserApi.tabsQueryFirst({
|
return await BrowserApi.tabsQueryFirst({
|
||||||
active: true,
|
active: true,
|
||||||
|
@ -49,4 +52,16 @@ export default class BrowserApi {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getBackgroundPage(): any {
|
||||||
|
function getBackgroundPage(): any {
|
||||||
|
if (BrowserApi.isChromeApi) {
|
||||||
|
return chrome.extension.getBackgroundPage();
|
||||||
|
} else if (BrowserApi.isSafariApi) {
|
||||||
|
return safari.extension.globalPage.contentWindow;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,3 +3,4 @@ declare function escape(s: string): string;
|
||||||
declare function unescape(s: string): string;
|
declare function unescape(s: string): string;
|
||||||
declare var opr: any;
|
declare var opr: any;
|
||||||
declare var chrome: any;
|
declare var chrome: any;
|
||||||
|
declare var safari: any;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
import BrowserApi from '../../../browser/browserApi';
|
||||||
|
|
||||||
import { ConstantsService } from 'jslib/services/constants.service';
|
import { ConstantsService } from 'jslib/services/constants.service';
|
||||||
|
|
||||||
import { ApiService } from 'jslib/abstractions/api.service';
|
import { ApiService } from 'jslib/abstractions/api.service';
|
||||||
|
@ -22,7 +24,7 @@ import { AutofillService } from '../../../services/abstractions/autofill.service
|
||||||
|
|
||||||
function getBackgroundService<T>(service: string) {
|
function getBackgroundService<T>(service: string) {
|
||||||
return (): T => {
|
return (): T => {
|
||||||
const page = chrome.extension.getBackgroundPage();
|
const page = BrowserApi.getBackgroundPage();
|
||||||
return page ? page.bitwardenMain[service] as T : null;
|
return page ? page.bitwardenMain[service] as T : null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,26 +9,38 @@ export default class BrowserStorageService implements StorageService {
|
||||||
|
|
||||||
get<T>(key: string): Promise<T> {
|
get<T>(key: string): Promise<T> {
|
||||||
if (this.platformUtilsService.isSafari()) {
|
if (this.platformUtilsService.isSafari()) {
|
||||||
// use safari storage
|
return new Promise((resolve) => {
|
||||||
|
const json = window.localStorage.getItem(key);
|
||||||
|
if (json) {
|
||||||
|
const obj = JSON.parse(json);
|
||||||
|
if (obj && (typeof obj[key] !== 'undefined') && obj[key] !== null) {
|
||||||
|
resolve(obj[key] as T);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resolve(null);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
chrome.storage.local.get(key, (obj: any) => {
|
chrome.storage.local.get(key, (obj: any) => {
|
||||||
if (obj && (typeof obj[key] !== 'undefined') && obj[key] !== null) {
|
if (obj && (typeof obj[key] !== 'undefined') && obj[key] !== null) {
|
||||||
resolve(obj[key] as T);
|
resolve(obj[key] as T);
|
||||||
} else {
|
return;
|
||||||
resolve(null);
|
|
||||||
}
|
}
|
||||||
|
resolve(null);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
save(key: string, obj: any): Promise<any> {
|
save(key: string, obj: any): Promise<any> {
|
||||||
|
const keyedObj = { [key]: obj };
|
||||||
if (this.platformUtilsService.isSafari()) {
|
if (this.platformUtilsService.isSafari()) {
|
||||||
// use safari storage
|
window.localStorage.setItem(key, JSON.stringify(keyedObj));
|
||||||
|
return Promise.resolve();
|
||||||
} else {
|
} else {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
chrome.storage.local.set({ [key]: obj }, () => {
|
chrome.storage.local.set(keyedObj, () => {
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -37,7 +49,8 @@ export default class BrowserStorageService implements StorageService {
|
||||||
|
|
||||||
remove(key: string): Promise<any> {
|
remove(key: string): Promise<any> {
|
||||||
if (this.platformUtilsService.isSafari()) {
|
if (this.platformUtilsService.isSafari()) {
|
||||||
// use safari storage
|
window.localStorage.removeItem(key);
|
||||||
|
return Promise.resolve();
|
||||||
} else {
|
} else {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
chrome.storage.local.remove(key, () => {
|
chrome.storage.local.remove(key, () => {
|
||||||
|
|
Loading…
Reference in New Issue