start converting some safari browser apis

This commit is contained in:
Kyle Spearrin 2019-08-05 10:03:25 -04:00
parent 67c5c03bdb
commit 703474bd93
5 changed files with 43 additions and 49 deletions

2
jslib

@ -1 +1 @@
Subproject commit ee91cfc2dfda974ab1ae8e0077e58998bbc6e832 Subproject commit 393f6c9c20fb7eded5008b65242ea44e69bc349c

View File

@ -1,7 +1,6 @@
export class BrowserApi { export class BrowserApi {
static isWebExtensionsApi: boolean = (typeof browser !== 'undefined'); static isWebExtensionsApi: boolean = (typeof browser !== 'undefined');
static isSafariApi: boolean = (typeof safari !== 'undefined') && static isSafariApi: boolean = (window as any).safariAppExtension === true;
navigator.userAgent.indexOf(' Safari/') !== -1 && navigator.userAgent.indexOf('Chrome') === -1;
static isChromeApi: boolean = !BrowserApi.isSafariApi && (typeof chrome !== 'undefined'); static isChromeApi: boolean = !BrowserApi.isSafariApi && (typeof chrome !== 'undefined');
static isFirefoxOnAndroid: boolean = navigator.userAgent.indexOf('Firefox/') !== -1 && static isFirefoxOnAndroid: boolean = navigator.userAgent.indexOf('Firefox/') !== -1 &&
navigator.userAgent.indexOf('Android') !== -1; navigator.userAgent.indexOf('Android') !== -1;
@ -40,6 +39,8 @@ export class BrowserApi {
}); });
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
let wins: any[] = []; let wins: any[] = [];
// TODO
/*
if (options.currentWindow) { if (options.currentWindow) {
if (safari.application.activeBrowserWindow) { if (safari.application.activeBrowserWindow) {
wins.push(safari.application.activeBrowserWindow); wins.push(safari.application.activeBrowserWindow);
@ -64,6 +65,8 @@ export class BrowserApi {
}); });
return Promise.resolve(returnedTabs); return Promise.resolve(returnedTabs);
*/
return Promise.resolve([]);
} }
} }
@ -103,6 +106,8 @@ export class BrowserApi {
}); });
}); });
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
// TODO
/*
let t = tab.safariTab; let t = tab.safariTab;
if (!t || !t.page) { if (!t || !t.page) {
const win = safari.application.activeBrowserWindow; const win = safari.application.activeBrowserWindow;
@ -128,6 +133,7 @@ export class BrowserApi {
if (t.page) { if (t.page) {
t.page.dispatchMessage('bitwarden', obj); t.page.dispatchMessage('bitwarden', obj);
} }
*/
return Promise.resolve(); return Promise.resolve();
} }
@ -137,7 +143,7 @@ export class BrowserApi {
if (BrowserApi.isChromeApi) { if (BrowserApi.isChromeApi) {
return chrome.extension.getBackgroundPage(); return chrome.extension.getBackgroundPage();
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
return safari.extension.globalPage.contentWindow; return window;
} else { } else {
return null; return null;
} }
@ -147,7 +153,8 @@ export class BrowserApi {
if (BrowserApi.isChromeApi) { if (BrowserApi.isChromeApi) {
return chrome.runtime.getManifest().version; return chrome.runtime.getManifest().version;
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
return safari.extension.displayVersion; // TODO
return null;
} else { } else {
return null; return null;
} }
@ -157,8 +164,8 @@ export class BrowserApi {
if (BrowserApi.isChromeApi) { if (BrowserApi.isChromeApi) {
return chrome.extension.getViews({ type: 'popup' }).length > 0; return chrome.extension.getViews({ type: 'popup' }).length > 0;
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
return safari.extension.popovers && safari.extension.popovers.length && // TODO
safari.extension.popovers[0].visible; return true;
} else { } else {
return null; return null;
} }
@ -169,14 +176,8 @@ export class BrowserApi {
chrome.tabs.create({ url: url }); chrome.tabs.create({ url: url });
return null; return null;
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
if (extensionPage && url.indexOf('/') === 0) { // TODO
url = BrowserApi.getAssetUrl(url); return;
}
const tab = safari.application.activeBrowserWindow.openTab();
if (tab) {
tab.url = url;
}
return tab;
} else { } else {
return; return;
} }
@ -186,10 +187,8 @@ export class BrowserApi {
if (BrowserApi.isChromeApi) { if (BrowserApi.isChromeApi) {
return chrome.extension.getURL(path); return chrome.extension.getURL(path);
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
if (path.indexOf('/') === 0) { // TODO
path = path.substr(1); return null;
}
return safari.extension.baseURI + path;
} else { } else {
return null; return null;
} }
@ -201,13 +200,16 @@ export class BrowserApi {
callback(msg, sender, response); callback(msg, sender, response);
}); });
} else if (BrowserApi.isSafariApi) { } else if (BrowserApi.isSafariApi) {
// TODO
/*
safari.application.addEventListener('message', async (msgEvent: any) => { safari.application.addEventListener('message', async (msgEvent: any) => {
callback(msgEvent.message, { callback(msgEvent.message, {
tab: BrowserApi.makeTabObject(msgEvent.target), tab: BrowserApi.makeTabObject(msgEvent.target),
frameId: msgEvent.message != null && msgEvent.message.bitwardenFrameId != null ? frameId: msgEvent.message != null && msgEvent.message.bitwardenFrameId != null ?
msgEvent.message.bitwardenFrameId : null, msgEvent.message.bitwardenFrameId : null,
}, () => { /* No responses in Safari */ }); }, () => { });
}, false); }, false);
*/
} }
} }
@ -220,24 +222,13 @@ export class BrowserApi {
} else if (BrowserApi.isWebExtensionsApi || BrowserApi.isChromeApi) { } else if (BrowserApi.isWebExtensionsApi || BrowserApi.isChromeApi) {
win.close(); win.close();
} else if (BrowserApi.isSafariApi && safari.extension.popovers && safari.extension.popovers.length > 0) { } else if (BrowserApi.isSafariApi && safari.extension.popovers && safari.extension.popovers.length > 0) {
safari.extension.popovers[0].hide(); BrowserApi.sendSafariMessageToApp({ command: 'hideWindow' });
} }
} }
static downloadFile(win: Window, blobData: any, blobOptions: any, fileName: string) { static downloadFile(win: Window, blobData: any, blobOptions: any, fileName: string) {
if (BrowserApi.isSafariApi) { if (BrowserApi.isSafariApi) {
const tab = BrowserApi.createNewTab(BrowserApi.getAssetUrl('downloader/index.html')); // TODO
const tabToSend = BrowserApi.makeTabObject(tab);
setTimeout(async () => {
BrowserApi.tabSendMessage(tabToSend, {
command: 'downloaderPageData',
data: {
blobData: blobData,
blobOptions: blobOptions,
fileName: fileName,
},
});
}, 1000);
} else { } else {
const blob = new Blob([blobData], blobOptions); const blob = new Blob([blobData], blobOptions);
if (navigator.msSaveOrOpenBlob) { if (navigator.msSaveOrOpenBlob) {
@ -262,6 +253,9 @@ export class BrowserApi {
return {}; return {};
} }
// TODO
return {};
/*
const winIndex = safari.application.browserWindows.indexOf(tab.browserWindow); const winIndex = safari.application.browserWindows.indexOf(tab.browserWindow);
const tabIndex = tab.browserWindow.tabs.indexOf(tab); const tabIndex = tab.browserWindow.tabs.indexOf(tab);
return { return {
@ -273,11 +267,11 @@ export class BrowserApi {
url: tab.url || 'about:blank', url: tab.url || 'about:blank',
safariTab: tab, safariTab: tab,
}; };
*/
} }
static gaFilter() { static gaFilter() {
return process.env.ENV !== 'production' || return process.env.ENV !== 'production';
(BrowserApi.isSafariApi && safari.application.activeBrowserWindow.activeTab.private);
} }
static getUILanguage(win: Window) { static getUILanguage(win: Window) {
@ -291,8 +285,16 @@ export class BrowserApi {
static reloadExtension(win: Window) { static reloadExtension(win: Window) {
if (win != null) { if (win != null) {
return win.location.reload(true); return win.location.reload(true);
} else if (BrowserApi.isSafariApi) {
BrowserApi.sendSafariMessageToApp({ command: 'reloadWindow' });
} else if (!BrowserApi.isSafariApi) { } else if (!BrowserApi.isSafariApi) {
return chrome.runtime.reload(); return chrome.runtime.reload();
} }
} }
static sendSafariMessageToApp(message: any, response: (data: any) => {} = null) {
if (this.isSafariApi) {
(window as any).webkit.messageHandlers.bitwardenApp.postMessage(message);
}
}
} }

View File

@ -26,7 +26,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
let bundleURL = Bundle.main.resourceURL!.absoluteURL let bundleURL = Bundle.main.resourceURL!.absoluteURL
let html = bundleURL.appendingPathComponent("app/popup/index.html") let html = bundleURL.appendingPathComponent("app/popup/index.html")
webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled") webViewConfig.preferences.setValue(true, forKey: "developerExtrasEnabled")
webViewConfig.userContentController.add(self, name: "webViewOnLoad") webViewConfig.userContentController.add(self, name: "bitwardenMessage")
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: parentWidth, height: parentHeight), configuration: webViewConfig) webView = WKWebView(frame: CGRect(x: 0, y: 0, width: parentWidth, height: parentHeight), configuration: webViewConfig)
webView.navigationDelegate = self webView.navigationDelegate = self
webView.allowsLinkPreview = false webView.allowsLinkPreview = false
@ -50,7 +50,7 @@ class SafariExtensionViewController: SFSafariExtensionViewController, WKScriptMe
} }
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == "webViewOnLoad" { if message.name == "bitwardenMessage" {
} }
} }

