diff --git a/src/browser/browserApi.ts b/src/browser/browserApi.ts index d20a078656..616e724d4f 100644 --- a/src/browser/browserApi.ts +++ b/src/browser/browserApi.ts @@ -1,4 +1,7 @@ export default class BrowserApi { + static isSafariApi: boolean = (typeof safari !== 'undefined'); + static isChromeApi: boolean = (typeof chrome !== 'undefined'); + static async getTabFromCurrentWindowId(): Promise { 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; + } + } + } } diff --git a/src/globals.d.ts b/src/globals.d.ts index 0e9fe7a449..87abc1ae24 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -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; diff --git a/src/popup/app/services/background.service.ts b/src/popup/app/services/background.service.ts index 5d42d7930f..48aaae7df3 100644 --- a/src/popup/app/services/background.service.ts +++ b/src/popup/app/services/background.service.ts @@ -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(service: string) { return (): T => { - const page = chrome.extension.getBackgroundPage(); + const page = BrowserApi.getBackgroundPage(); return page ? page.bitwardenMain[service] as T : null; }; } diff --git a/src/services/browserStorage.service.ts b/src/services/browserStorage.service.ts index b79724ca64..4d8c345c5d 100644 --- a/src/services/browserStorage.service.ts +++ b/src/services/browserStorage.service.ts @@ -9,26 +9,38 @@ export default class BrowserStorageService implements StorageService { get(key: string): Promise { 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 { + 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 { if (this.platformUtilsService.isSafari()) { - // use safari storage + window.localStorage.removeItem(key); + return Promise.resolve(); } else { return new Promise((resolve) => { chrome.storage.local.remove(key, () => {