more safari APIs

This commit is contained in:
Kyle Spearrin 2018-01-11 23:04:47 -05:00
parent 6b507c9071
commit 2b6c2bd4e2
4 changed files with 38 additions and 7 deletions

View File

@ -1,4 +1,7 @@
export default class BrowserApi {
static isSafariApi: boolean = (typeof safari !== 'undefined');
static isChromeApi: boolean = (typeof chrome !== 'undefined');
static async getTabFromCurrentWindowId(): Promise<any> {
return await BrowserApi.tabsQueryFirst({
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;
}
}
}
}

1
src/globals.d.ts vendored
View File

@ -3,3 +3,4 @@ declare function escape(s: string): string;
declare function unescape(s: string): string;
declare var opr: any;
declare var chrome: any;
declare var safari: any;

View File

@ -1,3 +1,5 @@
import BrowserApi from '../../../browser/browserApi';
import { ConstantsService } from 'jslib/services/constants.service';
import { ApiService } from 'jslib/abstractions/api.service';
@ -22,7 +24,7 @@ import { AutofillService } from '../../../services/abstractions/autofill.service
function getBackgroundService<T>(service: string) {
return (): T => {
const page = chrome.extension.getBackgroundPage();
const page = BrowserApi.getBackgroundPage();
return page ? page.bitwardenMain[service] as T : null;
};
}

View File

@ -9,26 +9,38 @@ export default class BrowserStorageService implements StorageService {
get<T>(key: string): Promise<T> {
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 {
return new Promise((resolve) => {
chrome.storage.local.get(key, (obj: any) => {
if (obj && (typeof obj[key] !== 'undefined') && obj[key] !== null) {
resolve(obj[key] as T);
} else {
resolve(null);
return;
}
resolve(null);
});
});
}
}
save(key: string, obj: any): Promise<any> {
const keyedObj = { [key]: obj };
if (this.platformUtilsService.isSafari()) {
// use safari storage
window.localStorage.setItem(key, JSON.stringify(keyedObj));
return Promise.resolve();
} else {
return new Promise((resolve) => {
chrome.storage.local.set({ [key]: obj }, () => {
chrome.storage.local.set(keyedObj, () => {
resolve();
});
});
@ -37,7 +49,8 @@ export default class BrowserStorageService implements StorageService {
remove(key: string): Promise<any> {
if (this.platformUtilsService.isSafari()) {
// use safari storage
window.localStorage.removeItem(key);
return Promise.resolve();
} else {
return new Promise((resolve) => {
chrome.storage.local.remove(key, () => {