skynet-mysky-utils
Version:
Skynet common MySky utilities
127 lines • 4.01 kB
JavaScript
;
// TODO: Move some of these to skynet-utils.
exports.__esModule = true;
exports.ensurePrefix = exports.trimSuffix = exports.removeAdjacentChars = exports.ensureUrl = exports.createFullScreenIframe = exports.createIframe = void 0;
/**
* Creates an invisible iframe with the given src and adds it to the page.
*
* @param srcUrl - The URL to use as src for the iframe.
* @param name - The name for the iframe element.
* @returns - The iframe element.
* @throws - Will throw if the iframe could not be created.
*/
function createIframe(srcUrl, name) {
srcUrl = ensureUrl(srcUrl);
var childFrame = document.createElement("iframe");
if (childFrame === null) {
throw new Error("Could not create new iframe");
}
childFrame.src = srcUrl;
childFrame.name = name;
childFrame.style.display = "none";
document.body.appendChild(childFrame);
return childFrame;
}
exports.createIframe = createIframe;
/**
* Creates a full-screen iframe with the given src and adds it to the page.
*
* @param srcUrl - The URL to use as src for the iframe.
* @param name - The name for the iframe element.
* @returns - The iframe element.
* @throws - Will throw if the iframe could not be created.
*/
function createFullScreenIframe(srcUrl, name) {
srcUrl = ensureUrl(srcUrl);
var childFrame = document.createElement("iframe");
if (childFrame === null) {
throw new Error("Could not create new iframe");
}
childFrame.src = srcUrl;
childFrame.name = name;
// Set properties to make the iframe full-screen.
childFrame.style.position = "fixed";
childFrame.style.top = "0";
childFrame.style.left = "0";
childFrame.style.bottom = "0";
childFrame.style.right = "0";
childFrame.style.width = "100%";
childFrame.style.height = "100%";
childFrame.style.border = "none";
childFrame.style.margin = "0";
childFrame.style.padding = "0";
childFrame.style.overflow = "hidden";
childFrame.style.zIndex = "999999";
document.body.appendChild(childFrame);
return childFrame;
}
exports.createFullScreenIframe = createFullScreenIframe;
/**
* Ensures that the given string is a URL.
*
* @param url - The given string.
* @returns - The URL.
*/
function ensureUrl(url) {
if (url.startsWith("http://")) {
return url;
}
return ensurePrefix(url, "https://");
}
exports.ensureUrl = ensureUrl;
/**
* Removes duplicate adjacent characters from the given string.
*
* @param str - The given string.
* @param char - The character to remove duplicates of.
* @returns - The string without duplicate adjacent characters.
*/
function removeAdjacentChars(str, char) {
var pathArray = Array.from(str);
for (var i = 0; i < pathArray.length - 1;) {
if (pathArray[i] === char && pathArray[i + 1] === char) {
pathArray.splice(i, 1);
}
else {
i++;
}
}
return pathArray.join("");
}
exports.removeAdjacentChars = removeAdjacentChars;
/**
* Removes a suffix from the end of the string.
*
* @param str - The string to process.
* @param suffix - The suffix to remove.
* @param [limit] - Maximum amount of times to trim. No limit by default.
* @returns - The processed string.
*/
function trimSuffix(str, suffix, limit) {
while (str.endsWith(suffix)) {
if (limit !== undefined && limit <= 0) {
break;
}
str = str.substring(0, str.length - suffix.length);
if (limit) {
limit -= 1;
}
}
return str;
}
exports.trimSuffix = trimSuffix;
/**
* Prepends the prefix to the given string only if the string does not already start with the prefix.
*
* @param str - The string.
* @param prefix - The prefix.
* @returns - The prefixed string.
*/
function ensurePrefix(str, prefix) {
if (!str.startsWith(prefix)) {
str = "".concat(prefix).concat(str);
}
return str;
}
exports.ensurePrefix = ensurePrefix;
//# sourceMappingURL=utils.js.map