UNPKG

codeschmiede-toolkit

Version:

A/B Test Development Toolkit

157 lines (126 loc) 3.46 kB
/** * @param {string} selector * @param {node} parent * @returns Node */ export function qs (selector, parent) { return (parent || document).querySelector(selector); } /** * @param {string} selector * @param {node} parent * @returns NodeList */ export function qsa (selector, parent) { return (parent || document).querySelectorAll(selector); } /** * @param {string|function} waitFor * @param {function} callback * @param {object} config */ export function waitFor (waitFor, callback, externalCfg = {}) { let cfg = { timeout: 10000, interval: 42, notFound: () => {}, }; Object.assign(cfg, externalCfg); const endTime = Number(new Date()) + cfg.timeout; if (typeof waitFor !== 'function') { const selector = waitFor; waitFor = () => { return qs(selector); } } const iteration = () => { const result = waitFor(); if(result) { callback(result); } else if(Number(new Date()) < endTime) { setTimeout(iteration, cfg.interval); } else { cfg.notFound(); } }; iteration(); } /** * @param {string|function} selector * @returns Promise */ export function waitForSync (selector) { return new Promise((resolve, reject) => { waitFor(selector, (response) => { resolve(response); }); }); } /** * @returns number */ export function getWidth () { return Math.max(document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth, document.documentElement.clientWidth); } /** * @returns boolean */ export function isMobile () { const userAgent = navigator.userAgent; return ('createTouch' in document) || screen.width <= 767 || userAgent.match(/(iPhone|iPod|iPad)/) || userAgent.match(/BlackBerry/) || userAgent.match(/Android/) || userAgent.match(/mobile/); } /** * @description https://www.w3schools.com/js/js_cookies.asp * @param {string} cname * @returns string */ export function getCookie (cname) { let name = cname + "="; let decodedCookie = decodeURIComponent(document.cookie); let ca = decodedCookie.split(';'); for(let i = 0; i <ca.length; i++) { let c = ca[i]; while (c.charAt(0) == ' ') { c = c.substring(1); } if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); } } return ""; } /** * @param {string} cname * @param {string} cvalue * @param {number} exdays */ export function setCookie (cname, cvalue, exdays) { const d = new Date(); d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); let expires = "expires=" + d.toUTCString(); document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } /** * @param {function} callback */ export function ready (callback) { if(document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") { callback(); } else { document.addEventListener("DOMContentLoaded", callback); } } /** * @param {node} element * @param {string} className */ export function addClass (element, className) { element.classList.add(className); } /** * @param {node} element * @param {string} className */ export function removeClass (element, className) { element.classList.remove(className); }