How to use newURLObject method in wpt

Best JavaScript code snippet using wpt

utils.ts

Source:utils.ts Github

copy

Full Screen

1import url from 'url';2import cookie from 'cookie';3import querystring from 'querystring';4import { locale, avaliableLanguages, Languages } from '../config';5/**6 * 解析描述或原因,转换 <, >, \n 等字符串7 * @param str 需要解析的文本8 */9export function parseDescription(str: string) {10 const language = getLanguage();11 const description = str.replace(/\</g, '&lt;').replace(/\>/g, '&gt;');12 if (language === 'zh-CN') {13 return description14 .replace(/[a-zA-Z0-9\(\)\[\]\{\}\\\/'"_\-\+\?\.\*!=\&\@\#$%~:; ]+/g, (codes) => {15 const matchSpaces = codes.match(/^( *)(.*?)( *)$/);16 if (matchSpaces === null) {17 return `<code>${codes}</code>`;18 }19 if (matchSpaces[2].startsWith('http')) {20 return `${matchSpaces[1]}<a href="${matchSpaces[2]}" target="_blank">${matchSpaces[2]}</a>${matchSpaces[3]}`;21 }22 return `${matchSpaces[1]}<code>${matchSpaces[2]}</code>${matchSpaces[3]}`;23 })24 .replace(/\n/g, '<br />')25 .replace(/禁止/g, '<strong style="color:#db5757; font-weight:600;">$&</strong>')26 .replace(/必须/g, '<strong style="color:#267fd9; font-weight:600;">$&</strong>');27 } else {28 let isOpen = false;29 return description30 .replace(/\n/g, '<br />')31 .split('')32 .map((letter) => {33 if (letter !== '`') {34 return letter;35 }36 isOpen = !isOpen;37 if (isOpen) {38 return '<code>';39 }40 return '</code>';41 })42 .join('');43 }44}45/** 翻译 */46export function t(str: string) {47 const language = getLanguage();48 return (locale as any)[language][str] || str;49}50/** 从 query 和 cookies 获取 language */51export function getLanguage(): Languages {52 let language: any;53 const queryLanguage = getQuery().language;54 if (Array.isArray(queryLanguage)) {55 language = queryLanguage[queryLanguage.length - 1];56 } else {57 language = queryLanguage;58 }59 // 非法的 queryLanguage60 if (!avaliableLanguages.includes(language)) {61 language = cookie.parse(document.cookie).language;62 // 非法的 cookieLanguage63 if (!avaliableLanguages.includes(language)) {64 language = avaliableLanguages[0];65 }66 }67 setCookie('language', language);68 return language;69}70export function getQuery() {71 return querystring.parse(location.search.replace(/^\?/, ''));72}73function setCookie(name: string, value: string, days = 365) {74 const cookieObject = cookie.parse(document.cookie);75 if (cookieObject[name] === value) {76 return;77 }78 let expires = '';79 if (days) {80 const date = new Date();81 date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);82 expires = '; expires=' + date.toUTCString();83 }84 document.cookie = name + '=' + (value || '') + expires + '; path=/';85}86interface UrlOptions {87 path?: string;88 query?: {89 [key: string]: string | boolean;90 };91}92/** 生成新的 url */93export function newUrl({ path, query }: UrlOptions) {94 const parsedUrl = url.parse(location.href, true);95 const newUrlObject = {96 pathname: parsedUrl.pathname || '',97 query: parsedUrl.query,98 };99 if (path) {100 newUrlObject.pathname = url.resolve(newUrlObject.pathname, path);101 }102 if (query) {103 Object.entries(query).forEach(([key, value]) => {104 if (value === false) {105 delete newUrlObject.query[key];106 } else if (value === true) {107 newUrlObject.query[key] = '1';108 } else {109 newUrlObject.query[key] = value;110 }111 });112 }113 // 排序 query114 const newQuery: querystring.ParsedUrlQuery = {};115 Object.entries(newUrlObject.query)116 .sort(([aKey], [bKey]) => aKey.localeCompare(bKey))117 .forEach(([key, value]) => {118 newQuery[key] = value;119 });120 newUrlObject.query = newQuery;121 return url.format(newUrlObject);122}123export function replaceUrl(url: string, title?: string) {124 history.replaceState({}, title || document.title, url);125}126/** 若为空则返回默认值,可提供取值范围 */127export function defaultTo<T>(source: any, defaultValue: T, avaliableValues?: T[]): T {128 if (typeof source === 'undefined') {129 return defaultValue;130 }131 if (source === null) {132 return defaultValue;133 }134 if (typeof source === 'number' && isNaN(source)) {135 return defaultValue;136 }137 if (typeof avaliableValues !== 'undefined') {138 if (!avaliableValues.includes(source)) {139 return defaultValue;140 }141 }142 return source;...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3 if (err) return console.error(err);4 console.log('Test submitted. Polling for results...');5 client.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log('Test completed. View results at ' + data.data.summary);8 });9});10var wpt = require('webpagetest');11var client = wpt('www.webpagetest.org');12 if (err) return console.error(err);13 console.log('Test submitted. Polling for results...');14 client.getTestResults(data.data.testId, function(err, data) {15 if (err) return console.error(err);16 console.log('Test completed. View results at ' + data.data.summary);17 });18});19var wpt = require('webpagetest');20var client = wpt('www.webpagetest.org');21 if (err) return console.error(err);22 console.log('Test submitted. Polling for results...');23 client.getTestResults(data.data.testId, function(err, data) {24 if (err) return console.error(err);25 console.log('Test completed. View results at ' + data.data.summary);26 });27});28var wpt = require('webpagetest');29var client = wpt('www.webpagetest.org');30 if (err) return console.error(err);31 console.log('Test submitted. Polling for results...');32 client.getTestResults(data.data.testId, function(err, data) {33 if (err) return console.error(err);34 console.log('Test completed. View results at ' + data.data.summary);35 });36});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.newURLObject(url, function(err, data) {4 console.log(data);5});6### WebPageTest(host, [port], [https])7### WebPageTest#runTest(url, [options], callback)

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var wp = wptools(url);3wp.get(function(err, info) {4 if (err) {5 console.log(err);6 } else {7 console.log(info);8 }9});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var server = new wpt('www.webpagetest.org');3var options = { location: 'Dulles:Chrome', firstViewOnly: true };4server.newURLObject(url, options, function(err, data) {5 if (err) {6 console.log('Error: ' + err);7 } else {8 console.log(data);9 }10});11MIT © [Tushar Singh](

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful