Best JavaScript code snippet using wpt
DomainAdd.js
Source:DomainAdd.js
1import { __, sprintf } from '@wordpress/i18n';2import React from 'react';3import Domains from '../API/Domains';4class DomainAdd extends React.Component {5 /**6 * Constructor.7 *8 * @param {Object} props9 */10 constructor( props ) {11 super( props );12 this.api = new Domains();13 this.state = {14 domain: {15 domain: '',16 is_primary: false,17 is_active: true,18 is_https: true,19 },20 };21 }22 /**23 * Helper method to make the AJAX call to add the domain to the database.24 */25 async addDomain() {26 const result = await this.api.add( this.state.domain );27 let message = '';28 if ( result.code ) {29 message = sprintf(30 /* translators: error message */31 __( 'Cannot add domain. %s', 'dark-matter' ),32 result.message33 );34 } else {35 message = sprintf(36 /* translators: added domain */37 __( '%s; has been added.', 'dark-matter' ),38 result.domain39 );40 }41 this.props.addNoticeAndRefresh(42 this.state.domain.domain,43 message,44 result.code ? 'error' : 'success'45 );46 if ( ! result.code ) {47 this.reset();48 }49 }50 /**51 * Handle the change event for each of the form elements.52 *53 * @param {Object} event Event Information.54 */55 handleChange = ( event ) => {56 const name = event.target.name;57 const value = event.target.value;58 const domain = { ...this.state.domain };59 domain[ name ] = value;60 this.setState( {61 domain,62 } );63 };64 /**65 * Handle the checkbox change for the protocol.66 *67 * @param {Object} event Event Information.68 */69 handleCheckboxChange = ( event ) => {70 const name = event.target.name;71 const domain = { ...this.state.domain };72 domain[ name ] = event.target.checked;73 this.setState( {74 domain,75 } );76 };77 /**78 * Handle the radio option change for the protocol.79 *80 * @param {Object} event Event Information.81 */82 handleProtocol = ( event ) => {83 const value = event.target.value;84 const domain = { ...this.state.domain };85 domain.is_https = 'https' === value;86 this.setState( {87 domain,88 } );89 };90 /**91 * Handle the form submission.92 *93 * @param {Object} event Event Information.94 */95 handleSubmit = ( event ) => {96 event.preventDefault();97 this.addDomain();98 };99 /**100 * Render the component.101 */102 render() {103 return (104 <form onSubmit={ this.handleSubmit }>105 <h2>Add Domain</h2>106 <table className="form-table">107 <tbody>108 <tr>109 <th scope="row">110 <label htmlFor="domain">111 { __( 'Domain', 'dark-matter' ) }112 </label>113 </th>114 <td>115 <input116 name="domain"117 type="text"118 onChange={ this.handleChange }119 value={ this.state.domain.domain }120 />121 </td>122 </tr>123 <tr>124 <th scope="row">125 <label htmlFor="is_primary">126 { __( 'Is Primary?', 'dark-matter' ) }127 </label>128 </th>129 <td>130 <input131 name="is_primary"132 type="checkbox"133 value="yes"134 onChange={ this.handleCheckboxChange }135 checked={ this.state.domain.is_primary }136 />137 </td>138 </tr>139 <tr>140 <th scope="row">141 <label htmlFor="is_active">142 { __( 'Is Active?', 'dark-matter' ) }143 </label>144 </th>145 <td>146 <input147 name="is_active"148 type="checkbox"149 value="yes"150 onChange={ this.handleCheckboxChange }151 checked={ this.state.domain.is_active }152 />153 </td>154 </tr>155 <tr>156 <th scope="row">{ __( 'Protocol', 'dark-matter' ) }</th>157 <td>158 <p>159 <input160 type="radio"161 name="is_https"162 id="protocol-http"163 value="http"164 onChange={ this.handleProtocol }165 checked={ ! this.state.domain.is_https }166 />167 <label htmlFor="protocol-http">168 { __( 'HTTP', 'dark-matter' ) }169 </label>170 </p>171 <p>172 <input173 type="radio"174 name="is_https"175 id="protocol-https"176 value="https"177 onChange={ this.handleProtocol }178 checked={ this.state.domain.is_https }179 />180 <label htmlFor="protocol-https">181 { __( 'HTTPS', 'dark-matter' ) }182 </label>183 </p>184 </td>185 </tr>186 </tbody>187 </table>188 <p className="submit">189 <button type="submit" className="button button-primary">190 { __( 'Add Domain', 'dark-matter' ) }191 </button>192 </p>193 </form>194 );195 }196 /**197 * Reset the form back to the default.198 */199 reset() {200 this.setState( {201 domain: {202 domain: '',203 is_primary: false,204 is_active: true,205 is_https: false,206 },207 } );208 }209}...
app.js
Source:app.js
1import Koa from 'koa'2import KoaBody from 'koa-body'3import serve from 'koa-static'4import KoaLogger from 'koa-logger'5import cors from 'koa-cors'6import path from 'path'7import fs from 'fs'8import render from 'koa-ejs'9import cookie from 'koa-cookie'10import session from 'koa-session'11import LRU from 'lru-cache'12import koa2Common from 'koa2-common'13import enforceHttps from 'koa-sslify'14import http from 'http'15import https from 'https'16import {17 SYSTEM18} from './config'19import {20 front,21 back22} from './routes'23const app = new Koa()24const env = process.env.NODE_ENV || 'production'25const IS_HTTPS = process.env.IS_HTTPS || 'FALSE'26// æå°æ¥å¿27app.on('error', (err, ctx) => {28 console.log(err)29});30render(app, {31 root: path.join(__dirname, 'view'),32 layout: 'template',33 viewExt: 'html',34 cache: LRU({35 max: 1000,36 maxAge: 1000 * 60 * 1537 }),38 debug: SYSTEM.DEBUG39});40 41//40442back.get('*', async(ctx,next)=>{43 let datas = {44 title:'404页é¢',45 }46 await ctx.render('404',{47 datas:datas48 });49})50let options=null;51if(IS_HTTPS === 'TRUE'){52 // Force HTTPS on all page 53 app.use(enforceHttps())54 options= {55 key: fs.readFileSync(path.resolve(__dirname, './assets/cert/214586773670023.key')),56 cert: fs.readFileSync(path.resolve(__dirname, './assets/cert/214586773670023.pem'))57 }58}59app60 .use(cookie())61 .use(session(app))62 .use(KoaBody({63 multipart: true,64 text:true,65 formidable: {66 uploadDir: path.join(__dirname, '/upload')67 }68 }))69 .use(serve(__dirname + "/assets",{70 maxage: env === 'development' ? 0 : 365 * 24 * 60 * 6071 }))72 .use(serve(__dirname + "/assets/js",{73 maxage: env === 'development' ? 0 : 365 * 24 * 60 * 6074 }))75 .use(koa2Common())76 .use(cors({77 origin: SYSTEM.ORIGIN,78 headers: 'Origin, X-Requested-With, Content-Type, Accept',79 methods: ['GET', 'PUT', 'POST'],80 credentials: true,81 }))82 .use(front.routes())83 .use(front.allowedMethods())84 .use(back.routes())85 .use(back.allowedMethods())86 .use((ctx, next) => {87 const start = new Date()88 return next().then(() => {89 const ms = new Date() - start90 console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)91 })92 })93// app.listen(SYSTEM.PROT);94if(IS_HTTPS==='FALSE')http.createServer(app.callback()).listen(SYSTEM.PROT);95if(IS_HTTPS==='TRUE')https.createServer(options, app.callback()).listen(SYSTEM.PROT);96console.log(`æå¡å¯å¨äºï¼è·¯å¾ä¸ºï¼127.0.0.1:${SYSTEM.PROT}`)...
client_api.js
Source:client_api.js
1// client_api2// 客æ·ç«¯è°ç¨çAPI, å
æ¬3// =========================================================4// get_api_server5// =========================================================6// =============================================================================7// import8// =============================================================================9// -----------------------------------------------------------------------------10// router11// -----------------------------------------------------------------------------12var express = require('express');13var router = express.Router();14// -----------------------------------------------------------------------------15// utils16// -----------------------------------------------------------------------------17var _ = require('underscore');18// -----------------------------------------------------------------------------19// API handler20// -----------------------------------------------------------------------------21var client_server = require('./client/server');22// -----------------------------------------------------------------------------23// config24// -----------------------------------------------------------------------------25var SERVER_CFG = require('../src/cfgs/server_cfg').SERVER_CFG;26// =============================================================================27// constant28// =============================================================================29var DEBUG = 0;30var ERROR = 1;31var TAG = "ãclient_apiã";32const img = 'http://thirdapp3.qlogo.cn/qzopenapp/b7f3014cca60d94df7f8636740b5836cf8a92a884e02b8f53466746062c86877/100';33// =============================================================================34// routes35// =============================================================================36/**37 * è·åæçç¥åé
çAPIæå¡å¨å°å.38 */39router.post('/get_api_server', function (req, res) {40 const FUNC = TAG + "/get_api_server --- ";41 if(DEBUG)console.log(FUNC + "CALL...");42 var headers = req.headers;43 var host = headers.host;44 var ip = host.split(":")[0];45 var port = host.split(":")[1];46 if(DEBUG)console.log(FUNC + "ip:", ip);47 if(DEBUG)console.log(FUNC + "port:", port);48 var is_https = SERVER_CFG.HTTPS_PORT == port;49 if(DEBUG)console.log(FUNC + "is_https:", is_https);50 client_server.getApiServer(req, res, is_https);51});...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.isHttps('www.google.com', function(err, data) {4 if (err) {5 console.log('Error: ' + err);6 } else {7 console.log(data);8 }9});10var wpt = require('webpagetest');11var wpt = new WebPageTest('www.webpagetest.org');12wpt.getLocations(function(err, data) {13 if (err) {14 console.log('Error: ' + err);15 } else {16 console.log(data);17 }18});
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5 if (err){6 console.log('Error: ' + err);7 }else{8 console.log('Test status: ' + data.statusText);9 console.log('Test ID: ' + data.data.testId);10 console.log('Test URL: ' + data.data.summary);11 console.log('Test results: ' + data.data.userUrl);12 }13});14var wpt = require('webpagetest');15var wpt = new WebPageTest('www.webpagetest.org');16var options = {17};18 if (err){19 console.log('Error: ' + err);20 }else{21 console.log('Test status: ' + data.statusText);22 console.log('Test ID: ' + data.data.testId);23 console.log('Test URL: ' + data.data.summary);24 console.log('Test results: ' + data.data.userUrl);25 }26});27var wpt = require('webpagetest');28var wpt = new WebPageTest('www.webpagetest.org');29var options = {30};31 if (err){32 console.log('Error: ' + err);33 }else{34 console.log('Test status: ' + data.statusText);35 console.log('Test
Using AI Code Generation
1const wpt = require('wpt');2const isHttps = wpt.IS_HTTPS;3console.log(isHttps);4const wpt = require('wpt');5const isHttps = wpt.IS_HTTPS;6console.log(isHttps);7const wpt = require('wpt');8const isHttps = wpt.IS_HTTPS;9console.log(isHttps);10const wpt = require('wpt');11const isHttps = wpt.IS_HTTPS;12console.log(isHttps);13const wpt = require('wpt');14const isHttps = wpt.IS_HTTPS;15console.log(isHttps);16const wpt = require('wpt');17const isHttps = wpt.IS_HTTPS;18console.log(isHttps);19const wpt = require('wpt');20const isHttps = wpt.IS_HTTPS;21console.log(isHttps);22const wpt = require('wpt');23const isHttps = wpt.IS_HTTPS;24console.log(isHttps);25const wpt = require('wpt');26const isHttps = wpt.IS_HTTPS;27console.log(isHttps);28const wpt = require('wpt');29const isHttps = wpt.IS_HTTPS;30console.log(isHttps);31const wpt = require('wpt');
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.6b5f6d5e6f5b6e5f6b5e6f5b6e5f6b5e');3wpt.isHttps(function(error, data) {4});5var wpt = require('wpt');6var wpt = new WebPageTest('www.webpagetest.org', 'A.6b5f6d5e6f5b6e5f6b5e6f5b6e5f6b5e');7wpt.getScripts(function(error, data) {8});9var wpt = require('wpt');10var wpt = new WebPageTest('www.webpagetest.org', 'A.6b5f6d5e6f5b6e5f6b5e6f5b6e5f6b5e');11wpt.getLocations(function(error, data) {12});13var wpt = require('wpt');14var wpt = new WebPageTest('www.webpagetest.org', 'A.6b5f6d5e6f5b6e5f6b5e6f5b6e5f6b5e');15wpt.getLocation('Dulles_MotoG4', function(error, data) {16});17var wpt = require('wpt');18var wpt = new WebPageTest('www.webpagetest.org', 'A.6b5f6d5e6f5b6e5f6b5e6f5b6e5f6b5e');19wpt.getWorkers(function(error
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!!