123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990 |
- /*! Capacitor: https://capacitorjs.com/ - MIT License */
- /* Generated File. Do not edit. */
- var nativeBridge = (function (exports) {
- 'use strict';
- var ExceptionCode;
- (function (ExceptionCode) {
- /**
- * API is not implemented.
- *
- * This usually means the API can't be used because it is not implemented for
- * the current platform.
- */
- ExceptionCode["Unimplemented"] = "UNIMPLEMENTED";
- /**
- * API is not available.
- *
- * This means the API can't be used right now because:
- * - it is currently missing a prerequisite, such as network connectivity
- * - it requires a particular platform or browser version
- */
- ExceptionCode["Unavailable"] = "UNAVAILABLE";
- })(ExceptionCode || (ExceptionCode = {}));
- class CapacitorException extends Error {
- constructor(message, code, data) {
- super(message);
- this.message = message;
- this.code = code;
- this.data = data;
- }
- }
- // For removing exports for iOS/Android, keep let for reassignment
- // eslint-disable-next-line
- let dummy = {};
- const readFileAsBase64 = (file) => new Promise((resolve, reject) => {
- const reader = new FileReader();
- reader.onloadend = () => {
- const data = reader.result;
- resolve(btoa(data));
- };
- reader.onerror = reject;
- reader.readAsBinaryString(file);
- });
- const convertFormData = async (formData) => {
- const newFormData = [];
- for (const pair of formData.entries()) {
- const [key, value] = pair;
- if (value instanceof File) {
- const base64File = await readFileAsBase64(value);
- newFormData.push({
- key,
- value: base64File,
- type: 'base64File',
- contentType: value.type,
- fileName: value.name,
- });
- }
- else {
- newFormData.push({ key, value, type: 'string' });
- }
- }
- return newFormData;
- };
- const convertBody = async (body, contentType) => {
- if (body instanceof ReadableStream) {
- const reader = body.getReader();
- const chunks = [];
- while (true) {
- const { done, value } = await reader.read();
- if (done)
- break;
- chunks.push(value);
- }
- const concatenated = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.length, 0));
- let position = 0;
- for (const chunk of chunks) {
- concatenated.set(chunk, position);
- position += chunk.length;
- }
- let data = new TextDecoder().decode(concatenated);
- let type;
- if (contentType === 'application/json') {
- try {
- data = JSON.parse(data);
- }
- catch (ignored) {
- // ignore
- }
- type = 'json';
- }
- else if (contentType === 'multipart/form-data') {
- type = 'formData';
- }
- else if (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith('image')) {
- type = 'image';
- }
- else if (contentType === 'application/octet-stream') {
- type = 'binary';
- }
- else {
- type = 'text';
- }
- return {
- data,
- type,
- headers: { 'Content-Type': contentType || 'application/octet-stream' },
- };
- }
- else if (body instanceof FormData) {
- const formData = await convertFormData(body);
- const boundary = `${Date.now()}`;
- return {
- data: formData,
- type: 'formData',
- headers: {
- 'Content-Type': `multipart/form-data; boundary=--${boundary}`,
- },
- };
- }
- else if (body instanceof File) {
- const fileData = await readFileAsBase64(body);
- return {
- data: fileData,
- type: 'file',
- headers: { 'Content-Type': body.type },
- };
- }
- return { data: body, type: 'json' };
- };
- const initBridge = (w) => {
- const getPlatformId = (win) => {
- var _a, _b;
- if (win === null || win === void 0 ? void 0 : win.androidBridge) {
- return 'android';
- }
- 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) {
- return 'ios';
- }
- else {
- return 'web';
- }
- };
- const convertFileSrcServerUrl = (webviewServerUrl, filePath) => {
- if (typeof filePath === 'string') {
- if (filePath.startsWith('/')) {
- return webviewServerUrl + '/_capacitor_file_' + filePath;
- }
- else if (filePath.startsWith('file://')) {
- return (webviewServerUrl + filePath.replace('file://', '/_capacitor_file_'));
- }
- else if (filePath.startsWith('content://')) {
- return (webviewServerUrl +
- filePath.replace('content:/', '/_capacitor_content_'));
- }
- }
- return filePath;
- };
- const initEvents = (win, cap) => {
- cap.addListener = (pluginName, eventName, callback) => {
- const callbackId = cap.nativeCallback(pluginName, 'addListener', {
- eventName: eventName,
- }, callback);
- return {
- remove: async () => {
- var _a;
- (_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.debug('Removing listener', pluginName, eventName);
- cap.removeListener(pluginName, callbackId, eventName, callback);
- },
- };
- };
- cap.removeListener = (pluginName, callbackId, eventName, callback) => {
- cap.nativeCallback(pluginName, 'removeListener', {
- callbackId: callbackId,
- eventName: eventName,
- }, callback);
- };
- cap.createEvent = (eventName, eventData) => {
- const doc = win.document;
- if (doc) {
- const ev = doc.createEvent('Events');
- ev.initEvent(eventName, false, false);
- if (eventData && typeof eventData === 'object') {
- for (const i in eventData) {
- // eslint-disable-next-line no-prototype-builtins
- if (eventData.hasOwnProperty(i)) {
- ev[i] = eventData[i];
- }
- }
- }
- return ev;
- }
- return null;
- };
- cap.triggerEvent = (eventName, target, eventData) => {
- const doc = win.document;
- const cordova = win.cordova;
- eventData = eventData || {};
- const ev = cap.createEvent(eventName, eventData);
- if (ev) {
- if (target === 'document') {
- if (cordova === null || cordova === void 0 ? void 0 : cordova.fireDocumentEvent) {
- cordova.fireDocumentEvent(eventName, eventData);
- return true;
- }
- else if (doc === null || doc === void 0 ? void 0 : doc.dispatchEvent) {
- return doc.dispatchEvent(ev);
- }
- }
- else if (target === 'window' && win.dispatchEvent) {
- return win.dispatchEvent(ev);
- }
- else if (doc === null || doc === void 0 ? void 0 : doc.querySelector) {
- const targetEl = doc.querySelector(target);
- if (targetEl) {
- return targetEl.dispatchEvent(ev);
- }
- }
- }
- return false;
- };
- win.Capacitor = cap;
- };
- const initLegacyHandlers = (win, cap) => {
- // define cordova if it's not there already
- win.cordova = win.cordova || {};
- const doc = win.document;
- const nav = win.navigator;
- if (nav) {
- nav.app = nav.app || {};
- nav.app.exitApp = () => {
- var _a;
- if (!((_a = cap.Plugins) === null || _a === void 0 ? void 0 : _a.App)) {
- win.console.warn('App plugin not installed');
- }
- else {
- cap.nativeCallback('App', 'exitApp', {});
- }
- };
- }
- if (doc) {
- const docAddEventListener = doc.addEventListener;
- doc.addEventListener = (...args) => {
- var _a;
- const eventName = args[0];
- const handler = args[1];
- if (eventName === 'deviceready' && handler) {
- Promise.resolve().then(handler);
- }
- else if (eventName === 'backbutton' && cap.Plugins.App) {
- // Add a dummy listener so Capacitor doesn't do the default
- // back button action
- if (!((_a = cap.Plugins) === null || _a === void 0 ? void 0 : _a.App)) {
- win.console.warn('App plugin not installed');
- }
- else {
- cap.Plugins.App.addListener('backButton', () => {
- // ignore
- });
- }
- }
- return docAddEventListener.apply(doc, args);
- };
- }
- // deprecated in v3, remove from v4
- cap.platform = cap.getPlatform();
- cap.isNative = cap.isNativePlatform();
- win.Capacitor = cap;
- };
- const initVendor = (win, cap) => {
- const Ionic = (win.Ionic = win.Ionic || {});
- const IonicWebView = (Ionic.WebView = Ionic.WebView || {});
- const Plugins = cap.Plugins;
- IonicWebView.getServerBasePath = (callback) => {
- var _a;
- (_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.getServerBasePath().then((result) => {
- callback(result.path);
- });
- };
- IonicWebView.setServerBasePath = (path) => {
- var _a;
- (_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerBasePath({ path });
- };
- IonicWebView.persistServerBasePath = () => {
- var _a;
- (_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.persistServerBasePath();
- };
- IonicWebView.convertFileSrc = (url) => cap.convertFileSrc(url);
- win.Capacitor = cap;
- win.Ionic.WebView = IonicWebView;
- };
- const initLogger = (win, cap) => {
- const BRIDGED_CONSOLE_METHODS = [
- 'debug',
- 'error',
- 'info',
- 'log',
- 'trace',
- 'warn',
- ];
- const createLogFromNative = (c) => (result) => {
- if (isFullConsole(c)) {
- const success = result.success === true;
- const tagStyles = success
- ? 'font-style: italic; font-weight: lighter; color: gray'
- : 'font-style: italic; font-weight: lighter; color: red';
- c.groupCollapsed('%cresult %c' +
- result.pluginId +
- '.' +
- result.methodName +
- ' (#' +
- result.callbackId +
- ')', tagStyles, 'font-style: italic; font-weight: bold; color: #444');
- if (result.success === false) {
- c.error(result.error);
- }
- else {
- c.dir(result.data);
- }
- c.groupEnd();
- }
- else {
- if (result.success === false) {
- c.error('LOG FROM NATIVE', result.error);
- }
- else {
- c.log('LOG FROM NATIVE', result.data);
- }
- }
- };
- const createLogToNative = (c) => (call) => {
- if (isFullConsole(c)) {
- c.groupCollapsed('%cnative %c' +
- call.pluginId +
- '.' +
- call.methodName +
- ' (#' +
- call.callbackId +
- ')', 'font-weight: lighter; color: gray', 'font-weight: bold; color: #000');
- c.dir(call);
- c.groupEnd();
- }
- else {
- c.log('LOG TO NATIVE: ', call);
- }
- };
- const isFullConsole = (c) => {
- if (!c) {
- return false;
- }
- return (typeof c.groupCollapsed === 'function' ||
- typeof c.groupEnd === 'function' ||
- typeof c.dir === 'function');
- };
- const serializeConsoleMessage = (msg) => {
- if (typeof msg === 'object') {
- try {
- msg = JSON.stringify(msg);
- }
- catch (e) {
- // ignore
- }
- }
- return String(msg);
- };
- const platform = getPlatformId(win);
- if (platform == 'android' || platform == 'ios') {
- // patch document.cookie on Android/iOS
- win.CapacitorCookiesDescriptor =
- Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') ||
- Object.getOwnPropertyDescriptor(HTMLDocument.prototype, 'cookie');
- let doPatchCookies = false;
- // check if capacitor cookies is disabled before patching
- if (platform === 'ios') {
- // Use prompt to synchronously get capacitor cookies config.
- // https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323
- const payload = {
- type: 'CapacitorCookies.isEnabled',
- };
- const isCookiesEnabled = prompt(JSON.stringify(payload));
- if (isCookiesEnabled === 'true') {
- doPatchCookies = true;
- }
- }
- else if (typeof win.CapacitorCookiesAndroidInterface !== 'undefined') {
- const isCookiesEnabled = win.CapacitorCookiesAndroidInterface.isEnabled();
- if (isCookiesEnabled === true) {
- doPatchCookies = true;
- }
- }
- if (doPatchCookies) {
- Object.defineProperty(document, 'cookie', {
- get: function () {
- var _a, _b, _c;
- if (platform === 'ios') {
- // Use prompt to synchronously get cookies.
- // https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323
- const payload = {
- type: 'CapacitorCookies.get',
- };
- const res = prompt(JSON.stringify(payload));
- return res;
- }
- else if (typeof win.CapacitorCookiesAndroidInterface !== 'undefined') {
- // return original document.cookie since Android does not support filtering of `httpOnly` cookies
- 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 : '';
- }
- },
- set: function (val) {
- const cookiePairs = val.split(';');
- const domainSection = val.toLowerCase().split('domain=')[1];
- const domain = cookiePairs.length > 1 &&
- domainSection != null &&
- domainSection.length > 0
- ? domainSection.split(';')[0].trim()
- : '';
- if (platform === 'ios') {
- // Use prompt to synchronously set cookies.
- // https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323
- const payload = {
- type: 'CapacitorCookies.set',
- action: val,
- domain,
- };
- prompt(JSON.stringify(payload));
- }
- else if (typeof win.CapacitorCookiesAndroidInterface !== 'undefined') {
- win.CapacitorCookiesAndroidInterface.setCookie(domain, val);
- }
- },
- });
- }
- // patch fetch / XHR on Android/iOS
- // store original fetch & XHR functions
- win.CapacitorWebFetch = window.fetch;
- win.CapacitorWebXMLHttpRequest = {
- abort: window.XMLHttpRequest.prototype.abort,
- constructor: window.XMLHttpRequest.prototype.constructor,
- fullObject: window.XMLHttpRequest,
- getAllResponseHeaders: window.XMLHttpRequest.prototype.getAllResponseHeaders,
- getResponseHeader: window.XMLHttpRequest.prototype.getResponseHeader,
- open: window.XMLHttpRequest.prototype.open,
- prototype: window.XMLHttpRequest.prototype,
- send: window.XMLHttpRequest.prototype.send,
- setRequestHeader: window.XMLHttpRequest.prototype.setRequestHeader,
- };
- let doPatchHttp = false;
- // check if capacitor http is disabled before patching
- if (platform === 'ios') {
- // Use prompt to synchronously get capacitor http config.
- // https://stackoverflow.com/questions/29249132/wkwebview-complex-communication-between-javascript-native-code/49474323#49474323
- const payload = {
- type: 'CapacitorHttp',
- };
- const isHttpEnabled = prompt(JSON.stringify(payload));
- if (isHttpEnabled === 'true') {
- doPatchHttp = true;
- }
- }
- else if (typeof win.CapacitorHttpAndroidInterface !== 'undefined') {
- const isHttpEnabled = win.CapacitorHttpAndroidInterface.isEnabled();
- if (isHttpEnabled === true) {
- doPatchHttp = true;
- }
- }
- if (doPatchHttp) {
- // fetch patch
- window.fetch = async (resource, options) => {
- const request = new Request(resource, options);
- if (request.url.startsWith(`${cap.getServerUrl()}/`)) {
- return win.CapacitorWebFetch(resource, options);
- }
- const tag = `CapacitorHttp fetch ${Date.now()} ${resource}`;
- console.time(tag);
- try {
- // intercept request & pass to the bridge
- const { body, method } = request;
- const optionHeaders = Object.fromEntries(request.headers.entries());
- const { data: requestData, type, headers, } = await convertBody((options === null || options === void 0 ? void 0 : options.body) || body || undefined, optionHeaders['Content-Type'] || optionHeaders['content-type']);
- const nativeResponse = await cap.nativePromise('CapacitorHttp', 'request', {
- url: request.url,
- method: method,
- data: requestData,
- dataType: type,
- headers: Object.assign(Object.assign({}, headers), optionHeaders),
- });
- const contentType = nativeResponse.headers['Content-Type'] ||
- nativeResponse.headers['content-type'];
- let data = (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith('application/json'))
- ? JSON.stringify(nativeResponse.data)
- : nativeResponse.data;
- // use null data for 204 No Content HTTP response
- if (nativeResponse.status === 204) {
- data = null;
- }
- // intercept & parse response before returning
- const response = new Response(data, {
- headers: nativeResponse.headers,
- status: nativeResponse.status,
- });
- /*
- * copy url to response, `cordova-plugin-ionic` uses this url from the response
- * we need `Object.defineProperty` because url is an inherited getter on the Response
- * see: https://stackoverflow.com/a/57382543
- * */
- Object.defineProperty(response, 'url', {
- value: nativeResponse.url,
- });
- console.timeEnd(tag);
- return response;
- }
- catch (error) {
- console.timeEnd(tag);
- return Promise.reject(error);
- }
- };
- window.XMLHttpRequest = function () {
- const xhr = new win.CapacitorWebXMLHttpRequest.constructor();
- Object.defineProperties(xhr, {
- _headers: {
- value: {},
- writable: true,
- },
- _method: {
- value: xhr.method,
- writable: true,
- },
- readyState: {
- get: function () {
- var _a;
- return (_a = this._readyState) !== null && _a !== void 0 ? _a : 0;
- },
- set: function (val) {
- this._readyState = val;
- setTimeout(() => {
- this.dispatchEvent(new Event('readystatechange'));
- });
- },
- },
- });
- xhr.readyState = 0;
- const prototype = win.CapacitorWebXMLHttpRequest.prototype;
- const isRelativeURL = (url) => !url || !(url.startsWith('http:') || url.startsWith('https:'));
- const isProgressEventAvailable = () => typeof ProgressEvent !== 'undefined' &&
- ProgressEvent.prototype instanceof Event;
- // XHR patch abort
- prototype.abort = function () {
- if (isRelativeURL(this._url)) {
- return win.CapacitorWebXMLHttpRequest.abort.call(this);
- }
- this.readyState = 0;
- setTimeout(() => {
- this.dispatchEvent(new Event('abort'));
- this.dispatchEvent(new Event('loadend'));
- });
- };
- // XHR patch open
- prototype.open = function (method, url) {
- this._url = url;
- this._method = method;
- if (isRelativeURL(url)) {
- return win.CapacitorWebXMLHttpRequest.open.call(this, method, url);
- }
- setTimeout(() => {
- this.dispatchEvent(new Event('loadstart'));
- });
- this.readyState = 1;
- };
- // XHR patch set request header
- prototype.setRequestHeader = function (header, value) {
- if (isRelativeURL(this._url)) {
- return win.CapacitorWebXMLHttpRequest.setRequestHeader.call(this, header, value);
- }
- this._headers[header] = value;
- };
- // XHR patch send
- prototype.send = function (body) {
- if (isRelativeURL(this._url)) {
- return win.CapacitorWebXMLHttpRequest.send.call(this, body);
- }
- const tag = `CapacitorHttp XMLHttpRequest ${Date.now()} ${this._url}`;
- console.time(tag);
- try {
- this.readyState = 2;
- Object.defineProperties(this, {
- response: {
- value: '',
- writable: true,
- },
- responseText: {
- value: '',
- writable: true,
- },
- responseURL: {
- value: '',
- writable: true,
- },
- status: {
- value: 0,
- writable: true,
- },
- });
- convertBody(body).then(({ data, type, headers }) => {
- const otherHeaders = this._headers != null && Object.keys(this._headers).length > 0
- ? this._headers
- : undefined;
- // intercept request & pass to the bridge
- cap
- .nativePromise('CapacitorHttp', 'request', {
- url: this._url,
- method: this._method,
- data: data !== null ? data : undefined,
- headers: Object.assign(Object.assign({}, headers), otherHeaders),
- dataType: type,
- })
- .then((nativeResponse) => {
- var _a;
- // intercept & parse response before returning
- if (this.readyState == 2) {
- //TODO: Add progress event emission on native side
- if (isProgressEventAvailable()) {
- this.dispatchEvent(new ProgressEvent('progress', {
- lengthComputable: true,
- loaded: nativeResponse.data.length,
- total: nativeResponse.data.length,
- }));
- }
- this._headers = nativeResponse.headers;
- this.status = nativeResponse.status;
- const responseString = typeof nativeResponse.data !== 'string'
- ? JSON.stringify(nativeResponse.data)
- : nativeResponse.data;
- if (this.responseType === '' ||
- this.responseType === 'text') {
- this.response = responseString;
- }
- else if (this.responseType === 'blob') {
- this.response = new Blob([responseString], {
- type: 'application/json',
- });
- }
- else if (this.responseType === 'arraybuffer') {
- const encoder = new TextEncoder();
- const uint8Array = encoder.encode(responseString);
- this.response = uint8Array.buffer;
- }
- else {
- this.response = nativeResponse.data;
- }
- this.responseText = ((_a = nativeResponse.headers['Content-Type']) === null || _a === void 0 ? void 0 : _a.startsWith('application/json'))
- ? JSON.stringify(nativeResponse.data)
- : nativeResponse.data;
- this.responseURL = nativeResponse.url;
- this.readyState = 4;
- setTimeout(() => {
- this.dispatchEvent(new Event('load'));
- this.dispatchEvent(new Event('loadend'));
- });
- }
- console.timeEnd(tag);
- })
- .catch((error) => {
- this.status = error.status;
- this._headers = error.headers;
- this.response = error.data;
- this.responseText = JSON.stringify(error.data);
- this.responseURL = error.url;
- this.readyState = 4;
- if (isProgressEventAvailable()) {
- this.dispatchEvent(new ProgressEvent('progress', {
- lengthComputable: false,
- loaded: 0,
- total: 0,
- }));
- }
- setTimeout(() => {
- this.dispatchEvent(new Event('error'));
- this.dispatchEvent(new Event('loadend'));
- });
- console.timeEnd(tag);
- });
- });
- }
- catch (error) {
- this.status = 500;
- this._headers = {};
- this.response = error;
- this.responseText = error.toString();
- this.responseURL = this._url;
- this.readyState = 4;
- if (isProgressEventAvailable()) {
- this.dispatchEvent(new ProgressEvent('progress', {
- lengthComputable: false,
- loaded: 0,
- total: 0,
- }));
- }
- setTimeout(() => {
- this.dispatchEvent(new Event('error'));
- this.dispatchEvent(new Event('loadend'));
- });
- console.timeEnd(tag);
- }
- };
- // XHR patch getAllResponseHeaders
- prototype.getAllResponseHeaders = function () {
- if (isRelativeURL(this._url)) {
- return win.CapacitorWebXMLHttpRequest.getAllResponseHeaders.call(this);
- }
- let returnString = '';
- for (const key in this._headers) {
- if (key.toLowerCase() !== 'set-cookie') {
- returnString += key + ': ' + this._headers[key] + '\r\n';
- }
- }
- return returnString;
- };
- // XHR patch getResponseHeader
- prototype.getResponseHeader = function (name) {
- if (isRelativeURL(this._url)) {
- return win.CapacitorWebXMLHttpRequest.getResponseHeader.call(this, name);
- }
- for (const key in this._headers) {
- if (key.toLowerCase() === name.toLowerCase()) {
- return this._headers[key];
- }
- }
- return null;
- };
- Object.setPrototypeOf(xhr, prototype);
- return xhr;
- };
- Object.assign(window.XMLHttpRequest, win.CapacitorWebXMLHttpRequest.fullObject);
- }
- }
- // patch window.console on iOS and store original console fns
- const isIos = getPlatformId(win) === 'ios';
- if (win.console && isIos) {
- Object.defineProperties(win.console, BRIDGED_CONSOLE_METHODS.reduce((props, method) => {
- const consoleMethod = win.console[method].bind(win.console);
- props[method] = {
- value: (...args) => {
- const msgs = [...args];
- cap.toNative('Console', 'log', {
- level: method,
- message: msgs.map(serializeConsoleMessage).join(' '),
- });
- return consoleMethod(...args);
- },
- };
- return props;
- }, {}));
- }
- cap.logJs = (msg, level) => {
- switch (level) {
- case 'error':
- win.console.error(msg);
- break;
- case 'warn':
- win.console.warn(msg);
- break;
- case 'info':
- win.console.info(msg);
- break;
- default:
- win.console.log(msg);
- }
- };
- cap.logToNative = createLogToNative(win.console);
- cap.logFromNative = createLogFromNative(win.console);
- cap.handleError = err => win.console.error(err);
- win.Capacitor = cap;
- };
- function initNativeBridge(win) {
- const cap = win.Capacitor || {};
- // keep a collection of callbacks for native response data
- const callbacks = new Map();
- const webviewServerUrl = typeof win.WEBVIEW_SERVER_URL === 'string' ? win.WEBVIEW_SERVER_URL : '';
- cap.getServerUrl = () => webviewServerUrl;
- cap.convertFileSrc = filePath => convertFileSrcServerUrl(webviewServerUrl, filePath);
- // Counter of callback ids, randomized to avoid
- // any issues during reloads if a call comes back with
- // an existing callback id from an old session
- let callbackIdCount = Math.floor(Math.random() * 134217728);
- let postToNative = null;
- const isNativePlatform = () => true;
- const getPlatform = () => getPlatformId(win);
- cap.getPlatform = getPlatform;
- cap.isPluginAvailable = name => Object.prototype.hasOwnProperty.call(cap.Plugins, name);
- cap.isNativePlatform = isNativePlatform;
- // create the postToNative() fn if needed
- if (getPlatformId(win) === 'android') {
- // android platform
- postToNative = data => {
- var _a;
- try {
- win.androidBridge.postMessage(JSON.stringify(data));
- }
- catch (e) {
- (_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.error(e);
- }
- };
- }
- else if (getPlatformId(win) === 'ios') {
- // ios platform
- postToNative = data => {
- var _a;
- try {
- data.type = data.type ? data.type : 'message';
- win.webkit.messageHandlers.bridge.postMessage(data);
- }
- catch (e) {
- (_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.error(e);
- }
- };
- }
- cap.handleWindowError = (msg, url, lineNo, columnNo, err) => {
- const str = msg.toLowerCase();
- if (str.indexOf('script error') > -1) ;
- else {
- const errObj = {
- type: 'js.error',
- error: {
- message: msg,
- url: url,
- line: lineNo,
- col: columnNo,
- errorObject: JSON.stringify(err),
- },
- };
- if (err !== null) {
- cap.handleError(err);
- }
- postToNative(errObj);
- }
- return false;
- };
- if (cap.DEBUG) {
- window.onerror = cap.handleWindowError;
- }
- initLogger(win, cap);
- /**
- * Send a plugin method call to the native layer
- */
- cap.toNative = (pluginName, methodName, options, storedCallback) => {
- var _a, _b;
- try {
- if (typeof postToNative === 'function') {
- let callbackId = '-1';
- if (storedCallback &&
- (typeof storedCallback.callback === 'function' ||
- typeof storedCallback.resolve === 'function')) {
- // store the call for later lookup
- callbackId = String(++callbackIdCount);
- callbacks.set(callbackId, storedCallback);
- }
- const callData = {
- callbackId: callbackId,
- pluginId: pluginName,
- methodName: methodName,
- options: options || {},
- };
- if (cap.isLoggingEnabled && pluginName !== 'Console') {
- cap.logToNative(callData);
- }
- // post the call data to native
- postToNative(callData);
- return callbackId;
- }
- else {
- (_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.warn(`implementation unavailable for: ${pluginName}`);
- }
- }
- catch (e) {
- (_b = win === null || win === void 0 ? void 0 : win.console) === null || _b === void 0 ? void 0 : _b.error(e);
- }
- return null;
- };
- if (win === null || win === void 0 ? void 0 : win.androidBridge) {
- win.androidBridge.onmessage = function (event) {
- returnResult(JSON.parse(event.data));
- };
- }
- /**
- * Process a response from the native layer.
- */
- cap.fromNative = result => {
- returnResult(result);
- };
- const returnResult = (result) => {
- var _a, _b;
- if (cap.isLoggingEnabled && result.pluginId !== 'Console') {
- cap.logFromNative(result);
- }
- // get the stored call, if it exists
- try {
- const storedCall = callbacks.get(result.callbackId);
- if (storedCall) {
- // looks like we've got a stored call
- if (result.error) {
- // ensure stacktraces by copying error properties to an Error
- result.error = Object.keys(result.error).reduce((err, key) => {
- // use any type to avoid importing util and compiling most of .ts files
- err[key] = result.error[key];
- return err;
- }, new cap.Exception(''));
- }
- if (typeof storedCall.callback === 'function') {
- // callback
- if (result.success) {
- storedCall.callback(result.data);
- }
- else {
- storedCall.callback(null, result.error);
- }
- }
- else if (typeof storedCall.resolve === 'function') {
- // promise
- if (result.success) {
- storedCall.resolve(result.data);
- }
- else {
- storedCall.reject(result.error);
- }
- // no need to keep this stored callback
- // around for a one time resolve promise
- callbacks.delete(result.callbackId);
- }
- }
- else if (!result.success && result.error) {
- // no stored callback, but if there was an error let's log it
- (_a = win === null || win === void 0 ? void 0 : win.console) === null || _a === void 0 ? void 0 : _a.warn(result.error);
- }
- if (result.save === false) {
- callbacks.delete(result.callbackId);
- }
- }
- catch (e) {
- (_b = win === null || win === void 0 ? void 0 : win.console) === null || _b === void 0 ? void 0 : _b.error(e);
- }
- // always delete to prevent memory leaks
- // overkill but we're not sure what apps will do with this data
- delete result.data;
- delete result.error;
- };
- cap.nativeCallback = (pluginName, methodName, options, callback) => {
- if (typeof options === 'function') {
- console.warn(`Using a callback as the 'options' parameter of 'nativeCallback()' is deprecated.`);
- callback = options;
- options = null;
- }
- return cap.toNative(pluginName, methodName, options, { callback });
- };
- cap.nativePromise = (pluginName, methodName, options) => {
- return new Promise((resolve, reject) => {
- cap.toNative(pluginName, methodName, options, {
- resolve: resolve,
- reject: reject,
- });
- });
- };
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
- cap.withPlugin = (_pluginId, _fn) => dummy;
- cap.Exception = CapacitorException;
- initEvents(win, cap);
- initLegacyHandlers(win, cap);
- initVendor(win, cap);
- win.Capacitor = cap;
- }
- initNativeBridge(w);
- };
- initBridge(typeof globalThis !== 'undefined'
- ? globalThis
- : typeof self !== 'undefined'
- ? self
- : typeof window !== 'undefined'
- ? window
- : typeof global !== 'undefined'
- ? global
- : {});
- dummy = initBridge;
- Object.defineProperty(exports, '__esModule', { value: true });
- return exports;
- })({});
|