"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = __importDefault(require("util")); const parse5_1 = __importDefault(require("parse5")); const class_name_1 = __importDefault(require("../../shadow-ui/class-name")); const dom_1 = __importDefault(require("../dom")); const parse5_dom_adapter_1 = __importDefault(require("../dom/parse5-dom-adapter")); const resource_processor_base_1 = __importDefault(require("./resource-processor-base")); const parse5Utils = __importStar(require("../../utils/parse5")); const get_bom_1 = __importDefault(require("../../utils/get-bom")); const get_storage_key_1 = __importDefault(require("../../utils/get-storage-key")); const self_removing_scripts_1 = __importDefault(require("../../utils/self-removing-scripts")); const service_routes_1 = __importDefault(require("../../proxy/service-routes")); const json_1 = require("../../utils/json"); const PARSED_BODY_CREATED_EVENT_SCRIPT = parse5_1.default.parseFragment(self_removing_scripts_1.default.onBodyCreated).childNodes[0]; const PARSED_ORIGIN_FIRST_TITLE_ELEMENT_LOADED_SCRIPT = parse5_1.default.parseFragment(self_removing_scripts_1.default.onOriginFirstTitleLoaded).childNodes[0]; const PARSED_INIT_SCRIPT_FOR_IFRAME_TEMPLATE = parse5_1.default.parseFragment(self_removing_scripts_1.default.iframeInit).childNodes[0]; class PageProcessor extends resource_processor_base_1.default { constructor() { super(); this.RESTART_PROCESSING = Symbol(); } static _createShadowUIStyleLinkNode(url) { return parse5Utils.createElement('link', [ { name: 'rel', value: 'stylesheet' }, { name: 'type', value: 'text/css' }, { name: 'class', value: class_name_1.default.uiStylesheet }, { name: 'href', value: url }, ]); } static _createShadowUIScriptWithUrlNode(url) { return parse5Utils.createElement('script', [ { name: 'type', value: 'text/javascript' }, { name: 'class', value: class_name_1.default.script }, { name: 'charset', value: 'UTF-8' }, { name: 'src', value: url }, ]); } static _createShadowUIScriptWithContentNode(content) { const scriptAsContentElement = parse5Utils.createElement('script', [ { name: 'type', value: 'text/javascript' }, { name: 'class', value: class_name_1.default.script }, { name: 'charset', value: 'UTF-8' }, ]); scriptAsContentElement.childNodes = [parse5Utils.createTextNode(content, scriptAsContentElement)]; return scriptAsContentElement; } static _createRestoreStoragesScript(storageKey, storages) { const parsedDocumentFragment = parse5_1.default.parseFragment(util_1.default.format(self_removing_scripts_1.default.restoreStorages, storageKey, (0, json_1.stringify)(storages.localStorage), storageKey, (0, json_1.stringify)(storages.sessionStorage))); return parsedDocumentFragment.childNodes[0]; } static _getPageProcessingOptions(ctx, urlReplacer) { return { crossDomainProxyPort: ctx.serverInfo.crossDomainPort, isIframe: ctx.isIframe, stylesheets: ctx.getInjectableStyles(), scripts: ctx.getInjectableScripts(), urlReplacer: urlReplacer, isIframeWithImageSrc: ctx.contentInfo && ctx.contentInfo.isIframeWithImageSrc, }; } static _getPageMetas(metaEls, domAdapter) { const metas = []; for (let i = 0; i < metaEls.length; i++) { metas.push({ httpEquiv: domAdapter.getAttr(metaEls[i], 'http-equiv'), content: domAdapter.getAttr(metaEls[i], 'content'), charset: domAdapter.getAttr(metaEls[i], 'charset'), }); } return metas; } static _addPageResources(head, processingOptions, options) { const injectedResources = []; if (processingOptions.storages && options) { const storages = processingOptions.storages; const script = PageProcessor._createRestoreStoragesScript((0, get_storage_key_1.default)(options.sessionId, options.host), storages); injectedResources.push(script); } if (processingOptions.stylesheets) { processingOptions.stylesheets.forEach(stylesheetUrl => { injectedResources.unshift(PageProcessor._createShadowUIStyleLinkNode(stylesheetUrl)); }); } if (processingOptions.scripts) { processingOptions.scripts.forEach(scriptUrl => { injectedResources.push(PageProcessor._createShadowUIScriptWithUrlNode(scriptUrl)); }); } if (processingOptions.embeddedScripts) { processingOptions.embeddedScripts.forEach(script => { injectedResources.push(PageProcessor._createShadowUIScriptWithContentNode(script)); }); } if (processingOptions.userScripts) { processingOptions.userScripts.forEach(script => { injectedResources.push(PageProcessor._createShadowUIScriptWithUrlNode(script)); }); } for (let i = injectedResources.length - 1; i > -1; i--) parse5Utils.insertBeforeFirstScript(injectedResources[i], head); return injectedResources; } static _getTaskScriptNodeIndex(head, ctx) { const taskScriptUrls = [ ctx.resolveInjectableUrl(service_routes_1.default.task), ctx.resolveInjectableUrl(service_routes_1.default.iframeTask), ]; return parse5Utils.findNodeIndex(head, node => { return node.tagName === 'script' && !!node.attrs.find(attr => attr.name === 'class' && attr.value === class_name_1.default.script) && !!node.attrs.find(attr => attr.name === 'src' && taskScriptUrls.includes(attr.value)); }); } /** * Inject the service script after the first title element * or after injected resources, * if they are placed right after the