nativescript-awesome-webview-with-custom-menu-items
Version:
Awesome WebViews for all NativeScript Apps!
59 lines • 2.47 kB
JavaScript
import { Application, Color } from '@nativescript/core';
var CustomTabsClient = androidx.browser.customtabs.CustomTabsClient;
var CustomTabsServiceConnection = androidx.browser.customtabs.CustomTabsServiceConnection;
var CustomTabsIntent = androidx.browser.customtabs.CustomTabsIntent;
const CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome";
let mCustomTabsClient = null;
let mCustomTabsServiceConnection = null;
let mCustomTabsSession = null;
const context = () => Application.android.startActivity || Application.android.context;
export function init() {
mCustomTabsServiceConnection = new (CustomTabsServiceConnection.extend({
onCustomTabsServiceConnected: function (name, client) {
mCustomTabsClient = client;
mCustomTabsClient.warmup(long(0));
mCustomTabsSession = mCustomTabsClient.newSession(null);
},
onServiceDisconnected(name) {
mCustomTabsClient = null;
}
}))();
CustomTabsClient.bindCustomTabsService(context(), CUSTOM_TAB_PACKAGE_NAME, mCustomTabsServiceConnection);
}
export function openWebView(options) {
if (!options.url) {
throw new Error('No url set in the Advanced WebView Options object.');
}
let intentBuilder;
if (mCustomTabsSession) {
intentBuilder = new CustomTabsIntent.Builder();
}
else {
intentBuilder = new CustomTabsIntent.Builder(mCustomTabsSession);
}
intentBuilder.setShowTitle(options.showTitle === true);
if (options.toolbarColor) {
intentBuilder.setToolbarColor(new Color(options.toolbarColor).android);
}
if (options.menuItems) {
options.menuItems.forEach((item) => {
intentBuilder.addMenuItem(item.title, item.pendingIntent);
});
}
intentBuilder.enableUrlBarHiding();
const customTabsIntent = intentBuilder.build();
if (options.headers) {
const headers = new android.os.Bundle();
Object.keys(options.headers).forEach(headerName => {
headers.putString(headerName, options.headers[headerName]);
});
customTabsIntent.intent.putExtra(android.provider.Browser.EXTRA_HEADERS, headers);
}
customTabsIntent.intent.setData(android.net.Uri.parse(options.url));
const activityOrContext = context();
if (!activityOrContext) {
return;
}
activityOrContext.startActivityForResult(customTabsIntent.intent, 1010);
}
//# sourceMappingURL=awesome-webview.android.js.map