View File

@ -11,6 +11,9 @@
<app-root> <app-root>
<div id="loading"><i class="fa fa-spinner fa-spin fa-3x"></i></div> <div id="loading"><i class="fa fa-spinner fa-spin fa-3x"></i></div>
</app-root> </app-root>
<script type="text/javascript">
window.safariAppExtension = true;
</script>
<script type="text/javascript" src="../vendor.js"></script> <script type="text/javascript" src="../vendor.js"></script>
<script type="text/javascript" src="../background.js"></script> <script type="text/javascript" src="../background.js"></script>
<script type="text/javascript" src="vendor.js"></script> <script type="text/javascript" src="vendor.js"></script>

View File

@ -7,18 +7,7 @@ export default class BrowserMessagingService implements MessagingService {
const message = Object.assign({}, { command: subscriber }, arg); const message = Object.assign({}, { command: subscriber }, arg);
if (BrowserApi.isSafariApi) { if (BrowserApi.isSafariApi) {
const bgPage = BrowserApi.getBackgroundPage(); BrowserApi.sendSafariMessageToApp(message);
bgPage.bitwardenMain.sendInternalRuntimeMessage(message);
if (!safari.extension.popovers || !safari.extension.popovers.length) {
return;
}
const popover = safari.extension.popovers[0];
const popoverPage = popover.contentWindow;
if (popover.visible && popoverPage.bitwardenPopupMainMessageListener) {
popoverPage.bitwardenPopupMainMessageListener(message);
}
} else { } else {
chrome.runtime.sendMessage(message); chrome.runtime.sendMessage(message);
} }