Best JavaScript code snippet using wpt
inline-scripts.js
Source: inline-scripts.js
1/* global ComponentSandbox */2(() => {3 const { createMessageEventListener, isPlainObject, safeParse, toMessage } = ComponentSandbox;4 let LISTENER_ID = 0;5 let LISTENER_BUFFER = {};6 let LISTENER_DEREGS = {};7 let EVENT_BUFFER = [];8 let CALLBACK_ID = 0;9 let CALLBACK_BUFFER = {};10 let MESSAGE_PORT;11 let MESSAGE_EVENT_LISTENER;12 function subscribe(evt, cb, src) {13 if (!MESSAGE_EVENT_LISTENER) {14 // The message port hasn't been provided yet by the host => Create proxy listener and deregister handler15 const id = LISTENER_ID++;16 LISTENER_BUFFER[id] = { evt, cb, src };17 return () => {18 if (LISTENER_DEREGS && LISTENER_DEREGS[id]) {19 LISTENER_DEREGS[id]();20 delete LISTENER_DEREGS[id];21 }22 if (LISTENER_BUFFER && LISTENER_BUFFER[id]) {23 delete LISTENER_BUFFER[id];24 }25 };26 } else {27 // Register listener and return deregister handler28 return MESSAGE_EVENT_LISTENER(evt, cb, src);29 }30 }31 function emit(obj, cb) {32 if (!MESSAGE_PORT) {33 EVENT_BUFFER.push({ obj, cb });34 } else {35 const { message, transfer } = toMessage(obj);36 let callbackId;37 if (typeof cb === 'function') {38 // Generate a unique callback ID and store the callback in a buffer39 // Pass the callback ID to the recipient/host as part of the message40 callbackId = `c${Date.now()}${++CALLBACK_ID}`;41 CALLBACK_BUFFER[callbackId] = cb;42 }43 MESSAGE_PORT.postMessage(callbackId ? { ...message, callback: callbackId } : message, transfer);44 }45 }46 // Prevent modifications to global `listen`, `subscribe` and `emit`47 Object.defineProperty(window, 'listen', {48 configurable: false,49 writable: false,50 enumerable: false,51 value: subscribe52 });53 Object.defineProperty(window, 'subscribe', {54 configurable: false,55 writable: false,56 enumerable: false,57 value: subscribe58 });59 Object.defineProperty(window, 'emit', {60 configurable: false,61 writable: false,62 enumerable: false,63 value: emit64 });65 // Prevent modifications to global `ComponentSandbox`66 [isPlainObject, safeParse, toMessage, createMessageEventListener].forEach(fn => {67 Object.defineProperty(ComponentSandbox, fn.name, {68 configurable: false,69 writable: false70 });71 });72 Object.defineProperty(window, 'ComponentSandbox', {73 configurable: false,74 writable: false75 });76 function syncEventListener({ data, ports }) {77 const { type } = safeParse(data);78 if (type === 'SBX:SYN' && !MESSAGE_PORT && Array.isArray(ports) && ports[0]) {79 window.removeEventListener('message', syncEventListener, false);80 MESSAGE_PORT = ports[0];81 MESSAGE_EVENT_LISTENER = createMessageEventListener(MESSAGE_PORT);82 MESSAGE_PORT.start();83 // Send `SBX:ACK` event84 window.emit({ type: 'SBX:ACK' });85 // Flush listener buffer86 const keys = Object.keys(LISTENER_BUFFER); // Prevent use of `Object.values` here to shim-free keep support for IE1187 keys.forEach(key => {88 const { evt, cb, src } = LISTENER_BUFFER[key];89 LISTENER_DEREGS[key] = MESSAGE_EVENT_LISTENER(evt, cb, src);90 });91 // Flush event buffer92 EVENT_BUFFER.forEach(({ obj, cb }) => window.emit(obj, cb));93 // Remove buffers94 LISTENER_BUFFER = null;95 EVENT_BUFFER = null;96 }97 }98 function focusEventListener() {99 setTimeout(() => {100 window.emit({101 type: 'SBX:FOCUS',102 payload: {103 isDocumentElement: document.activeElement === document.documentElement104 }105 });106 });107 }108 function blurEventListener() {109 setTimeout(() => {110 window.emit({ type: 'SBX:BLUR' });111 });112 }113 window.addEventListener('message', syncEventListener, false);114 window.addEventListener('focus', focusEventListener, false);115 window.addEventListener('blur', blurEventListener, false);116 window.onerror = (msg, url, lineNo, columnNo, error) => {117 window.emit({ type: 'SBX:ERROR', payload: { msg, url, lineNo, columnNo, error } });118 return true;119 };120 Object.defineProperty(window, 'onerror', {121 configurable: false,122 writable: false123 });124 window.listen('SBX:ECHO', ({ type, payload, source, transfer }) => {125 window.emit({ type, payload, source, transfer });126 });127 window.listen('SBX:CBK', (payload, { callback, source, transfer }) => {128 if (typeof callback === 'string' && CALLBACK_BUFFER[callback]) {129 // Invoke a certain stored callback and remove it immediately after its invocation130 CALLBACK_BUFFER[callback](payload, { source, transfer });131 delete CALLBACK_BUFFER[callback];132 }133 });134 // Remove security relevant properties from the sandboxed `window` object135 function safeDelete(property) {136 try {137 delete window[property];138 } catch (e) {139 // Intentionally empty140 }141 }142 safeDelete('parent');143 safeDelete('top');144 safeDelete('frameElement');145 safeDelete('opener');...
message-target-shared-worker.js
Source: message-target-shared-worker.js
1'use strict';2importScripts(3 'test-helpers.js',4 'messaging-serialize-helpers.js',5 'message-target.js'6);7self.addEventListener('connect', connect_event => {8 const message_port = connect_event.ports[0];9 add_message_event_handlers(10 /*receiver=*/message_port, /*target=*/message_port);11 message_port.start();...
Using AI Code Generation
1test(function() {2 var message_port = new MessageChannel();3 var port1 = message_port.port1;4 var port2 = message_port.port2;5 port1.onmessage = function(e) {6 assert_equals(e.data, "Hello World", "Message received");7 assert_equals(e.ports.length, 1, "Event has one port");8 assert_equals(e.ports[0], port2, "Event has port2");9 port2.postMessage("Hello World");10 };11 port2.onmessage = function(e) {12 assert_equals(e.data, "Hello World", "Message received");13 assert_equals(e.ports.length, 0, "Event has no ports");14 };15 port1.postMessage("Hello World", [port2]);16}, "MessageChannel: MessagePort.postMessage with one port");17test(function() {18 var message_port = new MessageChannel();19 var port1 = message_port.port1;20 var port2 = message_port.port2;21 var port3 = message_port.port1;22 port1.onmessage = function(e) {23 assert_equals(e.data, "Hello World", "Message received");24 assert_equals(e.ports.length, 2, "Event has two ports");25 assert_equals(e.ports[0], port2, "Event has port2");26 assert_equals(e.ports[1], port3, "Event has port3");27 port2.postMessage("Hello World");28 port3.postMessage("Hello World");29 };30 port2.onmessage = function(e) {31 assert_equals(e.data, "Hello World", "Message received");32 assert_equals(e.ports.length, 0, "Event has no ports");33 };34 port3.onmessage = function(e) {35 assert_equals(e.data, "Hello World", "Message received");36 assert_equals(e.ports.length, 0, "Event has no ports");37 };38 port1.postMessage("Hello World", [port2, port3]);39}, "MessageChannel: MessagePort.postMessage with two ports");40test(function() {41 var message_port = new MessageChannel();42 var port1 = message_port.port1;43 var port2 = message_port.port2;44 port1.onmessage = function(e) {
Using AI Code Generation
1self.onmessage = function(msg) {2 postMessage(msg.data);3};4var worker = new Worker('test.js');5worker.onmessage = function(msg) {6 postMessage(msg.data);7};8worker.postMessage('hello');9self.addEventListener('message', function(e) {10 postMessage(e.data);11}, false);12var worker = new Worker('test.js');13worker.addEventListener('message', function(e) {14 postMessage(e.data);15}, false);16worker.postMessage('hello');17self.onmessage = function(msg) {18 postMessage(msg.data);19};20var worker = new Worker('test.js');21worker.onmessage = function(msg) {22 postMessage(msg.data);23};24worker.postMessage('hello');25self.addEventListener('message', function(e) {26 postMessage(e.data);27}, false);28var worker = new Worker('test.js');29worker.addEventListener('message', function(e) {30 postMessage(e.data);31}, false);32worker.postMessage('hello');33self.onmessage = function(msg) {34 postMessage(msg.data);35};36var worker = new Worker('test.js');37worker.onmessage = function(msg) {38 postMessage(msg.data);39};40worker.postMessage('hello');
Using AI Code Generation
1var message_port = new MessagePort();2message_port.onmessage = function(event) {3 test(function() {4 assert_equals(event.data, "Hello World!", "Message received");5 }, "Message received");6};7message_port.postMessage("Hello World!");
Using AI Code Generation
1var wpt = require('wpt');2var port = wpt.message_port();3port.onmessage = function(e) {4 console.log(e.data);5 port.postMessage('Hello from test.js');6};7var wpt = require('wpt');8var port = wpt.message_port();9port.onmessage = function(e) {10 console.log(e.data);11 port.postMessage('Hello from test2.js');12};13var wpt = require('wpt');14var port = wpt.message_port();15port.onmessage = function(e) {16 console.log(e.data);17 port.postMessage('Hello from test3.js');18};19var wpt = require('wpt');20var port = wpt.message_port();21port.onmessage = function(e) {22 console.log(e.data);23 port.postMessage('Hello from test4.js');24};25var wpt = require('wpt');26var port = wpt.message_port();27port.onmessage = function(e) {28 console.log(e.data);29 port.postMessage('Hello from test5.js');30};31var wpt = require('wpt');32var port = wpt.message_port();33port.onmessage = function(e) {34 console.log(e.data);35 port.postMessage('Hello from test6.js');36};37var wpt = require('wpt');38var port = wpt.message_port();39port.onmessage = function(e) {40 console.log(e.data);41 port.postMessage('Hello from test7.js');42};43var wpt = require('wpt');44var port = wpt.message_port();45port.onmessage = function(e) {46 console.log(e.data);47 port.postMessage('Hello from test8.js');48};49var wpt = require('wpt');50var port = wpt.message_port();
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4wpt.runTest(options, function(err, data) {5 if (err) {6 return console.log('Error: ' + err);7 }8 console.log('Test submitted successfully. View results at: ' + data.data.userUrl);9});10var http = require('http');11var util = require('util');12var EventEmitter = require('events').EventEmitter;13var WebPageTest = function(options) {14 this.options = options;15 this.messagePort = options.messagePort || 8888;16 this.messagePortServer = null;17 this.messagePortClients = [];18 this.messagePortClientsCount = 0;19 this.messagePortClientsData = {};20 this.messagePortClientsDataCount = 0;21 this.messagePortClientsDataLimit = 1000;22 this.messagePortClientsDataLimitReached = false;23 this.messagePortClientsDataLimitReachedMessage = 'Message port clients data limit reached.';24 this.messagePortClientsDataLimitReachedMessageSent = false;25 this.messagePortClientsDataLimitReachedMessageSentCount = 0;26 this.messagePortClientsDataLimitReachedMessageSentCountLimit = 1000;27 this.messagePortClientsDataLimitReachedMessageSentCountLimitReached = false;28 this.messagePortClientsDataLimitReachedMessageSentCountLimitReachedMessage = 'Message port clients data limit reached message sent count limit reached.';29};30util.inherits(WebPageTest, EventEmitter);31WebPageTest.prototype.runTest = function(options, callback) {32 var self = this;33 var httpOptions = {34 path: '/runtest.php?url=' + encodeURIComponent(options.url) + '&k=' + options.key +
Check out the latest blogs from LambdaTest on this topic:
Testing is a critical step in any web application development process. However, it can be an overwhelming task if you don’t have the right tools and expertise. A large percentage of websites still launch with errors that frustrate users and negatively affect the overall success of the site. When a website faces failure after launch, it costs time and money to fix.
We launched LT Browser in 2020, and we were overwhelmed by the response as it was awarded as the #5 product of the day on the ProductHunt platform. Today, after 74,585 downloads and 7,000 total test runs with an average of 100 test runs each day, the LT Browser has continued to help developers build responsive web designs in a jiffy.
Smartphones have changed the way humans interact with technology. Be it travel, fitness, lifestyle, video games, or even services, it’s all just a few touches away (quite literally so). We only need to look at the growing throngs of smartphone or tablet users vs. desktop users to grasp this reality.
As part of one of my consulting efforts, I worked with a mid-sized company that was looking to move toward a more agile manner of developing software. As with any shift in work style, there is some bewilderment and, for some, considerable anxiety. People are being challenged to leave their comfort zones and embrace a continuously changing, dynamic working environment. And, dare I say it, testing may be the most ‘disturbed’ of the software roles in agile development.
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!!