Best JavaScript code snippet using wpt
main.js
Source:main.js
1/* Const variables */2CONST_DELTA_MS = 100;3CONST_HOUR_MS = 1000 * 60 * 60;4CONST_MIN_MS = 1000 * 60;5CONST_SEC_MS = 1000;6CONST_PRESSED_MS = 1000;7CONST_PRESSING_CHECK_MS = 300;8CONST_KEY_SPACE = " ";9CONST_KEY_R_LOWER = "r";10CONST_KEY_R_UPPER = "R";11/* DOM */12const DOM = {13 body: document.body,14 button: document.getElementById("timerButton"),15 resetIndicator: document.getElementById("timerFillRed"),16 h: document.getElementById("timerTextHour"),17 m: document.getElementById("timerTextMinute"),18 s: document.getElementById("timerTextSecond"),19 darkModeButton: document.getElementById("darkModeButton")20};21/* Status */22const status = {23 isOn: true,24 isBeingPressed: false,25 isPressed: false26}27status.currentTheme = localStorage.getItem("theme");28status.preferDarkScheme = window.matchMedia("(prefers-color-scheme: dark)");29if (status.currentTheme == "dark")30 DOM.body.classList.toggle("dark-theme");31else if (status.currentTheme == "light")32 DOM.body.classList.toggle("light-theme");33/* Time */34const data = {35 ms: 0,36 msPressed: 037};38/* Text */39const text = {40 h: "00",41 m: "00",42 s: "00"43};44/* Event Function */45let mouseDownInterval;46const eventFunc = {47 increase() {48 data.ms += CONST_DELTA_MS;49 },50 reset() {51 data.ms = 0;52 },53 getText() {54 const returnText = value => {55 if (value < 10)56 return "0" + value;57 else58 return String(value);59 }60 const h = parseInt(data.ms / CONST_HOUR_MS);61 text.h = returnText(h);62 63 const m = parseInt(data.ms % CONST_HOUR_MS / CONST_MIN_MS);64 text.m = returnText(m);65 66 const s = parseInt(data.ms % CONST_MIN_MS / CONST_SEC_MS);67 text.s = returnText(s);68 },69 showText() {70 DOM.h.innerText = text.h;71 DOM.m.innerText = text.m;72 DOM.s.innerText = text.s;73 document.title = `${text.h} : ${text.m} : ${text.s}`;74 },75 interval() {76 if (status.isOn)77 {78 eventFunc.increase();79 eventFunc.getText();80 eventFunc.showText();81 }82 },83 toggleStatus() {84 if (!status.isBeingPressed)85 {86 status.isOn = !status.isOn;87 DOM.body.classList.toggle("stop");88 }89 status.isBeingPressed = false;90 },91 toggleTheme() {92 let theme;93 if (status.preferDarkScheme.matches) {94 DOM.body.classList.toggle("light-theme");95 theme = DOM.body.classList.contains("light-theme") ? "light" : "dark";96 } else {97 DOM.body.classList.toggle("dark-theme");98 theme = DOM.body.classList.contains("dark-theme") ? "dark" : "light";99 }100 localStorage.setItem("theme", theme);101 },102 increasePress() {103 if (data.msPressed <= CONST_PRESSED_MS)104 data.msPressed += CONST_DELTA_MS;105 else106 data.msPressed = CONST_PRESSED_MS;107 DOM.resetIndicator.style.opacity = data.msPressed / CONST_PRESSED_MS;108 if (data.msPressed >= CONST_PRESSING_CHECK_MS)109 status.isBeingPressed = true;110 if (data.msPressed == CONST_PRESSED_MS)111 status.isPressed = true;112 },113 resetPress() {114 data.msPressed = 0;115 DOM.resetIndicator.style.opacity = 0;116 },117 mouseDown() {118 mouseDownInterval = setInterval(eventFunc.increasePress, CONST_DELTA_MS);119 },120 mouseUp() {121 clearInterval(mouseDownInterval);122 123 if (status.isPressed)124 {125 eventFunc.reset();126 eventFunc.getText();127 eventFunc.showText();128 status.isPressed = false;129 }130 eventFunc.resetPress();131 },132 keyDownR() {133 clearInterval(mouseDownInterval);134 eventFunc.reset();135 eventFunc.getText();136 eventFunc.showText();137 status.isPressed = false;138 eventFunc.resetPress();139 }140}141/* Initialize */142setInterval(eventFunc.interval, CONST_DELTA_MS);143DOM.button.addEventListener("click", eventFunc.toggleStatus, true);144DOM.darkModeButton.addEventListener("click", eventFunc.toggleTheme, true);145if (window.PointerEvent) {146 DOM.button.addEventListener("pointerdown", eventFunc.mouseDown, true);147 DOM.body.addEventListener("pointerup", event => {148 event.preventDefault();149 eventFunc.mouseUp();150 }, true);151 DOM.body.addEventListener("pointercancel", event => {152 event.preventDefault();153 eventFunc.mouseUp();154 }, true);155} else {156 DOM.button.addEventListener("touchstart", eventFunc.mouseDown);157 DOM.body.addEventListener("touchend", event => {158 event.preventDefault();159 eventFunc.mouseUp();160 }, true);161 DOM.body.addEventListener("touchcancel", event => {162 event.preventDefault();163 eventFunc.mouseUp();164 }, true);165 DOM.button.addEventListener("mousedown", eventFunc.mouseDown, true);166 DOM.body.addEventListener("mouseup", eventFunc.mouseUp, true);167 DOM.body.addEventListener("mouseleave", eventFunc.mouseUp, true);168}169window.addEventListener("keydown", event => {170 if (event.key == CONST_KEY_SPACE)171 eventFunc.toggleStatus();172 if (event.key == CONST_KEY_R_UPPER || event.key == CONST_KEY_R_LOWER)173 eventFunc.keyDownR();...
_createElementUtils.ts
Source:_createElementUtils.ts
1/**2 * Component A simple, ReactJS inspired library to create dynamic components within static sites easier3 *4 * @license MIT5 * @author Luke Zhang luke-zhang-04.github.io6 * @file share Functions and types for createElement and it's variants7 * @copyright Copyright (C) 2020 - 2021 Luke Zhang8 */9import type {Component} from "../component"10import type {Ref} from "../createRef"11import url from "./_url"12/* eslint-disable one-var, @typescript-eslint/no-explicit-any */13export type ChildrenFlatArrayType = (14 | HTMLElement15 | Element16 | number17 | string18 | Component<any, any>19)[]20export type ChildrenArrayType = ChildrenFlatArrayType | ChildrenArrayType[]21/** All types the children parameter can be */22export type ChildrenType =23 | ChildrenType[]24 | string25 | number26 | ChildrenArrayType27 | Node28 | Component<any, any>29interface EventMap extends HTMLElementEventMap {30 "": Event31}32export type EventFunc<T extends keyof EventMap = ""> = (e: EventMap[T]) => void33export interface BasicProps {34 // eslint-disable-next-line35 [key: string]: string | number | Element | Ref | EventFunc<keyof EventFunc> | undefined36 class?: string37 ref?: Ref38 id?: string39 src?: string40 href?: string41 width?: number42 height?: number43 alt?: string44 style?: string45 title?: string46 onFocus?: EventFunc<"focus">47 onBlur?: EventFunc<"blur">48 onFocusIn?: EventFunc<"focusin">49 onFocusOut?: EventFunc<"focusout">50 onAnimationStart?: EventFunc<"animationstart">51 onAnimationCancel?: EventFunc<"animationcancel">52 onAnimationEnd?: EventFunc<"animationend">53 onAnimationIteration?: EventFunc<"animationiteration">54 onTransitionStart?: EventFunc<"transitionstart">55 onTransitionCancel?: EventFunc<"transitioncancel">56 onTransitionEnd?: EventFunc<"transitionend">57 onTransitionRun?: EventFunc<"transitionrun">58 onAuxClick?: EventFunc<"auxclick">59 onClick?: EventFunc<"click">60 onDblClick?: EventFunc<"dblclick">61 onMouseDown?: EventFunc<"mousedown">62 onMouseEnter?: EventFunc<"mouseenter">63 onMouseLeave?: EventFunc<"mouseleave">64 onMouseMove?: EventFunc<"mousemove">65 onMouseOver?: EventFunc<"mouseover">66 onMouseOut?: EventFunc<"mouseout">67 onMouseUp?: EventFunc<"mouseup">68}69/**70 * Binds children to element71 *72 * @param element - Element to bind73 * @param props - Props to bind with74 * @param ns - If namespace element75 * @returns Void76 * @package77 */78export const bindProps = (element: Element, props?: BasicProps | null, ns = false): void => {79 if (props) {80 for (const [key, val] of Object.entries(props)) {81 if (typeof val === "string" || typeof val === "number") {82 if (key === "innerHTML") {83 element.innerHTML = val.toString()84 } else if (ns) {85 element.setAttributeNS(null, key, val.toString())86 } else {87 element.setAttribute(key, val.toString())88 }89 } else if (key.slice(0, 2) === "on") {90 // Works such as onClick, onAnimationEnd, etc.91 if (typeof val === "function") {92 element.addEventListener(93 key.slice(2).toLowerCase() as keyof EventMap,94 val as EventFunc,95 )96 }97 } else if (key === "ref" && typeof val === "object" && "current" in val) {98 ;(val as Ref<Element>).current = element99 } else if (val !== undefined) {100 console.warn(`${typeof val} is not a valid DeStagnate child`)101 }102 }103 }104}105const bindDestagnateElement = (element: Node, children: Component): void => {106 if (element instanceof HTMLElement) {107 if (children.didMount) {108 if (children.parent !== element) {109 children.parent = element as HTMLElement110 }111 children.forceUpdate()112 } else {113 children.mount(element)114 }115 } else {116 throw new Error(`ERROR: code 3. See ${url}`)117 }118}119/**120 * Binds children to element121 *122 * @param element - Element to bind123 * @param children - Children to bind with124 * @returns Void125 * @package126 */127export const bindChildren = (element: Node, children?: ChildrenType): void => {128 if (children !== null && children !== undefined) {129 if (children instanceof Array) {130 children.forEach((child: ChildrenType) => bindChildren(element, child))131 } else if (typeof children === "string" || typeof children === "number") {132 element.appendChild(document.createTextNode(children.toString()))133 } else if (children instanceof Node) {134 element.appendChild(children)135 } else {136 bindDestagnateElement(element, children)137 }138 }...
EventManager.ts
Source:EventManager.ts
1import{EventEnum,EventFunc,EventData,EventDataOne,EventDataTwo,EventDataThird} from './EventEnum';2export default class EventManager{3 private static eventDic:Array<EventFunc>[] = [];4 //æ´¾åäºä»¶5 public static DispatchEvent(type:EventEnum,data?:EventData){6 let allEvent:Array<EventFunc> = this.eventDic[type];7 let listener:Function;8 let thisObj:any;9 if(allEvent){10 for(let i = 0;i < allEvent.length;i++){11 let msg:EventFunc = allEvent[i];12 listener = msg.listener;13 thisObj = msg.thisObj;14 if(data){15 listener.apply(thisObj,[data]);16 }17 else{18 listener.apply(thisObj);19 }20 }21 }22 else{23 console.log("äºä»¶IDä¸åå¨!,",type);24 }25 }26 //æ·»å çå¬äºä»¶27 public static AddEventListener(type:EventEnum,call:Function,thisObj:any){28 let allEvent:Array<EventFunc> = this.eventDic[type];29 if(allEvent){30 for(let i = 0;i < allEvent.length;i++){31 let msg:EventFunc = allEvent[i];32 if(msg.listener == call&&msg.thisObj == thisObj){33 console.log("äºä»¶éå¤æ³¨å!",type);34 return;35 }36 }37 }38 else{39 this.eventDic[type] = [];40 }41 let msgTemp:EventFunc = <EventFunc>{};42 msgTemp.listener = call;43 msgTemp.thisObj = thisObj;44 this.eventDic[type].push(msgTemp);45 }46 //移é¤äºä»¶47 public static RemoveEventListener(type:EventEnum,call:Function,thisObj:any){48 let allEvent:Array<EventFunc> = this.eventDic[type];49 if(allEvent!=null){50 for(let i = 0;i<allEvent.length;i++){51 if(allEvent[i].listener == call&&allEvent[i].thisObj == thisObj){52 this.eventDic[type].splice(i,1);53 }54 }55 }56 }57 //移é¤è¯¥å¯¹è±¡ææäºä»¶58 public static RemoveAllEventListener(thisObj:any){59 for(let i = 0;i<this.eventDic.length;i++){60 for(let j = 0;j<this.eventDic[i].length;j++){61 if(this.eventDic[i][j].thisObj == thisObj){62 this.eventDic[i].splice(j,1);63 }64 }65 }66 }...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.runTest(url, function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 }9});
Using AI Code Generation
1var wpt = require('wpt');2var test = new wpt();3 console.log(data);4});5var wpt = require('wpt');6var test = new wpt();7 console.log(data);8});9var wpt = require('wpt');10var test = new wpt();11 console.log(data);12});13var wpt = require('wpt');14var test = new wpt();15 console.log(data);16});17var wpt = require('wpt');18var test = new wpt();19 console.log(data);20});
Using AI Code Generation
1wpt.eventFunc("test.js", "testEvent", "testEventFunc");2wpt.eventFunc("test.js", "testEvent", "testEventFunc");3wpt.eventFunc("test.js", "testEvent", "testEventFunc");4wpt.eventFunc("test.js", "testEvent", "testEventFunc");5wpt.eventFunc("test.js", "testEvent", "testEventFunc");6wpt.eventFunc("test.js", "testEvent", "testEventFunc");7wpt.eventFunc("test.js", "testEvent", "testEventFunc");8wpt.eventFunc("test.js", "testEvent", "testEventFunc");9wpt.eventFunc("test.js", "testEvent", "testEventFunc");10wpt.eventFunc("test.js", "testEvent", "testEventFunc");11wpt.eventFunc("test.js", "testEvent", "testEventFunc");12wpt.eventFunc("test.js", "testEvent", "testEventFunc");13wpt.eventFunc("test.js", "testEvent", "testEventFunc");14wpt.eventFunc("test.js", "testEvent", "testEventFunc");15wpt.eventFunc("test.js
Using AI Code Generation
1var wpt = require('wpt');2wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');3var wpt = require('wpt');4wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');5var wpt = require('wpt');6wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');7var wpt = require('wpt');8wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');9var wpt = require('wpt');10wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');11var wpt = require('wpt');12wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');13var wpt = require('wpt');14wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');15var wpt = require('wpt');16wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');17var wpt = require('wpt');18wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');19var wpt = require('wpt');20wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');21var wpt = require('wpt');22wpt.eventFunc('eventFunctionName','eventFunctionName', 'eventFunctionName');
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new wpt('API_KEY');3wpt.eventFunc('test', 'TEST', 'TEST', 'TEST', function(err) {4 if (err) {5 console.log('error: ' + err);6 } else {7 console.log('eventFunc successful');8 }9});10var wpt = require('wpt');11var wpt = new wpt('API_KEY');12wpt.eventFunc('test', 'TEST', 'TEST', 'TEST', function(err) {13 if (err) {14 console.log('error: ' + err);15 } else {16 console.log('eventFunc successful');17 }18});19var wpt = require('wpt');20var wpt = new wpt('API_KEY');21wpt.eventFunc('test', 'TEST', 'TEST', 'TEST', function(err) {22 if (err) {23 console.log('error: ' + err);24 } else {25 console.log('eventFunc successful');26 }27});28var wpt = require('wpt');29var wpt = new wpt('API_KEY');30wpt.eventFunc('test', 'TEST', 'TEST', 'TEST', function(err) {31 if (err) {32 console.log('error: ' + err);33 } else {34 console.log('eventFunc successful');35 }36});37var wpt = require('wpt');38var wpt = new wpt('API_KEY');39wpt.eventFunc('test', 'TEST', 'TEST', 'TEST', function(err) {40 if (err) {41 console.log('error: ' + err);42 } else {43 console.log('eventFunc successful');44 }45});46var wpt = require('wpt');47var wpt = new wpt('API_KEY');48wpt.eventFunc('test', 'TEST', 'TEST', 'TEST', function(err) {49 if (err) {50 console.log('error:
Using AI Code Generation
1wpt.eventFunc('onLoad', 'wptFunc');2wptFunc = function() {3 var wpt = window.wpt;4 var wptEvent = wpt.event;5 var wptEventName = wpt.eventName;6 var wptEventTime = wpt.eventTime;7 var wptEventURL = wpt.eventURL;8 var wptEventLabel = wpt.eventLabel;9 var wptEventValue = wpt.eventValue;10 wpt.log('onLoad fired');11};12wpt.eventFunc('onLoad', 'wptFunc');13wptFunc = function() {14 var wpt = window.wpt;15 var wptEvent = wpt.event;16 var wptEventName = wpt.eventName;17 var wptEventTime = wpt.eventTime;18 var wptEventURL = wpt.eventURL;19 var wptEventLabel = wpt.eventLabel;20 var wptEventValue = wpt.eventValue;21 wpt.log('onLoad fired');22};
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!