Innovenergy_trunk/frontend/node_modules/testcafe-hammerhead/lib/utils/self-removing-scripts.js

80 lines
3.1 KiB
JavaScript

"use strict";
// -------------------------------------------------------------
// WARNING: this file is used by both the client and the server.
// Do not use any browser or node-specific API!
// -------------------------------------------------------------
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const class_name_1 = __importDefault(require("../shadow-ui/class-name"));
const internal_properties_1 = __importDefault(require("../processing/dom/internal-properties"));
const CLEANUP_FORMATTING_REGEXP = /\n\s*|\/\*[\S\s]*?\*\//g;
function create(script) {
return `
<script class="${class_name_1.default.selfRemovingScript}">
(function () {
var currentScript = document.currentScript;
/* NOTE: IE11 doesn't support the 'currentScript' property */
if (!currentScript) {
var scripts = document.scripts;
var scriptsLength = scripts.length;
currentScript = scripts[scriptsLength - 1];
}
currentScript.parentNode.removeChild(currentScript);
${script}
})();
</script>
`.replace(CLEANUP_FORMATTING_REGEXP, '');
}
exports.default = {
iframeInit: create(`
var parentHammerhead = null;
if (!window["${internal_properties_1.default.hammerhead}"])
Object.defineProperty(window, "${internal_properties_1.default.documentWasCleaned}", { value: true, configurable: true });
try {
parentHammerhead = window.parent["${internal_properties_1.default.hammerhead}"];
} catch(e) {}
if (parentHammerhead)
parentHammerhead.sandbox.onIframeDocumentRecreated(window.frameElement);
`),
onWindowRecreation: create(`
var hammerhead = window["${internal_properties_1.default.hammerhead}"];
var sandbox = hammerhead && hammerhead.sandbox;
if (!sandbox) {
try {
sandbox = window.parent["${internal_properties_1.default.hammerhead}"].sandboxUtils.backup.get(window);
} catch(e) {}
}
if (sandbox) {
Object.defineProperty(window, "${internal_properties_1.default.documentWasCleaned}", { value: true, configurable: true });
sandbox.node.mutation.onDocumentCleaned(window, document);
/* NOTE: B234357 */
sandbox.node.processNodes(null, document);
}
`),
onBodyCreated: create(`
if (window["${internal_properties_1.default.hammerhead}"])
window["${internal_properties_1.default.hammerhead}"].sandbox.node.raiseBodyCreatedEvent();
`),
onOriginFirstTitleLoaded: create(`
window["${internal_properties_1.default.hammerhead}"].sandbox.node.onOriginFirstTitleElementInHeadLoaded();
`),
restoreStorages: create(`
window.localStorage.setItem("%s", %s);
window.sessionStorage.setItem("%s", %s);
`),
};module.exports = exports.default;