Best JavaScript code snippet using wpt
background-script.js
Source:background-script.js
1import MRPC from 'muxrpc'2import { pull } from 'pull-stream'3import { Buffer } from 'buffer'4browser.runtime.onMessage.addListener(notify);5function notify(message) {6 if (message.direction === "from-menu-script") {7 if (message.message.action === 'grant') {8 console.log('granting acces to', message.message.target);9 browser.storage.local.get({'granted': []}).then(({granted}) => {10 console.log('granted acces to', granted)11 Object.values(portConnections).filter(connection => !connection.connected).forEach((con) => {12 const relevantURL = con.port.sender.url.split('?')[0].split('#')[0]13 if (~granted.indexOf(relevantURL)) {14 connectPort(con.stream)15 portConnections[con.port.sender.tab.id].connected = true16 } else {17 console.log(con.stream)18 con.stream.abort()19 }20 })21 })22 }23 }24}25let portConnections = []26browser.runtime.onConnect.addListener(async function connected(port) {27 console.log('connection from', port.sender.url)28 const stream = createConnection(port)29 const relevantURL = port.sender.url.split('?')[0].split('#')[0]30 const { granted } = await browser.storage.local.get({'granted': []})31 if (~granted.indexOf(relevantURL)) {32 connectPort(stream) 33 portConnections[port.sender.tab.id] = { port, stream, connected: true }34 } else {35 portConnections[port.sender.tab.id] = { port, stream, connected: false }36 }37})38function connectPort(contentScriptStream) {39 //p.sender.tab.onClose(() => console.log('tab closed, should close connection'))40 41 const nativeScriptStream = createNativeConnection()42 pull(43 contentScriptStream,44 //logger('from content to native'),45 nativeScriptStream46 )47 pull(48 nativeScriptStream,49 //logger('from native to content'),50 contentScriptStream51 )52}53function createNativeConnection() {54 const port = browser.runtime.connectNative("scuttle_shell_browser")55 return createConnection(port)56}57function createConnection(port) {58 let messageDataCallback = null59 let messageDataBuffer = []60 61 const fromPort = function read(abort, cb) {62 if (messageDataBuffer.length > 0) {63 const data = messageDataBuffer[0]64 messageDataBuffer = messageDataBuffer.splice(1)65 cb(null, data)66 } else {67 messageDataCallback = cb68 }69 }70 port.onMessage.addListener(function(message) {71 const asBuffer = Buffer.from(message)72 if (messageDataCallback) {73 const _messageDataCallback = messageDataCallback74 messageDataCallback = null75 _messageDataCallback(null, asBuffer)76 } else {77 console.log('buffering....')78 messageDataBuffer.push(asBuffer)79 }80 });81 const toPort = function(read) {82 read(null, function more (end,data) {83 if (end) return84 port.postMessage(data);85 read(null, more)86 })87 }88 return {source: fromPort, sink: toPort}...
findLink.js
Source:findLink.js
1function getAnchorBefore(node) {2 return document.evaluate(3 "((./ancestor-or-self::a|./preceding::*)/@*[name()='class'])[last()]",4 node,5 null,6 XPathResult.STRING_TYPE7 ).stringValue;8}9function getDivBefore(node) {10 return document.evaluate(11 "((./ancestor-or-self::div|./preceding::*)/@*[name()='class'])[last()]",12 node,13 null,14 XPathResult.STRING_TYPE15 ).stringValue;16}17function onMessageListener(message, sender, sendResponse) {18 chrome.extension.onMessage.removeListener(onMessageListener);19 if (sender.id != chrome.runtime.id){20 console.log("Unexpected sender", sender, message);21 return;22 }23 if (message.messageId == "contextDetails") {24 var relevantUrl = message.frameUrl;25 if (!relevantUrl)26 relevantUrl = message.pageUrl;27 if (document.URL != relevantUrl)28 return;29 var selection = window.getSelection();30 if (!selection)31 return;32 if (message.pageUrl.includes("bestbuy"))33 result_url = getDivBefore(selection.getRangeAt(0).startContainer);34 else35 result_url = getAnchorBefore(selection.getRangeAt(0).startContainer);36 sendResponse(result_url);37 } else {38 console.log("Unexpected message", message);39 }40}...
menu-script.js
Source:menu-script.js
1const toggleSwitch = document.getElementById('toggle')2const locationArea = document.getElementById('location')3// this doesn't work: browser.tabs.getCurrent().then(...4browser.tabs.query({ "active": true, "currentWindow": true, "windowType": "normal" }).then(5 tabs => tabs[0]6).then(7 tab => {8 const relevantURL = tab.url.split('?')[0].split('#')[0]9 locationArea.innerText = relevantURL10 browser.storage.local.get({'granted': []}).then(result => {11 let granted = result.granted12 const enabled = !!~granted.indexOf(relevantURL)13 toggleSwitch.checked = enabled14 toggleSwitch.addEventListener('change', (e) => {15 if (e.target.checked) {16 granted.push(relevantURL)17 } else {18 granted = granted.filter(e => e !== relevantURL)19 }20 browser.storage.local.set({'granted': granted}).then(() => {21 return browser.runtime.sendMessage({22 direction: "from-menu-script",23 message: {24 action: e.target.checked ? 'grant' : 'revoke',25 target: relevantURL26 }27 })28 }).then(() => { window.setTimeout(window.close, 500) })29 })30 })31 },32 error => {33 console.log('error', error)34 }35)...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3wpt.getLocations(function(err, data) {4 if (err) return console.error(err);5 console.log(data);6});
Using AI Code Generation
1var wptHook = require('wptHook');2var url = wptHook.relevantUrl();3console.log(url);4var wptHook = require('wptHook');5var url = wptHook.relevantUrl();6console.log(url);7var wptHook = require('wptHook');8var url = wptHook.relevantUrl();9console.log(url);10var wptHook = require('wptHook');11var url = wptHook.relevantUrl();12console.log(url);13var wptHook = require('wptHook');14var url = wptHook.relevantUrl();15console.log(url);16var wptHook = require('wptHook');17var url = wptHook.relevantUrl();18console.log(url);19var wptHook = require('wptHook');20var url = wptHook.relevantUrl();21console.log(url);22var wptHook = require('wptHook');23var url = wptHook.relevantUrl();24console.log(url);25var wptHook = require('wptHook');26var url = wptHook.relevantUrl();27console.log(url);28var wptHook = require('wptHook');29var url = wptHook.relevantUrl();30console.log(url);31var wptHook = require('wptHook');32var url = wptHook.relevantUrl();33console.log(url);34var wptHook = require('wptHook');35var url = wptHook.relevantUrl();36console.log(url
Using AI Code Generation
1var wpt = require('webpagetest');2var webPageTest = new wpt('www.webpagetest.org');3var options = {4};5webPageTest.runTest('www.example.com', options, function(err, data) {6 if (err) return console.error(err);7 console.log('Test status: ' + data.statusText);8 console.log('Test ID: ' + data.data.testId);9 console.log('Test URL: ' + data.data.summary);10 console.log('Test URL: ' + data.data.userUrl);11 console.log('Test URL: ' + data.data.jsonUrl);12 console.log('Test URL: ' + data.data.xmlUrl);13 console.log('Test URL: ' + data.data.csvUrl);14 console.log('Test URL: ' + data.data.harUrl);15 console.log('Test URL: ' + data.data.pagespeedUrl);16 console.log('Test URL: ' + data.data.videoUrl);17});18var wpt = require('webpagetest');19var webPageTest = new wpt('www.webpagetest.org');20var options = {21};22webPageTest.runTest('www.example.com', options, function(err, data) {23 if (err) return console.error(err);24 console.log('Test status: ' + data.statusText);25 console.log('Test ID: ' + data.data.testId);26 console.log('Test URL: ' + data.data.summary);27 console.log('Test URL: ' + data.data.userUrl);28 console.log('Test URL: ' + data.data.jsonUrl);29 console.log('Test URL: ' + data.data.xmlUrl);30 console.log('Test URL: ' + data.data.csvUrl);31 console.log('Test URL: ' + data.data.harUrl);32 console.log('Test URL: ' + data.data.pagespeedUrl);
Using AI Code Generation
1var wptApi = require('wpt-api');2var wpt = new wptApi();3wpt.relevantUrl(url, function (err, data) {4 if (err) {5 console.log("Error: " + err);6 }7 else {8 console.log("Data: " + data);9 }10});11var wptApi = require('wpt-api');12var testId = "160225_1M_4a9a2f7e4c4a4d4e1c4e4e4a4c4a4e4e";13var wpt = new wptApi();14wpt.testStatus(testId, function (err, data) {15 if (err) {16 console.log("Error: " + err);17 }18 else {19 console.log("Data: " + data);20 }21});22var wptApi = require('wpt-api');23var testId = "160225_1M_4a9a2f7e4c4a4d4e1c4e4e4a4c4a4e4e";24var wpt = new wptApi();25wpt.testResults(testId, function (err, data) {26 if (err) {27 console.log("Error: " + err);28 }29 else {30 console.log("Data: " + data);31 }32});33var wptApi = require('wpt-api');34var testId = "160225_1M_4a9a2f7e4c4a4d4e1c4e4e4a4c4a4e4e";35var wpt = new wptApi();36wpt.testResults(testId, function (err, data) {37 if (err) {38 console.log("Error: " + err);39 }40 else {41 console.log("Data: " + data);42 }43});
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!!