@capacitor-plus/android 8.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/capacitor/build.gradle +94 -0
- package/capacitor/lint-baseline.xml +136 -0
- package/capacitor/lint.xml +9 -0
- package/capacitor/proguard-rules.pro +28 -0
- package/capacitor/src/main/AndroidManifest.xml +3 -0
- package/capacitor/src/main/assets/native-bridge.js +1029 -0
- package/capacitor/src/main/java/com/getcapacitor/AndroidProtocolHandler.java +94 -0
- package/capacitor/src/main/java/com/getcapacitor/App.java +61 -0
- package/capacitor/src/main/java/com/getcapacitor/AppUUID.java +65 -0
- package/capacitor/src/main/java/com/getcapacitor/Bridge.java +1627 -0
- package/capacitor/src/main/java/com/getcapacitor/BridgeActivity.java +212 -0
- package/capacitor/src/main/java/com/getcapacitor/BridgeWebChromeClient.java +467 -0
- package/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java +105 -0
- package/capacitor/src/main/java/com/getcapacitor/CapConfig.java +709 -0
- package/capacitor/src/main/java/com/getcapacitor/CapacitorWebView.java +57 -0
- package/capacitor/src/main/java/com/getcapacitor/FileUtils.java +303 -0
- package/capacitor/src/main/java/com/getcapacitor/InvalidPluginException.java +8 -0
- package/capacitor/src/main/java/com/getcapacitor/InvalidPluginMethodException.java +16 -0
- package/capacitor/src/main/java/com/getcapacitor/JSArray.java +51 -0
- package/capacitor/src/main/java/com/getcapacitor/JSExport.java +208 -0
- package/capacitor/src/main/java/com/getcapacitor/JSExportException.java +16 -0
- package/capacitor/src/main/java/com/getcapacitor/JSInjector.java +127 -0
- package/capacitor/src/main/java/com/getcapacitor/JSObject.java +164 -0
- package/capacitor/src/main/java/com/getcapacitor/JSValue.java +65 -0
- package/capacitor/src/main/java/com/getcapacitor/Logger.java +103 -0
- package/capacitor/src/main/java/com/getcapacitor/MessageHandler.java +157 -0
- package/capacitor/src/main/java/com/getcapacitor/NativePlugin.java +37 -0
- package/capacitor/src/main/java/com/getcapacitor/PermissionState.java +31 -0
- package/capacitor/src/main/java/com/getcapacitor/Plugin.java +1050 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginCall.java +394 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginConfig.java +116 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginHandle.java +160 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginInvocationException.java +16 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginLoadException.java +19 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginManager.java +56 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginMethod.java +15 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginMethodHandle.java +33 -0
- package/capacitor/src/main/java/com/getcapacitor/PluginResult.java +84 -0
- package/capacitor/src/main/java/com/getcapacitor/ProcessedRoute.java +37 -0
- package/capacitor/src/main/java/com/getcapacitor/RouteProcessor.java +8 -0
- package/capacitor/src/main/java/com/getcapacitor/ServerPath.java +25 -0
- package/capacitor/src/main/java/com/getcapacitor/UriMatcher.java +184 -0
- package/capacitor/src/main/java/com/getcapacitor/WebViewListener.java +57 -0
- package/capacitor/src/main/java/com/getcapacitor/WebViewLocalServer.java +758 -0
- package/capacitor/src/main/java/com/getcapacitor/annotation/ActivityCallback.java +10 -0
- package/capacitor/src/main/java/com/getcapacitor/annotation/CapacitorPlugin.java +35 -0
- package/capacitor/src/main/java/com/getcapacitor/annotation/Permission.java +22 -0
- package/capacitor/src/main/java/com/getcapacitor/annotation/PermissionCallback.java +10 -0
- package/capacitor/src/main/java/com/getcapacitor/cordova/CapacitorCordovaCookieManager.java +42 -0
- package/capacitor/src/main/java/com/getcapacitor/cordova/MockCordovaInterfaceImpl.java +39 -0
- package/capacitor/src/main/java/com/getcapacitor/cordova/MockCordovaWebViewImpl.java +282 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookieManager.java +239 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorCookies.java +119 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/CapacitorHttp.java +119 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/SystemBars.java +220 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/WebView.java +48 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/AssetUtil.java +358 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/CapacitorHttpUrlConnection.java +473 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/HttpRequestHandler.java +465 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/ICapacitorHttpUrlConnection.java +15 -0
- package/capacitor/src/main/java/com/getcapacitor/plugin/util/MimeType.java +17 -0
- package/capacitor/src/main/java/com/getcapacitor/util/HostMask.java +123 -0
- package/capacitor/src/main/java/com/getcapacitor/util/InternalUtils.java +27 -0
- package/capacitor/src/main/java/com/getcapacitor/util/JSONUtils.java +166 -0
- package/capacitor/src/main/java/com/getcapacitor/util/PermissionHelper.java +114 -0
- package/capacitor/src/main/java/com/getcapacitor/util/WebColor.java +28 -0
- package/capacitor/src/main/res/layout/capacitor_bridge_layout_main.xml +15 -0
- package/capacitor/src/main/res/layout/no_webview.xml +14 -0
- package/capacitor/src/main/res/values/colors.xml +6 -0
- package/capacitor/src/main/res/values/strings.xml +3 -0
- package/capacitor/src/main/res/values/styles.xml +6 -0
- package/package.json +31 -0
|
@@ -0,0 +1,1029 @@
|
|
|
1
|
+
|
|
2
|
+
/*! Capacitor: https://capacitorjs.com/ - MIT License */
|
|
3
|
+
/* Generated File. Do not edit. */
|
|
4
|
+
|
|
5
|
+
var nativeBridge = (function (exports) {
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
var ExceptionCode;
|
|
9
|
+
(function (ExceptionCode) {
|
|
10
|
+
/**
|
|
11
|
+
* API is not implemented.
|
|
12
|
+
*
|
|
13
|
+
* This usually means the API can't be used because it is not implemented for
|
|
14
|
+
* the current platform.
|
|
15
|
+
*/
|
|
16
|
+
ExceptionCode["Unimplemented"] = "UNIMPLEMENTED";
|
|
17
|
+
/**
|
|
18
|
+
* API is not available.
|
|
19
|
+
*
|
|
20
|
+
* This means the API can't be used right now because:
|
|
21
|
+
* - it is currently missing a prerequisite, such as network connectivity
|
|
22
|
+
* - it requires a particular platform or browser version
|
|
23
|
+
*/
|
|
24
|
+
ExceptionCode["Unavailable"] = "UNAVAILABLE";
|
|
25
|
+
})(ExceptionCode || (ExceptionCode = {}));
|
|
26
|
+
class CapacitorException extends Error {
|
|
27
|
+
constructor(message, code, data) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.message = message;
|
|
30
|
+
this.code = code;
|
|
31
|
+
this.data = data;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// For removing exports for iOS/Android, keep let for reassignment
|
|
36
|
+
// eslint-disable-next-line
|
|
37
|
+
let dummy = {};
|
|
38
|
+
const readFileAsBase64 = (file) => new Promise((resolve, reject) => {
|
|
39
|
+
const reader = new FileReader();
|
|
40
|
+
reader.onloadend = () => {
|
|
41
|
+
const data = reader.result;
|
|
42
|
+
resolve(btoa(data));
|
|
43
|
+
};
|
|
44
|
+
reader.onerror = reject;
|
|
45
|
+
reader.readAsBinaryString(file);
|
|
46
|
+
});
|
|
47
|
+
const convertFormData = async (formData) => {
|
|
48
|
+
const newFormData = [];
|
|
49
|
+
for (const pair of formData.entries()) {
|
|
50
|
+
const [key, value] = pair;
|
|
51
|
+
if (value instanceof File) {
|
|
52
|
+
const base64File = await readFileAsBase64(value);
|
|
53
|
+
newFormData.push({
|
|
54
|
+
key,
|
|
55
|
+
value: base64File,
|
|
56
|
+
type: 'base64File',
|
|
57
|
+
contentType: value.type,
|
|
58
|
+
fileName: value.name,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
newFormData.push({ key, value, type: 'string' });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return newFormData;
|
|
66
|
+
};
|
|
67
|
+
const convertBody = async (body, contentType) => {
|
|
68
|
+
if (body instanceof ReadableStream || body instanceof Uint8Array) {
|
|
69
|
+
let encodedData;
|
|
70
|
+
if (body instanceof ReadableStream) {
|
|
71
|
+
const reader = body.getReader();
|
|
72
|
+
const chunks = [];
|
|
73
|
+
while (true) {
|
|
74
|
+
const { done, value } = await reader.read();
|
|
75
|
+
if (done)
|
|
76
|
+
break;
|
|
77
|
+
chunks.push(value);
|
|
78
|
+
}
|
|
79
|
+
const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0));
|
|
80
|
+
let position = 0;
|
|
81
|
+
for (const chunk of chunks) {
|
|
82
|
+
concatenated.set(chunk, position);
|
|
83
|
+
position += chunk.length;
|
|
84
|
+
}
|
|
85
|
+
encodedData = concatenated;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
encodedData = body;
|
|
89
|
+
}
|
|
90
|
+
let data = new TextDecoder().decode(encodedData);
|
|
91
|
+
let type;
|
|
92
|
+
if (contentType === 'application/json') {
|
|
93
|
+
try {
|
|
94
|
+
data = JSON.parse(data);
|
|
95
|
+
}
|
|
96
|
+
catch (ignored) {
|
|
97
|
+
// ignore
|
|
98
|
+
}
|
|
99
|
+
type = 'json';
|
|
100
|
+
}
|
|
101
|
+
else if (contentType === 'multipart/form-data') {
|
|
102
|
+
type = 'formData';
|
|
103
|
+
}
|
|
104
|
+
else if (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith('image')) {
|
|
105
|
+
type = 'image';
|
|
106
|
+
}
|
|
107
|
+
else if (contentType === 'application/octet-stream') {
|
|
108
|
+
type = 'binary';
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
type = 'text';
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
data,
|
|
115
|
+
type,
|
|
116
|
+
headers: { 'Content-Type': contentType || 'application/octet-stream' },
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
else if (body instanceof URLSearchParams) {
|
|
120
|
+
return {
|
|
121
|
+
data: body.toString(),
|
|
122
|
+
type: 'text',
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
else if (body instanceof FormData) {
|
|
126
|
+
return {
|
|
127
|
+
data: await convertFormData(body),
|
|
128
|
+
type: 'formData',
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
else if (body instanceof File) {
|
|
132
|
+
const fileData = await readFileAsBase64(body);
|
|
133
|
+
return {
|
|
134
|
+
data: fileData,
|
|
135
|
+
type: 'file',
|
|
136
|
+
headers: { 'Content-Type': body.type },
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return { data: body, type: 'json' };
|
|
140
|
+
};
|
|
141
|
+
const CAPACITOR_HTTP_INTERCEPTOR = '/_capacitor_http_interceptor_';
|
|
142
|
+
const CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM = 'u';
|
|
143
|
+
// TODO: export as Cap function
|
|
144
|
+
const isRelativeOrProxyUrl = (url) => !url || !(url.startsWith('http:') || url.startsWith('https:')) || url.indexOf(CAPACITOR_HTTP_INTERCEPTOR) > -1;
|
|
145
|
+
// TODO: export as Cap function
|
|
146
|
+
const createProxyUrl = (url, win) => {
|
|
147
|
+
var _a, _b;
|
|
148
|
+
if (isRelativeOrProxyUrl(url))
|
|
149
|
+
return url;
|
|
150
|
+
const bridgeUrl = new URL((_b = (_a = win.Capacitor) === null || _a === void 0 ? void 0 : _a.getServerUrl()) !== null && _b !== void 0 ? _b : '');
|
|
151
|
+
bridgeUrl.pathname = CAPACITOR_HTTP_INTERCEPTOR;
|
|
152
|
+
bridgeUrl.searchParams.append(CAPACITOR_HTTP_INTERCEPTOR_URL_PARAM, url);
|
|
153
|
+
return bridgeUrl.toString();
|
|
154
|
+
};
|
|
155
|
+
const initBridge = (w) => {
|
|
156
|
+
const getPlatformId = (win) => {
|
|
157
|
+
var _a, _b;
|
|
158
|
+
if (win === null || win === void 0 ? void 0 : win.androidBridge) {
|
|
159
|
+
return 'android';
|
|
160
|
+
}
|
|
161
|
+
else if ((_b = (_a = win === null || win === void 0 ? void 0 : win.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.bridge) {
|
|
162
|
+
return 'ios';
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
return 'web';
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
const convertFileSrcServerUrl = (webviewServerUrl, filePath) => {
|
|
169
|
+
if (typeof filePath === 'string') {
|
|
170
|
+
if (filePath.startsWith('/')) {
|
|
171
|
+
return webviewServerUrl + '/_capacitor_file_' + filePath;
|
|
172
|
+
}
|
|
173
|
+
else if (filePath.startsWith('file://')) {
|
|
174
|
+
return webviewServerUrl + filePath.replace('file://', '/_capacitor_file_');
|
|
175
|
+
}
|
|
176
|
+
else if (filePath.startsWith('content://')) {
|
|
177
|
+
return webviewServerUrl + filePath.replace('content:/', '/_capacitor_content_');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return filePath;
|
|
181
|
+
};
|
|
182
|
+
const initEvents = (win, cap) => {
|
|
183
|
+
cap.addListener = (pluginName, eventName, callback) => {
|
|
184
|
+
const callbackId = cap.nativeCallback(pluginName, 'addListener', {
|
|
185
|
+
eventName: eventName,
|
|
186
|
+
}, callback);
|
|
187
|
+
return {
|
|
188
|
+
remove: async () => {
|
|
189
|
+
var _a;
|
|
190
|
+
(_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.debug('Removing listener', pluginName, eventName);
|
|
191
|
+
cap.removeListener(pluginName, callbackId, eventName, callback);
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
};
|
|
195
|
+
cap.removeListener = (pluginName, callbackId, eventName, callback) => {
|
|
196
|
+
cap.nativeCallback(pluginName, 'removeListener', {
|
|
197
|
+
callbackId: callbackId,
|
|
198
|
+
eventName: eventName,
|
|
199
|
+
}, callback);
|
|
200
|
+
};
|
|
201
|
+
cap.createEvent = (eventName, eventData) => {
|
|
202
|
+
const doc = win.document;
|
|
203
|
+
if (doc) {
|
|
204
|
+
const ev = doc.createEvent('Events');
|
|
205
|
+
ev.initEvent(eventName, false, false);
|
|
206
|
+
if (eventData && typeof eventData === 'object') {
|
|
207
|
+
for (const i in eventData) {
|
|
208
|
+
// eslint-disable-next-line no-prototype-builtins
|
|
209
|
+
if (eventData.hasOwnProperty(i)) {
|
|
210
|
+
ev[i] = eventData[i];
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return ev;
|
|
215
|
+
}
|
|
216
|
+
return null;
|
|
217
|
+
};
|
|
218
|
+
cap.triggerEvent = (eventName, target, eventData) => {
|
|
219
|
+
const doc = win.document;
|
|
220
|
+
const cordova = win.cordova;
|
|
221
|
+
eventData = eventData || {};
|
|
222
|
+
const ev = cap.createEvent(eventName, eventData);
|
|
223
|
+
if (ev) {
|
|
224
|
+
if (target === 'document') {
|
|
225
|
+
if (cordova === null || cordova === void 0 ? void 0 : cordova.fireDocumentEvent) {
|
|
226
|
+
cordova.fireDocumentEvent(eventName, eventData);
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
else if (doc === null || doc === void 0 ? void 0 : doc.dispatchEvent) {
|
|
230
|
+
return doc.dispatchEvent(ev);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
else if (target === 'window' && win.dispatchEvent) {
|
|
234
|
+
return win.dispatchEvent(ev);
|
|
235
|
+
}
|
|
236
|
+
else if (doc === null || doc === void 0 ? void 0 : doc.querySelector) {
|
|
237
|
+
const targetEl = doc.querySelector(target);
|
|
238
|
+
if (targetEl) {
|
|
239
|
+
return targetEl.dispatchEvent(ev);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return false;
|
|
244
|
+
};
|
|
245
|
+
win.Capacitor = cap;
|
|
246
|
+
};
|
|
247
|
+
const initLegacyHandlers = (win, cap) => {
|
|
248
|
+
// define cordova if it's not there already
|
|
249
|
+
win.cordova = win.cordova || {};
|
|
250
|
+
const doc = win.document;
|
|
251
|
+
const nav = win.navigator;
|
|
252
|
+
if (nav) {
|
|
253
|
+
nav.app = nav.app || {};
|
|
254
|
+
nav.app.exitApp = () => {
|
|
255
|
+
var _a;
|
|
256
|
+
if (!((_a = cap.Plugins) === null || _a === void 0 ? void 0 : _a.App)) {
|
|
257
|
+
win.console.warn('App plugin not installed');
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
cap.nativeCallback('App', 'exitApp', {});
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
if (doc) {
|
|
265
|
+
const docAddEventListener = doc.addEventListener;
|
|
266
|
+
doc.addEventListener = (...args) => {
|
|
267
|
+
var _a;
|
|
268
|
+
const eventName = args[0];
|
|
269
|
+
const handler = args[1];
|
|
270
|
+
if (eventName === 'deviceready' && handler) {
|
|
271
|
+
Promise.resolve().then(handler);
|
|
272
|
+
}
|
|
273
|
+
else if (eventName === 'backbutton' && cap.Plugins.App) {
|
|
274
|
+
// Add a dummy listener so Capacitor doesn't do the default
|
|
275
|
+
// back button action
|
|
276
|
+
if (!((_a = cap.Plugins) === null || _a === void 0 ? void 0 : _a.App)) {
|
|
277
|
+
win.console.warn('App plugin not installed');
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
cap.Plugins.App.addListener('backButton', () => {
|
|
281
|
+
// ignore
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return docAddEventListener.apply(doc, args);
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
win.Capacitor = cap;
|
|
289
|
+
};
|
|
290
|
+
const initVendor = (win, cap) => {
|
|
291
|
+
const Ionic = (win.Ionic = win.Ionic || {});
|
|
292
|
+
const IonicWebView = (Ionic.WebView = Ionic.WebView || {});
|
|
293
|
+
const Plugins = cap.Plugins;
|
|
294
|
+
IonicWebView.getServerBasePath = (callback) => {
|
|
295
|
+
var _a;
|
|
296
|
+
(_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.getServerBasePath().then((result) => {
|
|
297
|
+
callback(result.path);
|
|
298
|
+
});
|
|
299
|
+
};
|
|
300
|
+
IonicWebView.setServerAssetPath = (path) => {
|
|
301
|
+
var _a;
|
|
302
|
+
(_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerAssetPath({ path });
|
|
303
|
+
};
|
|
304
|
+
IonicWebView.setServerBasePath = (path) => {
|
|
305
|
+
var _a;
|
|
306
|
+
(_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerBasePath({ path });
|
|
307
|
+
};
|
|
308
|
+
IonicWebView.persistServerBasePath = () => {
|
|
309
|
+
var _a;
|
|
310
|
+
(_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.persistServerBasePath();
|
|
311
|
+
};
|
|
312
|
+
IonicWebView.convertFileSrc = (url) => cap.convertFileSrc(url);
|
|
313
|
+
win.Capacitor = cap;
|
|
314
|
+
win.Ionic.WebView = IonicWebView;
|
|
315
|
+
};
|
|
316
|
+
const initLogger = (win, cap) => {
|
|
317
|
+
const BRIDGED_CONSOLE_METHODS = ['debug', 'error', 'info', 'log', 'trace', 'warn'];
|
|
318
|
+
const createLogFromNative = (c) => (result) => {
|
|
319
|
+
if (isFullConsole(c)) {
|
|
320
|
+
const success = result.success === true;
|
|
321
|
+
const tagStyles = success
|
|
322
|
+
? 'font-style: italic; font-weight: lighter; color: gray'
|
|
323
|
+
: 'font-style: italic; font-weight: lighter; color: red';
|
|
324
|
+
c.groupCollapsed('%cresult %c' + result.pluginId + '.' + result.methodName + ' (#' + result.callbackId + ')', tagStyles, 'font-style: italic; font-weight: bold; color: #444');
|
|
325
|
+
if (result.success === false) {
|
|
326
|
+
c.error(result.error);
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
c.dir(JSON.stringify(result.data));
|
|
330
|
+
}
|
|
331
|
+
c.groupEnd();
|
|
332
|
+
}
|
|
333
|
+
else {
|
|
334
|
+
if (result.success === false) {
|
|
335
|
+
c.error('LOG FROM NATIVE', result.error);
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
c.log('LOG FROM NATIVE', result.data);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
const createLogToNative = (c) => (call) => {
|
|
343
|
+
if (isFullConsole(c)) {
|
|
344
|
+
c.groupCollapsed('%cnative %c' + call.pluginId + '.' + call.methodName + ' (#' + call.callbackId + ')', 'font-weight: lighter; color: gray', 'font-weight: bold; color: #000');
|
|
345
|
+
c.dir(call);
|
|
346
|
+
c.groupEnd();
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
c.log('LOG TO NATIVE: ', call);
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
const isFullConsole = (c) => {
|
|
353
|
+
if (!c) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
return typeof c.groupCollapsed === 'function' || typeof c.groupEnd === 'function' || typeof c.dir === 'function';
|
|
357
|
+
};
|
|
358
|
+
const serializeConsoleMessage = (msg) => {
|
|
359
|
+
try {
|
|
360
|
+
if (typeof msg === 'object') {
|
|
361
|
+
msg = JSON.stringify(msg);
|
|
362
|
+
}
|
|
363
|
+
return String(msg);
|
|
364
|
+
}
|
|
365
|
+
catch (e) {
|
|
366
|
+
return '';
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
const platform = getPlatformId(win);
|
|
370
|
+
if (platform == 'android' || platform == 'ios') {
|
|
371
|
+
// patch document.cookie on Android/iOS
|
|
372
|
+
win.CapacitorCookiesDescriptor =
|
|
373
|
+
Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') ||
|
|
374
|
+
Object.getOwnPropertyDescriptor(HTMLDocument.prototype, 'cookie');
|
|
375
|
+
let doPatchCookies = false;
|
|
376
|
+
// check if capacitor cookies is disabled before patching
|
|
377
|
+
if (platform === 'ios') {
|
|
378
|
+
// Use prompt to synchronously get capacitor cookies config.
|
|
379
|
+
// https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323
|
|
380
|
+
const payload = {
|
|
381
|
+
type: 'CapacitorCookies.isEnabled',
|
|
382
|
+
};
|
|
383
|
+
const isCookiesEnabled = prompt(JSON.stringify(payload));
|
|
384
|
+
if (isCookiesEnabled === 'true') {
|
|
385
|
+
doPatchCookies = true;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
else if (typeof win.CapacitorCookiesAndroidInterface !== 'undefined') {
|
|
389
|
+
const isCookiesEnabled = win.CapacitorCookiesAndroidInterface.isEnabled();
|
|
390
|
+
if (isCookiesEnabled === true) {
|
|
391
|
+
doPatchCookies = true;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
if (doPatchCookies) {
|
|
395
|
+
Object.defineProperty(document, 'cookie', {
|
|
396
|
+
get: function () {
|
|
397
|
+
var _a, _b, _c;
|
|
398
|
+
if (platform === 'ios') {
|
|
399
|
+
// Use prompt to synchronously get cookies.
|
|
400
|
+
// https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323
|
|
401
|
+
const payload = {
|
|
402
|
+
type: 'CapacitorCookies.get',
|
|
403
|
+
};
|
|
404
|
+
const res = prompt(JSON.stringify(payload));
|
|
405
|
+
return res;
|
|
406
|
+
}
|
|
407
|
+
else if (typeof win.CapacitorCookiesAndroidInterface !== 'undefined') {
|
|
408
|
+
// return original document.cookie since Android does not support filtering of `httpOnly` cookies
|
|
409
|
+
return (_c = (_b = (_a = win.CapacitorCookiesDescriptor) === null || _a === void 0 ? void 0 : _a.get) === null || _b === void 0 ? void 0 : _b.call(document)) !== null && _c !== void 0 ? _c : '';
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
set: function (val) {
|
|
413
|
+
const cookiePairs = val.split(';');
|
|
414
|
+
const domainSection = val.toLowerCase().split('domain=')[1];
|
|
415
|
+
const domain = cookiePairs.length > 1 && domainSection != null && domainSection.length > 0
|
|
416
|
+
? domainSection.split(';')[0].trim()
|
|
417
|
+
: '';
|
|
418
|
+
if (platform === 'ios') {
|
|
419
|
+
// Use prompt to synchronously set cookies.
|
|
420
|
+
// https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323
|
|
421
|
+
const payload = {
|
|
422
|
+
type: 'CapacitorCookies.set',
|
|
423
|
+
action: val,
|
|
424
|
+
domain,
|
|
425
|
+
};
|
|
426
|
+
prompt(JSON.stringify(payload));
|
|
427
|
+
}
|
|
428
|
+
else if (typeof win.CapacitorCookiesAndroidInterface !== 'undefined') {
|
|
429
|
+
win.CapacitorCookiesAndroidInterface.setCookie(domain, val);
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
// patch fetch / XHR on Android/iOS
|
|
435
|
+
// store original fetch & XHR functions
|
|
436
|
+
win.CapacitorWebFetch = window.fetch;
|
|
437
|
+
win.CapacitorWebXMLHttpRequest = {
|
|
438
|
+
abort: window.XMLHttpRequest.prototype.abort,
|
|
439
|
+
constructor: window.XMLHttpRequest.prototype.constructor,
|
|
440
|
+
fullObject: window.XMLHttpRequest,
|
|
441
|
+
getAllResponseHeaders: window.XMLHttpRequest.prototype.getAllResponseHeaders,
|
|
442
|
+
getResponseHeader: window.XMLHttpRequest.prototype.getResponseHeader,
|
|
443
|
+
open: window.XMLHttpRequest.prototype.open,
|
|
444
|
+
prototype: window.XMLHttpRequest.prototype,
|
|
445
|
+
send: window.XMLHttpRequest.prototype.send,
|
|
446
|
+
setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader,
|
|
447
|
+
};
|
|
448
|
+
let doPatchHttp = false;
|
|
449
|
+
// check if capacitor http is disabled before patching
|
|
450
|
+
if (platform === 'ios') {
|
|
451
|
+
// Use prompt to synchronously get capacitor http config.
|
|
452
|
+
// https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323
|
|
453
|
+
const payload = {
|
|
454
|
+
type: 'CapacitorHttp',
|
|
455
|
+
};
|
|
456
|
+
const isHttpEnabled = prompt(JSON.stringify(payload));
|
|
457
|
+
if (isHttpEnabled === 'true') {
|
|
458
|
+
doPatchHttp = true;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
else if (typeof win.CapacitorHttpAndroidInterface !== 'undefined') {
|
|
462
|
+
const isHttpEnabled = win.CapacitorHttpAndroidInterface.isEnabled();
|
|
463
|
+
if (isHttpEnabled === true) {
|
|
464
|
+
doPatchHttp = true;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (doPatchHttp) {
|
|
468
|
+
// fetch patch
|
|
469
|
+
window.fetch = async (resource, options) => {
|
|
470
|
+
const headers = new Headers(options === null || options === void 0 ? void 0 : options.headers);
|
|
471
|
+
const contentType = headers.get('Content-Type') || headers.get('content-type');
|
|
472
|
+
if ((options === null || options === void 0 ? void 0 : options.body) instanceof FormData &&
|
|
473
|
+
(contentType === null || contentType === void 0 ? void 0 : contentType.includes('multipart/form-data')) &&
|
|
474
|
+
!contentType.includes('boundary')) {
|
|
475
|
+
headers.delete('Content-Type');
|
|
476
|
+
headers.delete('content-type');
|
|
477
|
+
options.headers = headers;
|
|
478
|
+
}
|
|
479
|
+
const request = new Request(resource, options);
|
|
480
|
+
if (request.url.startsWith(`${cap.getServerUrl()}/`)) {
|
|
481
|
+
return win.CapacitorWebFetch(resource, options);
|
|
482
|
+
}
|
|
483
|
+
const { method } = request;
|
|
484
|
+
if (method.toLocaleUpperCase() === 'GET' ||
|
|
485
|
+
method.toLocaleUpperCase() === 'HEAD' ||
|
|
486
|
+
method.toLocaleUpperCase() === 'OPTIONS' ||
|
|
487
|
+
method.toLocaleUpperCase() === 'TRACE') {
|
|
488
|
+
// a workaround for following android webview issue:
|
|
489
|
+
// https://issues.chromium.org/issues/40450316
|
|
490
|
+
// Sets the user-agent header to a custom value so that its not stripped
|
|
491
|
+
// on its way to the native layer
|
|
492
|
+
if (platform === 'android' && (options === null || options === void 0 ? void 0 : options.headers)) {
|
|
493
|
+
const userAgent = headers.get('User-Agent') || headers.get('user-agent');
|
|
494
|
+
if (userAgent !== null) {
|
|
495
|
+
headers.set('x-cap-user-agent', userAgent);
|
|
496
|
+
options.headers = headers;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
if (typeof resource === 'string') {
|
|
500
|
+
return await win.CapacitorWebFetch(createProxyUrl(resource, win), options);
|
|
501
|
+
}
|
|
502
|
+
else if (resource instanceof Request) {
|
|
503
|
+
const modifiedRequest = new Request(createProxyUrl(resource.url, win), resource);
|
|
504
|
+
return await win.CapacitorWebFetch(modifiedRequest, options);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const tag = `CapacitorHttp fetch ${Date.now()} ${resource}`;
|
|
508
|
+
console.time(tag);
|
|
509
|
+
try {
|
|
510
|
+
const { body } = request;
|
|
511
|
+
const optionHeaders = Object.fromEntries(request.headers.entries());
|
|
512
|
+
const { data: requestData, type, headers: requestHeaders, } = await convertBody((options === null || options === void 0 ? void 0 : options.body) || body || undefined, optionHeaders['Content-Type'] || optionHeaders['content-type']);
|
|
513
|
+
const nativeHeaders = Object.assign(Object.assign({}, requestHeaders), optionHeaders);
|
|
514
|
+
if (platform === 'android') {
|
|
515
|
+
if (headers.has('User-Agent')) {
|
|
516
|
+
nativeHeaders['User-Agent'] = headers.get('User-Agent');
|
|
517
|
+
}
|
|
518
|
+
if (headers.has('user-agent')) {
|
|
519
|
+
nativeHeaders['user-agent'] = headers.get('user-agent');
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', {
|
|
523
|
+
url: request.url,
|
|
524
|
+
method: method,
|
|
525
|
+
data: requestData,
|
|
526
|
+
dataType: type,
|
|
527
|
+
headers: nativeHeaders,
|
|
528
|
+
});
|
|
529
|
+
const contentType = nativeResponse.headers['Content-Type'] || nativeResponse.headers['content-type'];
|
|
530
|
+
let data = (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith('application/json'))
|
|
531
|
+
? JSON.stringify(nativeResponse.data)
|
|
532
|
+
: nativeResponse.data;
|
|
533
|
+
// use null data for 204 No Content HTTP response
|
|
534
|
+
if (nativeResponse.status === 204) {
|
|
535
|
+
data = null;
|
|
536
|
+
}
|
|
537
|
+
// intercept & parse response before returning
|
|
538
|
+
const response = new Response(data, {
|
|
539
|
+
headers: nativeResponse.headers,
|
|
540
|
+
status: nativeResponse.status,
|
|
541
|
+
});
|
|
542
|
+
/*
|
|
543
|
+
* copy url to response, `cordova-plugin-ionic` uses this url from the response
|
|
544
|
+
* we need `Object.defineProperty` because url is an inherited getter on the Response
|
|
545
|
+
* see: https://stackoverflow.com/a/57382543
|
|
546
|
+
* */
|
|
547
|
+
Object.defineProperty(response, 'url', {
|
|
548
|
+
value: nativeResponse.url,
|
|
549
|
+
});
|
|
550
|
+
console.timeEnd(tag);
|
|
551
|
+
return response;
|
|
552
|
+
}
|
|
553
|
+
catch (error) {
|
|
554
|
+
console.timeEnd(tag);
|
|
555
|
+
return Promise.reject(error);
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
window.XMLHttpRequest = function () {
|
|
559
|
+
const xhr = new win.CapacitorWebXMLHttpRequest.constructor();
|
|
560
|
+
Object.defineProperties(xhr, {
|
|
561
|
+
_headers: {
|
|
562
|
+
value: {},
|
|
563
|
+
writable: true,
|
|
564
|
+
},
|
|
565
|
+
_method: {
|
|
566
|
+
value: xhr.method,
|
|
567
|
+
writable: true,
|
|
568
|
+
},
|
|
569
|
+
});
|
|
570
|
+
const prototype = win.CapacitorWebXMLHttpRequest.prototype;
|
|
571
|
+
const isProgressEventAvailable = () => typeof ProgressEvent !== 'undefined' && ProgressEvent.prototype instanceof Event;
|
|
572
|
+
// XHR patch abort
|
|
573
|
+
prototype.abort = function () {
|
|
574
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
575
|
+
return win.CapacitorWebXMLHttpRequest.abort.call(this);
|
|
576
|
+
}
|
|
577
|
+
this.readyState = 0;
|
|
578
|
+
setTimeout(() => {
|
|
579
|
+
this.dispatchEvent(new Event('abort'));
|
|
580
|
+
this.dispatchEvent(new Event('loadend'));
|
|
581
|
+
});
|
|
582
|
+
};
|
|
583
|
+
// XHR patch open
|
|
584
|
+
prototype.open = function (method, url) {
|
|
585
|
+
this._method = method.toLocaleUpperCase();
|
|
586
|
+
this._url = url;
|
|
587
|
+
if (!this._method ||
|
|
588
|
+
this._method === 'GET' ||
|
|
589
|
+
this._method === 'HEAD' ||
|
|
590
|
+
this._method === 'OPTIONS' ||
|
|
591
|
+
this._method === 'TRACE') {
|
|
592
|
+
if (isRelativeOrProxyUrl(url)) {
|
|
593
|
+
return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
|
|
594
|
+
}
|
|
595
|
+
this._url = createProxyUrl(this._url, win);
|
|
596
|
+
return win.CapacitorWebXMLHttpRequest.open.call(this, method, this._url);
|
|
597
|
+
}
|
|
598
|
+
Object.defineProperties(this, {
|
|
599
|
+
readyState: {
|
|
600
|
+
get: function () {
|
|
601
|
+
var _a;
|
|
602
|
+
return (_a = this._readyState) !== null && _a !== void 0 ? _a : 0;
|
|
603
|
+
},
|
|
604
|
+
set: function (val) {
|
|
605
|
+
this._readyState = val;
|
|
606
|
+
setTimeout(() => {
|
|
607
|
+
this.dispatchEvent(new Event('readystatechange'));
|
|
608
|
+
});
|
|
609
|
+
},
|
|
610
|
+
},
|
|
611
|
+
});
|
|
612
|
+
setTimeout(() => {
|
|
613
|
+
this.dispatchEvent(new Event('loadstart'));
|
|
614
|
+
});
|
|
615
|
+
this.readyState = 1;
|
|
616
|
+
};
|
|
617
|
+
// XHR patch set request header
|
|
618
|
+
prototype.setRequestHeader = function (header, value) {
|
|
619
|
+
// a workaround for the following android web view issue:
|
|
620
|
+
// https://issues.chromium.org/issues/40450316
|
|
621
|
+
// Sets the user-agent header to a custom value so that its not stripped
|
|
622
|
+
// on its way to the native layer
|
|
623
|
+
if (platform === 'android' && (header === 'User-Agent' || header === 'user-agent')) {
|
|
624
|
+
header = 'x-cap-user-agent';
|
|
625
|
+
}
|
|
626
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
627
|
+
return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(this, header, value);
|
|
628
|
+
}
|
|
629
|
+
this._headers[header] = value;
|
|
630
|
+
};
|
|
631
|
+
// XHR patch send
|
|
632
|
+
prototype.send = function (body) {
|
|
633
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
634
|
+
return win.CapacitorWebXMLHttpRequest.send.call(this, body);
|
|
635
|
+
}
|
|
636
|
+
const tag = `CapacitorHttp XMLHttpRequest ${Date.now()} ${this._url}`;
|
|
637
|
+
console.time(tag);
|
|
638
|
+
try {
|
|
639
|
+
this.readyState = 2;
|
|
640
|
+
Object.defineProperties(this, {
|
|
641
|
+
response: {
|
|
642
|
+
value: '',
|
|
643
|
+
writable: true,
|
|
644
|
+
},
|
|
645
|
+
responseText: {
|
|
646
|
+
value: '',
|
|
647
|
+
writable: true,
|
|
648
|
+
},
|
|
649
|
+
responseURL: {
|
|
650
|
+
value: '',
|
|
651
|
+
writable: true,
|
|
652
|
+
},
|
|
653
|
+
status: {
|
|
654
|
+
value: 0,
|
|
655
|
+
writable: true,
|
|
656
|
+
},
|
|
657
|
+
});
|
|
658
|
+
convertBody(body).then(({ data, type, headers }) => {
|
|
659
|
+
let otherHeaders = this._headers != null && Object.keys(this._headers).length > 0 ? this._headers : undefined;
|
|
660
|
+
if (body instanceof FormData) {
|
|
661
|
+
if (!this._headers['Content-Type'] && !this._headers['content-type']) {
|
|
662
|
+
otherHeaders = Object.assign(Object.assign({}, otherHeaders), { 'Content-Type': `multipart/form-data; boundary=----WebKitFormBoundary${Math.random().toString(36).substring(2, 15)}` });
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
// intercept request & pass to the bridge
|
|
666
|
+
cap
|
|
667
|
+
.nativePromise('CapacitorHttp', 'request', {
|
|
668
|
+
url: this._url,
|
|
669
|
+
method: this._method,
|
|
670
|
+
data: data !== null ? data : undefined,
|
|
671
|
+
headers: Object.assign(Object.assign({}, headers), otherHeaders),
|
|
672
|
+
dataType: type,
|
|
673
|
+
})
|
|
674
|
+
.then((nativeResponse) => {
|
|
675
|
+
var _a;
|
|
676
|
+
// intercept & parse response before returning
|
|
677
|
+
if (this.readyState == 2) {
|
|
678
|
+
//TODO: Add progress event emission on native side
|
|
679
|
+
if (isProgressEventAvailable()) {
|
|
680
|
+
this.dispatchEvent(new ProgressEvent('progress', {
|
|
681
|
+
lengthComputable: true,
|
|
682
|
+
loaded: nativeResponse.data.length,
|
|
683
|
+
total: nativeResponse.data.length,
|
|
684
|
+
}));
|
|
685
|
+
}
|
|
686
|
+
this._headers = nativeResponse.headers;
|
|
687
|
+
this.status = nativeResponse.status;
|
|
688
|
+
if (this.responseType === '' || this.responseType === 'text') {
|
|
689
|
+
this.response =
|
|
690
|
+
typeof nativeResponse.data !== 'string'
|
|
691
|
+
? JSON.stringify(nativeResponse.data)
|
|
692
|
+
: nativeResponse.data;
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
this.response = nativeResponse.data;
|
|
696
|
+
}
|
|
697
|
+
this.responseText = ((_a = (nativeResponse.headers['Content-Type'] || nativeResponse.headers['content-type'])) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
|
|
698
|
+
? JSON.stringify(nativeResponse.data)
|
|
699
|
+
: nativeResponse.data;
|
|
700
|
+
this.responseURL = nativeResponse.url;
|
|
701
|
+
this.readyState = 4;
|
|
702
|
+
setTimeout(() => {
|
|
703
|
+
this.dispatchEvent(new Event('load'));
|
|
704
|
+
this.dispatchEvent(new Event('loadend'));
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
console.timeEnd(tag);
|
|
708
|
+
})
|
|
709
|
+
.catch((error) => {
|
|
710
|
+
this.status = error.status;
|
|
711
|
+
this._headers = error.headers;
|
|
712
|
+
this.response = error.data;
|
|
713
|
+
this.responseText = JSON.stringify(error.data);
|
|
714
|
+
this.responseURL = error.url;
|
|
715
|
+
this.readyState = 4;
|
|
716
|
+
if (isProgressEventAvailable()) {
|
|
717
|
+
this.dispatchEvent(new ProgressEvent('progress', {
|
|
718
|
+
lengthComputable: false,
|
|
719
|
+
loaded: 0,
|
|
720
|
+
total: 0,
|
|
721
|
+
}));
|
|
722
|
+
}
|
|
723
|
+
setTimeout(() => {
|
|
724
|
+
this.dispatchEvent(new Event('error'));
|
|
725
|
+
this.dispatchEvent(new Event('loadend'));
|
|
726
|
+
});
|
|
727
|
+
console.timeEnd(tag);
|
|
728
|
+
});
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
catch (error) {
|
|
732
|
+
this.status = 500;
|
|
733
|
+
this._headers = {};
|
|
734
|
+
this.response = error;
|
|
735
|
+
this.responseText = error.toString();
|
|
736
|
+
this.responseURL = this._url;
|
|
737
|
+
this.readyState = 4;
|
|
738
|
+
if (isProgressEventAvailable()) {
|
|
739
|
+
this.dispatchEvent(new ProgressEvent('progress', {
|
|
740
|
+
lengthComputable: false,
|
|
741
|
+
loaded: 0,
|
|
742
|
+
total: 0,
|
|
743
|
+
}));
|
|
744
|
+
}
|
|
745
|
+
setTimeout(() => {
|
|
746
|
+
this.dispatchEvent(new Event('error'));
|
|
747
|
+
this.dispatchEvent(new Event('loadend'));
|
|
748
|
+
});
|
|
749
|
+
console.timeEnd(tag);
|
|
750
|
+
}
|
|
751
|
+
};
|
|
752
|
+
// XHR patch getAllResponseHeaders
|
|
753
|
+
prototype.getAllResponseHeaders = function () {
|
|
754
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
755
|
+
return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(this);
|
|
756
|
+
}
|
|
757
|
+
let returnString = '';
|
|
758
|
+
for (const key in this._headers) {
|
|
759
|
+
if (key != 'Set-Cookie') {
|
|
760
|
+
returnString += key + ': ' + this._headers[key] + '\r\n';
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
return returnString;
|
|
764
|
+
};
|
|
765
|
+
// XHR patch getResponseHeader
|
|
766
|
+
prototype.getResponseHeader = function (name) {
|
|
767
|
+
if (isRelativeOrProxyUrl(this._url)) {
|
|
768
|
+
return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
|
|
769
|
+
}
|
|
770
|
+
return this._headers[name];
|
|
771
|
+
};
|
|
772
|
+
Object.setPrototypeOf(xhr, prototype);
|
|
773
|
+
return xhr;
|
|
774
|
+
};
|
|
775
|
+
Object.assign(window.XMLHttpRequest, win.CapacitorWebXMLHttpRequest.fullObject);
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
// patch window.console on iOS and store original console fns
|
|
779
|
+
const isIos = getPlatformId(win) === 'ios';
|
|
780
|
+
if (win.console && isIos) {
|
|
781
|
+
Object.defineProperties(win.console, BRIDGED_CONSOLE_METHODS.reduce((props, method) => {
|
|
782
|
+
const consoleMethod = win.console[method].bind(win.console);
|
|
783
|
+
props[method] = {
|
|
784
|
+
value: (...args) => {
|
|
785
|
+
const msgs = [...args];
|
|
786
|
+
cap.toNative('Console', 'log', {
|
|
787
|
+
level: method,
|
|
788
|
+
message: msgs.map(serializeConsoleMessage).join(' '),
|
|
789
|
+
});
|
|
790
|
+
return consoleMethod(...args);
|
|
791
|
+
},
|
|
792
|
+
};
|
|
793
|
+
return props;
|
|
794
|
+
}, {}));
|
|
795
|
+
}
|
|
796
|
+
cap.logJs = (msg, level) => {
|
|
797
|
+
switch (level) {
|
|
798
|
+
case 'error':
|
|
799
|
+
win.console.error(msg);
|
|
800
|
+
break;
|
|
801
|
+
case 'warn':
|
|
802
|
+
win.console.warn(msg);
|
|
803
|
+
break;
|
|
804
|
+
case 'info':
|
|
805
|
+
win.console.info(msg);
|
|
806
|
+
break;
|
|
807
|
+
default:
|
|
808
|
+
win.console.log(msg);
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
cap.logToNative = createLogToNative(win.console);
|
|
812
|
+
cap.logFromNative = createLogFromNative(win.console);
|
|
813
|
+
cap.handleError = (err) => win.console.error(err);
|
|
814
|
+
win.Capacitor = cap;
|
|
815
|
+
};
|
|
816
|
+
function initNativeBridge(win) {
|
|
817
|
+
const cap = win.Capacitor || {};
|
|
818
|
+
// keep a collection of callbacks for native response data
|
|
819
|
+
const callbacks = new Map();
|
|
820
|
+
const webviewServerUrl = typeof win.WEBVIEW_SERVER_URL === 'string' ? win.WEBVIEW_SERVER_URL : '';
|
|
821
|
+
cap.getServerUrl = () => webviewServerUrl;
|
|
822
|
+
cap.convertFileSrc = (filePath) => convertFileSrcServerUrl(webviewServerUrl, filePath);
|
|
823
|
+
// Counter of callback ids, randomized to avoid
|
|
824
|
+
// any issues during reloads if a call comes back with
|
|
825
|
+
// an existing callback id from an old session
|
|
826
|
+
let callbackIdCount = Math.floor(Math.random() * 134217728);
|
|
827
|
+
let postToNative = null;
|
|
828
|
+
const isNativePlatform = () => true;
|
|
829
|
+
const getPlatform = () => getPlatformId(win);
|
|
830
|
+
cap.getPlatform = getPlatform;
|
|
831
|
+
cap.isPluginAvailable = (name) => Object.prototype.hasOwnProperty.call(cap.Plugins, name);
|
|
832
|
+
cap.isNativePlatform = isNativePlatform;
|
|
833
|
+
// create the postToNative() fn if needed
|
|
834
|
+
if (getPlatformId(win) === 'android') {
|
|
835
|
+
// android platform
|
|
836
|
+
postToNative = (data) => {
|
|
837
|
+
var _a;
|
|
838
|
+
try {
|
|
839
|
+
win.androidBridge.postMessage(JSON.stringify(data));
|
|
840
|
+
}
|
|
841
|
+
catch (e) {
|
|
842
|
+
(_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.error(e);
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
else if (getPlatformId(win) === 'ios') {
|
|
847
|
+
// ios platform
|
|
848
|
+
postToNative = (data) => {
|
|
849
|
+
var _a;
|
|
850
|
+
try {
|
|
851
|
+
data.type = data.type ? data.type : 'message';
|
|
852
|
+
win.webkit.messageHandlers.bridge.postMessage(data);
|
|
853
|
+
}
|
|
854
|
+
catch (e) {
|
|
855
|
+
(_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.error(e);
|
|
856
|
+
}
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
cap.handleWindowError = (msg, url, lineNo, columnNo, err) => {
|
|
860
|
+
const str = msg.toLowerCase();
|
|
861
|
+
if (str.indexOf('script error') > -1) ;
|
|
862
|
+
else {
|
|
863
|
+
const errObj = {
|
|
864
|
+
type: 'js.error',
|
|
865
|
+
error: {
|
|
866
|
+
message: msg,
|
|
867
|
+
url: url,
|
|
868
|
+
line: lineNo,
|
|
869
|
+
col: columnNo,
|
|
870
|
+
errorObject: JSON.stringify(err),
|
|
871
|
+
},
|
|
872
|
+
};
|
|
873
|
+
if (err !== null) {
|
|
874
|
+
cap.handleError(err);
|
|
875
|
+
}
|
|
876
|
+
postToNative(errObj);
|
|
877
|
+
}
|
|
878
|
+
return false;
|
|
879
|
+
};
|
|
880
|
+
if (cap.DEBUG) {
|
|
881
|
+
window.onerror = cap.handleWindowError;
|
|
882
|
+
}
|
|
883
|
+
initLogger(win, cap);
|
|
884
|
+
/**
|
|
885
|
+
* Send a plugin method call to the native layer
|
|
886
|
+
*/
|
|
887
|
+
cap.toNative = (pluginName, methodName, options, storedCallback) => {
|
|
888
|
+
var _a, _b;
|
|
889
|
+
try {
|
|
890
|
+
if (typeof postToNative === 'function') {
|
|
891
|
+
let callbackId = '-1';
|
|
892
|
+
if (storedCallback &&
|
|
893
|
+
(typeof storedCallback.callback === 'function' || typeof storedCallback.resolve === 'function')) {
|
|
894
|
+
// store the call for later lookup
|
|
895
|
+
callbackId = String(++callbackIdCount);
|
|
896
|
+
callbacks.set(callbackId, storedCallback);
|
|
897
|
+
}
|
|
898
|
+
const callData = {
|
|
899
|
+
callbackId: callbackId,
|
|
900
|
+
pluginId: pluginName,
|
|
901
|
+
methodName: methodName,
|
|
902
|
+
options: options || {},
|
|
903
|
+
};
|
|
904
|
+
if (cap.isLoggingEnabled && pluginName !== 'Console') {
|
|
905
|
+
cap.logToNative(callData);
|
|
906
|
+
}
|
|
907
|
+
// post the call data to native
|
|
908
|
+
postToNative(callData);
|
|
909
|
+
return callbackId;
|
|
910
|
+
}
|
|
911
|
+
else {
|
|
912
|
+
(_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.warn(`implementation unavailable for: ${pluginName}`);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
catch (e) {
|
|
916
|
+
(_b = win === null || win === void 0 ? void 0 : win.console) === null || _b === void 0 ? void 0 : _b.error(e);
|
|
917
|
+
}
|
|
918
|
+
return null;
|
|
919
|
+
};
|
|
920
|
+
if (win === null || win === void 0 ? void 0 : win.androidBridge) {
|
|
921
|
+
win.androidBridge.onmessage = function (event) {
|
|
922
|
+
returnResult(JSON.parse(event.data));
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
/**
|
|
926
|
+
* Process a response from the native layer.
|
|
927
|
+
*/
|
|
928
|
+
cap.fromNative = (result) => {
|
|
929
|
+
returnResult(result);
|
|
930
|
+
};
|
|
931
|
+
const returnResult = (result) => {
|
|
932
|
+
var _a, _b;
|
|
933
|
+
if (cap.isLoggingEnabled && result.pluginId !== 'Console') {
|
|
934
|
+
cap.logFromNative(result);
|
|
935
|
+
}
|
|
936
|
+
// get the stored call, if it exists
|
|
937
|
+
try {
|
|
938
|
+
const storedCall = callbacks.get(result.callbackId);
|
|
939
|
+
if (storedCall) {
|
|
940
|
+
// looks like we've got a stored call
|
|
941
|
+
if (result.error) {
|
|
942
|
+
// ensure stacktraces by copying error properties to an Error
|
|
943
|
+
result.error = Object.keys(result.error).reduce((err, key) => {
|
|
944
|
+
// use any type to avoid importing util and compiling most of .ts files
|
|
945
|
+
err[key] = result.error[key];
|
|
946
|
+
return err;
|
|
947
|
+
}, new cap.Exception(''));
|
|
948
|
+
}
|
|
949
|
+
if (typeof storedCall.callback === 'function') {
|
|
950
|
+
// callback
|
|
951
|
+
if (result.success) {
|
|
952
|
+
storedCall.callback(result.data);
|
|
953
|
+
}
|
|
954
|
+
else {
|
|
955
|
+
storedCall.callback(null, result.error);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
else if (typeof storedCall.resolve === 'function') {
|
|
959
|
+
// promise
|
|
960
|
+
if (result.success) {
|
|
961
|
+
storedCall.resolve(result.data);
|
|
962
|
+
}
|
|
963
|
+
else {
|
|
964
|
+
storedCall.reject(result.error);
|
|
965
|
+
}
|
|
966
|
+
// no need to keep this stored callback
|
|
967
|
+
// around for a one time resolve promise
|
|
968
|
+
callbacks.delete(result.callbackId);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
else if (!result.success && result.error) {
|
|
972
|
+
// no stored callback, but if there was an error let's log it
|
|
973
|
+
(_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.warn(result.error);
|
|
974
|
+
}
|
|
975
|
+
if (result.save === false) {
|
|
976
|
+
callbacks.delete(result.callbackId);
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
catch (e) {
|
|
980
|
+
(_b = win === null || win === void 0 ? void 0 : win.console) === null || _b === void 0 ? void 0 : _b.error(e);
|
|
981
|
+
}
|
|
982
|
+
// always delete to prevent memory leaks
|
|
983
|
+
// overkill but we're not sure what apps will do with this data
|
|
984
|
+
delete result.data;
|
|
985
|
+
delete result.error;
|
|
986
|
+
};
|
|
987
|
+
cap.nativeCallback = (pluginName, methodName, options, callback) => {
|
|
988
|
+
if (typeof options === 'function') {
|
|
989
|
+
console.warn(`Using a callback as the 'options' parameter of 'nativeCallback()' is deprecated.`);
|
|
990
|
+
callback = options;
|
|
991
|
+
options = null;
|
|
992
|
+
}
|
|
993
|
+
return cap.toNative(pluginName, methodName, options, { callback });
|
|
994
|
+
};
|
|
995
|
+
cap.nativePromise = (pluginName, methodName, options) => {
|
|
996
|
+
return new Promise((resolve, reject) => {
|
|
997
|
+
cap.toNative(pluginName, methodName, options, {
|
|
998
|
+
resolve: resolve,
|
|
999
|
+
reject: reject,
|
|
1000
|
+
});
|
|
1001
|
+
});
|
|
1002
|
+
};
|
|
1003
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1004
|
+
cap.withPlugin = (_pluginId, _fn) => dummy;
|
|
1005
|
+
cap.Exception = CapacitorException;
|
|
1006
|
+
initEvents(win, cap);
|
|
1007
|
+
initLegacyHandlers(win, cap);
|
|
1008
|
+
initVendor(win, cap);
|
|
1009
|
+
win.Capacitor = cap;
|
|
1010
|
+
}
|
|
1011
|
+
initNativeBridge(w);
|
|
1012
|
+
};
|
|
1013
|
+
initBridge(typeof globalThis !== 'undefined'
|
|
1014
|
+
? globalThis
|
|
1015
|
+
: typeof self !== 'undefined'
|
|
1016
|
+
? self
|
|
1017
|
+
: typeof window !== 'undefined'
|
|
1018
|
+
? window
|
|
1019
|
+
: typeof global !== 'undefined'
|
|
1020
|
+
? global
|
|
1021
|
+
: {});
|
|
1022
|
+
|
|
1023
|
+
dummy = initBridge;
|
|
1024
|
+
|
|
1025
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1026
|
+
|
|
1027
|
+
return exports;
|
|
1028
|
+
|
|
1029
|
+
})({});
|