1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-20 10:13:31 +00:00

WIP Safari web extension

This commit is contained in:
Hinton
2020-12-07 20:00:49 +01:00
parent 6760cec1ec
commit 27ca7cc739
28 changed files with 607 additions and 1672 deletions

View File

@@ -1,16 +1,8 @@
import { BrowserApi } from '../browser/browserApi';
import { SafariApp } from '../browser/safariApp';
import { MessagingService } from 'jslib/abstractions/messaging.service';
export default class BrowserMessagingService implements MessagingService {
send(subscriber: string, arg: any = {}) {
const message = Object.assign({}, { command: subscriber }, arg);
if (BrowserApi.isSafariApi) {
SafariApp.sendMessageToApp(subscriber, arg);
SafariApp.sendMessageToListeners(message, 'BrowserMessagingService', null);
} else {
chrome.runtime.sendMessage(message);
}
chrome.runtime.sendMessage(message);
}
}

View File

@@ -189,13 +189,7 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
}
const clearing = options ? !!options.clearing : false;
const clearMs: number = options && options.clearMs ? options.clearMs : null;
if (this.isSafariExtension()) {
SafariApp.sendMessageToApp('copyToClipboard', text).then(() => {
if (!clearing && this.clipboardWriteCallback != null) {
this.clipboardWriteCallback(text, clearMs);
}
});
} else if (this.isFirefox() && (win as any).navigator.clipboard && (win as any).navigator.clipboard.writeText) {
if (this.isFirefox() && (win as any).navigator.clipboard && (win as any).navigator.clipboard.writeText) {
(win as any).navigator.clipboard.writeText(text).then(() => {
if (!clearing && this.clipboardWriteCallback != null) {
this.clipboardWriteCallback(text, clearMs);
@@ -311,6 +305,6 @@ export default class BrowserPlatformUtilsService implements PlatformUtilsService
}
private isSafariExtension(): boolean {
return (window as any).safariAppExtension === true;
return navigator.userAgent.indexOf(' Safari/') !== -1;
}
}

View File

@@ -3,61 +3,39 @@ import {
StorageService,
} from 'jslib/abstractions';
import { SafariApp } from '../browser/safariApp';
export default class BrowserStorageService implements StorageService {
private chromeStorageApi: any;
private isSafari: boolean;
constructor(platformUtilsService: PlatformUtilsService) {
this.isSafari = platformUtilsService.isSafari();
if (!this.isSafari) {
this.chromeStorageApi = chrome.storage.local;
}
constructor() {
this.chromeStorageApi = chrome.storage.local;
}
async get<T>(key: string): Promise<T> {
if (this.isSafari) {
const obj = await SafariApp.sendMessageToApp('storage_get', key);
return obj == null ? null : JSON.parse(obj) as T;
} else {
return new Promise((resolve) => {
this.chromeStorageApi.get(key, (obj: any) => {
if (obj != null && obj[key] != null) {
resolve(obj[key] as T);
return;
}
resolve(null);
});
return new Promise((resolve) => {
this.chromeStorageApi.get(key, (obj: any) => {
if (obj != null && obj[key] != null) {
resolve(obj[key] as T);
return;
}
resolve(null);
});
}
});
}
async save(key: string, obj: any): Promise<any> {
const keyedObj = { [key]: obj };
if (this.isSafari) {
await SafariApp.sendMessageToApp('storage_save', JSON.stringify({
key: key,
obj: JSON.stringify(obj),
}));
} else {
return new Promise((resolve) => {
this.chromeStorageApi.set(keyedObj, () => {
resolve();
});
return new Promise((resolve) => {
this.chromeStorageApi.set(keyedObj, () => {
resolve();
});
}
});
}
async remove(key: string): Promise<any> {
if (this.isSafari) {
await SafariApp.sendMessageToApp('storage_remove', key);
} else {
return new Promise((resolve) => {
this.chromeStorageApi.remove(key, () => {
resolve();
});
return new Promise((resolve) => {
this.chromeStorageApi.remove(key, () => {
resolve();
});
}
});
}
}

View File

@@ -1,19 +1,11 @@
import { I18nService as BaseI18nService } from 'jslib/services/i18n.service';
import { BrowserApi } from '../browser/browserApi';
import { SafariApp } from '../browser/safariApp';
export default class I18nService extends BaseI18nService {
constructor(systemLanguage: string) {
super(systemLanguage, BrowserApi.isSafariApi ? 'safari' : null, async (formattedLocale: string) => {
if (BrowserApi.isSafariApi) {
await SafariApp.sendMessageToApp('getLocaleStrings', formattedLocale);
return (window as any).bitwardenLocaleStrings;
} else {
// Deprecated
const file = await fetch(this.localesDirectory + formattedLocale + '/messages.json');
return await file.json();
}
super(systemLanguage, null, async (formattedLocale: string) => {
// Deprecated
const file = await fetch(this.localesDirectory + formattedLocale + '/messages.json');
return await file.json();
});
this.supportedTranslationLocales = [