@formidable-webview/webshell
Version:
🔥 Craft Robust React Native WebView-based components with ease.
39 lines (27 loc) • 6.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.WebFeaturesLoader = void 0;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
/* babel-plugin-inline-import './web-features-loader.webjs' */
var featuresLoaderScript = "var messagesHandlerRegistry = {};\n\nvar safePostMessage = (function () {\n return window.ReactNativeWebView &&\n typeof window.ReactNativeWebView.postMessage === 'function'\n ? window.ReactNativeWebView.postMessage.bind(window.ReactNativeWebView)\n : typeof window.postMessage === 'function'\n ? window.postMessage\n : function throwPostMessage() {\n throw 'Missing postMessage. You must run this script in a WebView or @formidable-webview/ersatz.';\n };\n})();\n\nfunction numericFromPxString(pixelString) {\n return pixelString ? parseFloat(pixelString.match(/[\\d.]+/)) : 0;\n}\n\nfunction makeCallbackSafe(onError, callback) {\n return function () {\n try {\n callback.apply(null, arguments);\n } catch (e) {\n onError(e);\n }\n };\n}\n\nfunction __getDOMSelection(request, multiple) {\n var normalRequest =\n typeof request === 'string' ? { query: request } : request;\n var selection;\n if (\n !multiple &&\n (normalRequest.query === 'html' || normalRequest.tagName === 'html')\n ) {\n return document.documentElement;\n }\n if (\n !multiple &&\n (normalRequest.query === 'body' || normalRequest.tagName === 'body')\n ) {\n return document.body;\n }\n if (normalRequest.query) {\n return multiple\n ? document.querySelectorAll(normalRequest.query)\n : document.querySelector(normalRequest.query);\n }\n if (normalRequest.className) {\n selection = document.getElementsByClassName(normalRequest.className);\n return multiple ? selection : (selection.length && selection[0]) || null;\n }\n if (normalRequest.id) {\n return document.getElementById(normalRequest.id) || null;\n }\n if (normalRequest.tagName) {\n selection = document.getElementsByTagName(normalRequest.tagName);\n if (multiple) {\n return selection;\n }\n return (selection.length && selection[0]) || null;\n }\n}\n\nfunction sendLogMessage(snippetIdentifier, severity, message) {\n safePostMessage(\n JSON.stringify({\n type: 'log',\n severity: severity,\n identifier: snippetIdentifier,\n __isWebshellPostMessage: true,\n body: message\n })\n );\n}\n\nfunction sendErrorMessage(identifier, e) {\n var message =\n typeof e === 'string'\n ? e\n : typeof e === 'object' && typeof e.message === 'string'\n ? e.message\n : 'unknown error';\n console.error(message);\n safePostMessage(\n JSON.stringify({\n type: 'error',\n identifier: identifier,\n __isWebshellPostMessage: true,\n body: message\n })\n );\n}\n\nfunction createLogger(identifier, severity) {\n return function (text) {\n var logger = console[severity] || console.log;\n var message = identifier + ': ' + text;\n logger && logger.call(console, message);\n window.ReactNativeWebshell.debug &&\n sendLogMessage(identifier, severity, message);\n };\n}\n\nfunction registerFeature(specs) {\n if (specs && typeof specs === 'object') {\n var executable = specs.source;\n var snippetIdentifier = specs.identifier;\n var snippetOptions = specs.options || {};\n } else {\n return;\n }\n messagesHandlerRegistry[specs.identifier] = {};\n var domUtils = {\n getDOMSelection: function (selection) {\n return __getDOMSelection(selection, false);\n },\n getDOMSelectionAll: function (selection) {\n return __getDOMSelection(selection, true);\n },\n numericFromPxString: numericFromPxString\n };\n var __makeCallbackSafe = makeCallbackSafe.bind(\n null,\n sendErrorMessage.bind(null, snippetIdentifier)\n );\n var context = {\n options: snippetOptions,\n utils: domUtils,\n makeCallbackSafe: __makeCallbackSafe,\n onShellMessage: function (eventId, messageHandler) {\n messagesHandlerRegistry[specs.identifier][eventId] = __makeCallbackSafe(\n messageHandler\n );\n },\n postMessageToShell: function () {\n var message = arguments.length > 1 ? arguments[1] : arguments[0];\n var eventId = arguments.length > 1 ? arguments[0] : 'default';\n safePostMessage(\n JSON.stringify({\n type: 'feature',\n identifier: snippetIdentifier,\n body: message,\n eventId: eventId,\n __isWebshellPostMessage: true\n })\n );\n },\n info: createLogger(snippetIdentifier, 'info'),\n warn: createLogger(snippetIdentifier, 'warn')\n };\n if (executable && typeof executable === 'function') {\n try {\n executable.call(null, context);\n } catch (e) {\n sendErrorMessage(snippetIdentifier, e);\n }\n }\n}\n\nwindow.ReactNativeWebshell = {\n debug: true,\n registerFeature: registerFeature,\n sendErrorMessage: sendErrorMessage,\n postMessageToWeb: function (identifier, eventId, message) {\n var identifierReg = messagesHandlerRegistry[identifier];\n if (identifierReg) {\n var handler = identifierReg[eventId];\n typeof handler === 'function' && handler(message);\n }\n }\n};\n\nsafePostMessage(\n JSON.stringify({\n type: 'init',\n __isWebshellPostMessage: true\n })\n);\n";
function serializeFeature(feature) {
return "{source:".concat(feature.script, ",identifier:").concat(JSON.stringify(feature.identifier), ",options:").concat(JSON.stringify(feature.options || {}), "}");
}
function registerFeature(feat) {
return "try {\n window.ReactNativeWebshell.registerFeature(".concat(serializeFeature(feat), ");\n } catch (e) {\n window.ReactNativeWebshell.sendErrorMessage(").concat(JSON.stringify(feat.identifier), ",e);\n };");
}
function assembleScript(feats) {
return "".concat(featuresLoaderScript, "(function(){").concat(feats.map(registerFeature).join('\n'), ";})();");
}
var WebFeaturesLoader = function WebFeaturesLoader(features) {
_classCallCheck(this, WebFeaturesLoader);
_defineProperty(this, "features", void 0);
_defineProperty(this, "assembledFeaturesScript", void 0);
this.features = features;
this.assembledFeaturesScript = assembleScript(features);
};
exports.WebFeaturesLoader = WebFeaturesLoader;
//# sourceMappingURL=WebFeaturesLoader.js.map