Best JavaScript code snippet using wpt
domParser.js
Source:domParser.js
1/*global document, DOMParser*/2(function (DOMParser) {3 "use strict";4 var5 DOMParser_proto = DOMParser.prototype,6 real_parseFromString = DOMParser_proto.parseFromString7 ;8 // Firefox/Opera/IE throw errors on unsupported types9 try {10 // WebKit returns null on unsupported types11 if ((new DOMParser).parseFromString("", "text/html")) {12 // text/html parsing is natively supported13 return;14 }15 } catch (ex) { }16 var createDocumentForOldBrowser = function(markup) {17 var doc = document.implementation.createHTMLDocument("");18 if (markup.toLowerCase().indexOf("<!doctype") > -1) {19 doc.documentElement.innerHTML = markup;20 }21 else {22 doc.body.innerHTML = markup;23 }24 return doc;25 };26 /** @const */27 var _Function_apply_ = Function.prototype.apply28 , _Array_slice_ = Array.prototype.slice29 /** Use native or unsafe but fast 'bind' for service and performance needs30 * @const31 * @param {Object} object32 * @param {...} var_args33 * @return {Function} */34 , _fastUnsafe_Function_bind_ = Function.prototype.bind || function(object, var_args) {35 var __method = this36 , args37 ;38 if( arguments.length > 1 ) {39 args = _Array_slice_.call(arguments, 1);40 return function () {41 return _Function_apply_.call(__method, object, args.concat(_Array_slice_.call(arguments)));42 };43 }44 return function () {45 return _Function_apply_.call(__method, object, arguments);46 };47 };48 function prepareTextForIFrame(text) {49 return text50 .replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '')//remove script tags from HTML text51 //TODO:: not remove all <script (.*?)>, just <script>, <script type="text/javascript">, <script type="">, <script type="text/vbscript">. Due <script> can contains a template52 .replace(/"/g, '\\"')53 ;54 }55 var createDocumentForOldIeBrowser = function (markup, type) {56 if (!type || type == "text/html" || /xml$/.test(type)) {57 markup = prepareTextForIFrame(markup);58 var iframe = document.createElement('iframe');59 iframe.style.display = 'none';60 iframe.src = 'javascript:document.write("' + markup + '")';61 document.body.appendChild(iframe);62 var newHTMLDocument = iframe.contentDocument || iframe.contentWindow.document;63 /* if (iframe.contentWindow) {64 newHTMLDocument["contentWindow"] = {};65 newHTMLDocument["contentWindow"]["document"] = newHTMLDocument;66 for (var k in iframe.contentWindow) {67 if (!newHTMLDocument["contentWindow"][k])68 newHTMLDocument["contentWindow"][k] = iframe.contentWindow[k];69 }70 // newHTMLDocument["contentWindow"]["document"]["documentElement"] = newHTMLDocument;71 }*/72 newHTMLDocument["__destroy__"] = _fastUnsafe_Function_bind_.call(function () {73 var _doc = this.contentWindow.document;74 _doc.documentElement.innerHTML = "";75 _doc["_"] = _doc.documentElement["_"] = null;76 /*TODO:: filter build-in properties suche as "URL", "location", etc77 Object.keys(_doc).forEach(function(key){78 try{79 _doc[key] = null;80 }81 catch(e){}82 })83 */84 document.body.removeChild(this);85 }, iframe);86 markup = iframe = null;87 //TODO::88 //shimDocument(newHTMLDocument);89 newHTMLDocument.getElementsByClassName = newHTMLDocument.getElementsByClassName || document.getElementsByClassName;90 newHTMLDocument.getElementsByTagName = newHTMLDocument.getElementsByTagName || document.getElementsByTagName;91 newHTMLDocument.getElementsByName = newHTMLDocument.getElementsByName || document.getElementsByName;92 newHTMLDocument.getElementById =newHTMLDocument.getElementById || document.getElementById;93 newHTMLDocument.querySelector = newHTMLDocument.querySelector || document.querySelector;94 newHTMLDocument.querySelectorAll = newHTMLDocument.querySelectorAll || document.querySelectorAll;95 if (document["find"]) newHTMLDocument["find"] = document["find"];96 if (document["findAll"]) newHTMLDocument["findAll"] = document["findAll"];97 return newHTMLDocument;98 }99 else {100 //Not supported101 return null;102 }103 };104 DOMParser_proto.parseFromString = function (markup, type) {105 if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {106 if (document.implementation && document.implementation.createHTMLDocument) {107 return createDocumentForOldBrowser(markup);108 } else {109 return createDocumentForOldIeBrowser(markup,type);110 }111 } else {112 return real_parseFromString.apply(this, arguments);113 }114 };...
script.js
Source:script.js
1"use strict";2let newHTMLDocument;3let demoURL;4let backingColor = `#ffffff`;5let title = `Choose a title`;6let paragraph = `Enter some paragraph text`;7compileHtmlDoc(); //do an initial compile of the default state for display purposes.8// updateBackgroundColor()9// Updates the page background color based on user input.10function updateBackgroundColor() {11 backingColor = $(`#color-box`).val();12 compileHtmlDoc();13}14// updateTitle()15// Updates the title based on user input.16function updateTitle() {17 title = $(`#title-box`).val();18 compileHtmlDoc();19}20// updateParagraph()21// Updates the paragraph copy based on user input.22function updateParagraph() {23 paragraph = $(`#copy-box`).val();24 compileHtmlDoc();25}26// compileHtmlDoc()27// Procedurally generates an HTML file based on the parameters the user has input.28function compileHtmlDoc() {29//clear the html30newHTMLDocument = ``;31//open the head tag + generic head contents32newHTMLDocument +=33`<!DOCTYPE html>34<html lang="en" dir="ltr">35 <head>36 <meta charset="utf-8">37 <meta name="viewport" width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0>38 <title>Demo-frame</title>`39//open the style tag40newHTMLDocument +=41 `<style>`42//insert our CSS43newHTMLDocument +=44`body {45 position: fixed;46 height: 600px;47 width: 400px;48 top: 0;49 left: 0;50 margin: 0px;51}52 #diagram-container {53 position: fixed;54 height: 600px;55 width: 400px;56 top: 0;57 left: 0;58 background-color: ${backingColor};59 }`60//close the style and body tags61newHTMLDocument +=62 `</style>63 </head>`64//open the body tag65newHTMLDocument +=66 `<body>67 <div id="diagram-container">`68//insert our body html69newHTMLDocument +=70 `<h1>${title}</h1>71 <p>${paragraph}72 </p>`73//close the body tag74newHTMLDocument +=75`</div>76 </body>77</html>`78//make a blob from our html, create a URL for it, and push it to the iframe79let htmlBlob = new Blob([newHTMLDocument], {type : 'text/html'});80demoURL = URL.createObjectURL(htmlBlob);81$(`iframe`).attr(`src`, demoURL);82}83// exportHtml()84// This takes the compiled HTML and downloads it on the client-side via a hidden link on the page.85function exportHtml() {86 $(`#download-link`).attr(`download`, `${title}.html`); //assign title to the file87 $(`#download-link`).attr(`href`, demoURL); //pass temporary URL to the link88 document.getElementById(`download-link`).click(); //simulate a click on the download link...
createTempHtml.js
Source:createTempHtml.js
1export function createTempHtml (data) {2 var newHTMLDocument = document.implementation.createHTMLDocument('temp');3 var el = newHTMLDocument.createElement('div')4 el.innerHTML = data;5 return el...
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3var url = 'www.google.com';4var options = { runs: 1, location: 'Dulles:Chrome', pollResults: 1, connectivity: 'Cable' };5api.runTest(url, options, function (err, data) {6 if (err) {7 console.error(err);8 } else {9 console.log(data);10 api.getTestResults(data.data.testId, function (err, data) {11 if (err) {12 console.error(err);13 } else {14 console.log(data);15 }16 });17 }18});19var wpt = require('webpagetest');20var api = new wpt('www.webpagetest.org');21var url = 'www.google.com';22var options = { runs: 1, location: 'Dulles:Chrome', pollResults: 1, connectivity: 'Cable' };23api.runTest(url, options, function (err, data) {24 if (err) {25 console.error(err);26 } else {27 console.log(data);28 api.getTestResults(data.data.testId, function (err, data) {29 if (err) {30 console.error(err);31 } else {32 console.log(data);33 }34 });35 }36});37var request = require('request');38 if (!error && response.statusCode == 200) {39 console.log(body);40 }41});
Using AI Code Generation
1function test() {2 var doc = document.implementation.createHTMLDocument("title");3 var p = doc.createElement("p");4 var t = doc.createTextNode("Hello World");5 p.appendChild(t);6 doc.body.appendChild(p);7 document.body.appendChild(doc);8}
Using AI Code Generation
1function createHTMLDocument() {2 var doc = document.implementation.createHTMLDocument("Title");3 var body = doc.body;4 var p = doc.createElement("p");5 var text = doc.createTextNode("Hello World");6 p.appendChild(text);7 body.appendChild(p);8 document.body.appendChild(body);9}
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2 until = webdriver.until;3var driver = new webdriver.Builder()4 .forBrowser('firefox')5 .build();6driver.findElement(By.id('runtest')).click();7driver.wait(until.titleIs('WebPageTest - Page Speed Testing'), 10000);8driver.quit();
Using AI Code Generation
1var wpt = require('webpagetest');2var options = { 3};4var wpt = new WebPageTest('www.webpagetest.org', options.key);5 if (err) return console.error(err);6 console.log('Test status:', data.statusText);7 if (data.statusCode == 200) {8 console.log('Test completed, view your test at %s', data.data.userUrl);9 }10});
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!!