Best JavaScript code snippet using wpt
head.js
Source:head.js
1/* Any copyright is dedicated to the Public Domain.2 http://creativecommons.org/publicdomain/zero/1.0/ */3let pendingEmulatorCmdCount = 0;4let Promise =5 SpecialPowers.Cu.import("resource://gre/modules/Promise.jsm").Promise;6let nfc = window.navigator.mozNfc;7SpecialPowers.addPermission("nfc-manager", true, document);8/**9 * Emulator helper.10 */11let emulator = (function() {12 let pendingCmdCount = 0;13 let originalRunEmulatorCmd = runEmulatorCmd;14 // Overwritten it so people could not call this function directly.15 runEmulatorCmd = function() {16 throw "Use emulator.run(cmd, callback) instead of runEmulatorCmd";17 };18 function run(cmd, callback) {19 log("Executing emulator command '" + cmd + "'");20 pendingCmdCount++;21 originalRunEmulatorCmd(cmd, function(result) {22 pendingCmdCount--;23 if (callback && typeof callback === "function") {24 callback(result);25 }26 });27 };28 function activateRE(re) {29 let deferred = Promise.defer();30 let cmd = 'nfc nci rf_intf_activated_ntf ' + re;31 this.run(cmd, function(result) {32 is(result.pop(), 'OK', 'check activation of RE' + re);33 deferred.resolve();34 });35 return deferred.promise;36 };37 function notifyDiscoverRE(re, type) {38 let deferred = Promise.defer();39 let cmd = 'nfc nci rf_discover_ntf ' + re + ' ' + type;40 this.run(cmd, function(result) {41 is(result.pop(), 'OK', 'check discovery of RE' + re);42 deferred.resolve();43 });44 return deferred.promise;45 };46 function setTagData(re, flag, tnf, type, payload) {47 let deferred = Promise.defer();48 let cmd = "nfc tag set " + re +49 " [" + flag + "," + tnf + "," + type + "," + payload + ",]";50 this.run(cmd, function(result) {51 is(result.pop(), "OK", "set NDEF data of tag" + re);52 deferred.resolve();53 });54 return deferred.promise;55 };56 function snepPutNdef(dsap, ssap, flags, tnf, type, payload, id) {57 let deferred = Promise.defer();58 let cmd = "nfc snep put " + dsap + " " + ssap + " [" + flags + "," +59 tnf + "," +60 type + "," +61 payload + "," +62 id + "]";63 this.run(cmd, function(result) {64 is(result.pop(), "OK", "send SNEP PUT");65 deferred.resolve();66 });67 return deferred.promise;68 };69 return {70 run: run,71 activateRE: activateRE,72 notifyDiscoverRE: notifyDiscoverRE,73 setTagData: setTagData,74 snepPutNdef: snepPutNdef75 };76}());77function toggleNFC(enabled) {78 let deferred = Promise.defer();79 let req;80 if (enabled) {81 req = nfc.startPoll();82 } else {83 req = nfc.powerOff();84 }85 req.onsuccess = function() {86 deferred.resolve();87 };88 req.onerror = function() {89 ok(false, 'operation failed, error ' + req.error.name);90 deferred.reject();91 finish();92 };93 return deferred.promise;94}95function clearPendingMessages(type) {96 if (!window.navigator.mozHasPendingMessage(type)) {97 return;98 }99 // setting a handler removes all messages from queue100 window.navigator.mozSetMessageHandler(type, function() {101 window.navigator.mozSetMessageHandler(type, null);102 });103}104function enableRE0() {105 let deferred = Promise.defer();106 let cmd = 'nfc nci rf_intf_activated_ntf 0';107 emulator.run(cmd, function(result) {108 is(result.pop(), 'OK', 'check activation of RE0');109 deferred.resolve();110 });111 return deferred.promise;112}113function cleanUp() {114 log('Cleaning up');115 waitFor(function() {116 SpecialPowers.removePermission("nfc-manager", document);117 finish()118 },119 function() {120 return pendingEmulatorCmdCount === 0;121 });122}123function runNextTest() {124 clearPendingMessages('nfc-manager-tech-discovered');125 clearPendingMessages('nfc-manager-tech-lost');126 let test = tests.shift();127 if (!test) {128 cleanUp();129 return;130 }131 test();132}133// run this function to start tests134function runTests() {135 if ('mozNfc' in window.navigator) {136 runNextTest();137 } else {138 // succeed immediately on systems without NFC139 log('Skipping test on system without NFC');140 ok(true, 'Skipping test on system without NFC');141 finish();142 }143}144const NDEF = {145 TNF_WELL_KNOWN: 1,146 // compares two NDEF messages147 compare: function(ndef1, ndef2) {148 isnot(ndef1, null, "LHS message is not null");149 isnot(ndef2, null, "RHS message is not null");150 is(ndef1.length, ndef2.length,151 "NDEF messages have the same number of records");152 ndef1.forEach(function(record1, index) {153 let record2 = this[index];154 is(record1.tnf, record2.tnf, "test for equal TNF fields");155 let fields = ["type", "id", "payload"];156 fields.forEach(function(value) {157 let field1 = record1[value];158 let field2 = record2[value];159 is(field1.length, field2.length,160 value + " fields have the same length");161 let eq = true;162 for (let i = 0; eq && i < field1.length; ++i) {163 eq = (field1[i] === field2[i]);164 }165 ok(eq, value + " fields contain the same data");166 });167 }, ndef2);168 },169 // parses an emulator output string into an NDEF message170 parseString: function(str) {171 // make it an object172 let arr = null;173 try {174 arr = JSON.parse(str);175 } catch (e) {176 ok(false, "Parser error: " + e.message);177 return null;178 }179 // and build NDEF array180 let ndef = arr.map(function(value) {181 let type = new Uint8Array(NfcUtils.fromUTF8(this.atob(value.type)));182 let id = new Uint8Array(NfcUtils.fromUTF8(this.atob(value.id)));183 let payload =184 new Uint8Array(NfcUtils.fromUTF8(this.atob(value.payload)));185 return new MozNDEFRecord(value.tnf, type, id, payload);186 }, window);187 return ndef;188 }189};190var NfcUtils = {191 fromUTF8: function(str) {192 let buf = new Uint8Array(str.length);193 for (let i = 0; i < str.length; ++i) {194 buf[i] = str.charCodeAt(i);195 }196 return buf;197 },198 toUTF8: function(array) {199 if (!array) {200 return null;201 }202 let str = "";203 for (var i = 0; i < array.length; i++) {204 str += String.fromCharCode(array[i]);205 }206 return str;207 }...
Using AI Code Generation
1var ndef = require('ndef');2var message = [ndef.textRecord("Hello, World!")];3var encodedMessage = ndef.encodeMessage(message);4console.log(encodedMessage);5var decodedMessage = ndef.decodeMessage(encodedMessage);6console.log(decodedMessage);
Using AI Code Generation
1var wptag = require('./wptag.js');2var ndef = require('./ndef.js');3var ndef2 = require('./ndef2.js');4var util = require('util');5var fs = require('fs');6var nfc = require('nfc').nfc;7var nfc2 = require('nfc').nfc2;8var nfc3 = require('nfc').nfc3;9var nfc4 = require('nfc').nfc4;10var nfc5 = require('nfc').nfc5;11var nfc6 = require('nfc').nfc6;12var nfc7 = require('nfc').nfc7;13var nfc8 = require('nfc').nfc8;14var nfc9 = require('nfc').nfc9;15var nfc10 = require('nfc').nfc10;16var nfc11 = require('nfc').nfc11;17var nfc12 = require('nfc').nfc12;18var nfc13 = require('nfc').nfc13;19var nfc14 = require('nfc').nfc14;20var nfc15 = require('nfc').nfc15;21var nfc16 = require('nfc').nfc16;22var nfc17 = require('nfc').nfc17;23var nfc18 = require('nfc').nfc18;24var nfc19 = require('nfc').nfc19;25var nfc20 = require('nfc').nfc20;26var nfc21 = require('nfc').nfc21;27var nfc22 = require('nfc').nfc22;28var nfc23 = require('nfc').nfc23;29var nfc24 = require('nfc').nfc24;30var nfc25 = require('nfc').nfc25;31var nfc26 = require('nfc').nfc26;32var nfc27 = require('nfc').nfc27;33var nfc28 = require('nfc').nfc28;34var nfc29 = require('nfc').nfc29;35var nfc30 = require('nfc').nfc30;36var nfc31 = require('nfc').nfc31;37var nfc32 = require('
Using AI Code Generation
1var ndef = require('ndef');2var util = require('util');3 ndef.textRecord("Hello, World")4];5var encoded = ndef.encodeMessage(message);6console.log(util.inspect(encoded, { showHidden: false, depth: null }));
Using AI Code Generation
1var ndef = require('ndef');2var util = require('util');3var fs = require('fs');4var ndef2 = require('ndef2');5var nfc = require('nfc').nfc;6var nfc = new nfc();7var nfc = require('nfc').nfc;8var nfc = new nfc();9var ndef2 = require('ndef2');10var ndef = require('ndef');11var util = require('util');12var fs = require('fs');13var ndef2 = require('ndef2');14var nfc = require('nfc').nfc;15var nfc = new nfc();16var nfc = require('nfc').nfc;17var nfc = new nfc();18var ndef2 = require('ndef2');19var ndef = require('ndef');20var util = require('util');21var fs = require('fs');22var ndef2 = require('ndef2');23var nfc = require('nfc').nfc;24var nfc = new nfc();25var nfc = require('nfc').nfc;26var nfc = new nfc();27var ndef2 = require('ndef2');28var ndef = require('ndef');29var util = require('util');30var fs = require('fs');31var ndef2 = require('ndef2');32var nfc = require('nfc').nfc;33var nfc = new nfc();34var nfc = require('nfc').nfc;35var nfc = new nfc();36var ndef2 = require('ndef2');37var ndef = require('ndef');38var util = require('util');39var fs = require('fs');40var ndef2 = require('ndef2');41var nfc = require('nfc').nfc;42var nfc = new nfc();43var nfc = require('nfc').nfc;44var nfc = new nfc();45var ndef2 = require('ndef2');46var ndef = require('ndef');47var util = require('util');48var fs = require('fs');49var ndef2 = require('ndef2');50var nfc = require('nfc').nfc;51var nfc = new nfc();52var nfc = require('nfc').nfc;53var nfc = new nfc();54var ndef2 = require('ndef2');
Using AI Code Generation
1var ndef = require('wpt-ndef');2var nfc = require('nfc').nfc;3var nfc = new nfc();4nfc.on('read', function(tag) {5 console.log(ndef.decodeMessage(tag.ndefMessage));6});7nfc.start();8var ndef = require('wpt-ndef');9var nfc = require('nfc').nfc;10var nfc = new nfc();11nfc.on('read', function(tag) {12 console.log(ndef.decodeMessage(tag.ndefMessage));13});14nfc.start();15var ndef = require('wpt-ndef');16var nfc = require('nfc').nfc;17var nfc = new nfc();18nfc.on('read', function(tag) {19 console.log(ndef.decodeMessage(tag.ndefMessage));20});21nfc.start();22var ndef = require('wpt-ndef');23var nfc = require('nfc').nfc;24var nfc = new nfc();25nfc.on('read', function(tag) {26 console.log(ndef.decodeMessage(tag.ndefMessage));27});28nfc.start();29var ndef = require('wpt-ndef');30var nfc = require('nfc').nfc;31var nfc = new nfc();32nfc.on('read', function(tag) {33 console.log(ndef.decodeMessage(tag.ndefMessage));34});35nfc.start();36var ndef = require('wpt-ndef');37var nfc = require('nfc').nfc;38var nfc = new nfc();39nfc.on('read
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!!