Best JavaScript code snippet using wpt
base64.js
Source:base64.js
1/**2* UTF16åUTF8转æ¢å¯¹ç
§è¡¨3* U+00000000 â U+0000007F 0xxxxxxx4* U+00000080 â U+000007FF 110xxxxx 10xxxxxx5* U+00000800 â U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx6* U+00010000 â U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx7* U+00200000 â U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx8* U+04000000 â U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx9*/10var Base64 = {11 // 转ç 表12 table : [13 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',14 'I', 'J', 'K', 'L', 'M', 'N', 'O' ,'P',15 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',16 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',17 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',18 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',19 'w', 'x', 'y', 'z', '0', '1', '2', '3',20 '4', '5', '6', '7', '8', '9', '+', '/'21 ],22 UTF16ToUTF8 : function(str) {23 var res = [], len = str.length;24 for (var i = 0; i < len; i++) {25 var code = str.charCodeAt(i);26 if (code > 0x0000 && code <= 0x007F) {27 // ååèï¼è¿é并ä¸èè0x0000ï¼å 为å®æ¯ç©ºåè28 // U+00000000 â U+0000007F 0xxxxxxx29 res.push(str.charAt(i));30 } else if (code >= 0x0080 && code <= 0x07FF) {31 // ååè32 // U+00000080 â U+000007FF 110xxxxx 10xxxxxx33 // 110xxxxx34 var byte1 = 0xC0 | ((code >> 6) & 0x1F);35 // 10xxxxxx36 var byte2 = 0x80 | (code & 0x3F);37 res.push(38 String.fromCharCode(byte1), 39 String.fromCharCode(byte2)40 );41 } else if (code >= 0x0800 && code <= 0xFFFF) {42 // ä¸åè43 // U+00000800 â U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx44 // 1110xxxx45 var byte1 = 0xE0 | ((code >> 12) & 0x0F);46 // 10xxxxxx47 var byte2 = 0x80 | ((code >> 6) & 0x3F);48 // 10xxxxxx49 var byte3 = 0x80 | (code & 0x3F);50 res.push(51 String.fromCharCode(byte1), 52 String.fromCharCode(byte2), 53 String.fromCharCode(byte3)54 );55 } else if (code >= 0x00010000 && code <= 0x001FFFFF) {56 // ååè57 // U+00010000 â U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx58 } else if (code >= 0x00200000 && code <= 0x03FFFFFF) {59 // äºåè60 // U+00200000 â U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx61 } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ {62 // å
åè63 // U+04000000 â U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx64 }65 }66 67 return res.join('');68 },69 UTF8ToUTF16 : function(str) {70 var res = [], len = str.length;71 var i = 0;72 for (var i = 0; i < len; i++) {73 var code = str.charCodeAt(i);74 // 对第ä¸ä¸ªåèè¿è¡å¤æ75 if (((code >> 7) & 0xFF) == 0x0) {76 // ååè77 // 0xxxxxxx78 res.push(str.charAt(i));79 } else if (((code >> 5) & 0xFF) == 0x6) {80 // ååè81 // 110xxxxx 10xxxxxx82 var code2 = str.charCodeAt(++i);83 var byte1 = (code & 0x1F) << 6;84 var byte2 = code2 & 0x3F;85 var utf16 = byte1 | byte2;86 res.push(String.fromCharCode(utf16));87 } else if (((code >> 4) & 0xFF) == 0xE) {88 // ä¸åè89 // 1110xxxx 10xxxxxx 10xxxxxx90 var code2 = str.charCodeAt(++i);91 var code3 = str.charCodeAt(++i);92 var byte1 = (code << 4) | ((code2 >> 2) & 0x0F);93 var byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F);94 utf16 = ((byte1 & 0x00FF) << 8) | byte295 res.push(String.fromCharCode(utf16));96 } else if (((code >> 3) & 0xFF) == 0x1E) {97 // ååè98 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx99 } else if (((code >> 2) & 0xFF) == 0x3E) {100 // äºåè101 // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx102 } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ {103 // å
åè104 // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx105 }106 }107 108 return res.join('');109 },110 encode : function(str) {111 if (!str) {112 return '';113 }114 var utf8 = this.UTF16ToUTF8(str); // 转æUTF8115 var i = 0; // éåç´¢å¼116 var len = utf8.length;117 var res = [];118 while (i < len) {119 var c1 = utf8.charCodeAt(i++) & 0xFF;120 res.push(this.table[c1 >> 2]);121 // éè¦è¡¥2个=122 if (i == len) {123 res.push(this.table[(c1 & 0x3) << 4]);124 res.push('==');125 break;126 }127 var c2 = utf8.charCodeAt(i++);128 // éè¦è¡¥1个=129 if (i == len) {130 res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);131 res.push(this.table[(c2 & 0x0F) << 2]);132 res.push('=');133 break;134 }135 var c3 = utf8.charCodeAt(i++);136 res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]);137 res.push(this.table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]);138 res.push(this.table[c3 & 0x3F]);139 }140 141 return res.join('');142 },143 decode : function(str) {144 if (!str) {145 return '';146 }147 148 var len = str.length;149 var i = 0;150 var res = [];151 152 while (i < len) {153 code1 = this.table.indexOf(str.charAt(i++));154 code2 = this.table.indexOf(str.charAt(i++));155 code3 = this.table.indexOf(str.charAt(i++));156 code4 = this.table.indexOf(str.charAt(i++));157 158 c1 = (code1 << 2) | (code2 >> 4);159 c2 = ((code2 & 0xF) << 4) | (code3 >> 2);160 c3 = ((code3 & 0x3) << 6) | code4;161 162 res.push(String.fromCharCode(c1));163 164 if (code3 != 64) {165 res.push(String.fromCharCode(c2));166 }167 if (code4 != 64) {168 res.push(String.fromCharCode(c3));169 }170 171 }172 173 return this.UTF8ToUTF16(res.join(''));174 },175 176 decodeForUrl : function(s) {177 if (s == null)178 return null;179 s = this.decodeSpecialLetter1(s);180 s = s.replace("*", "+");181 s = s.replace("-", "/");182 s += "=";183 return this.decode(s);184 },185 186 decodeSpecialLetter1:function(str){187 str = str.replace("-x-", "*");188 str = str.replace("-xx", "-x");189 return str;190 },191 192 encodeSpecialLetter1:function (str){193 str = str.replace("-x", "-xx");194 str = str.replace("*", "-x-");195 return str;196 },197 198 encodeForUrl:function(s){199 if (s == null)200 return null;201 var standerBase64 = this.encode(s); 202 var encodeForUrl = standerBase64; 203204 205 //转æé对urlçbase64ç¼ç 206 encodeForUrl = encodeForUrl.replace("=", "");207 encodeForUrl = encodeForUrl.replace("+", "*");208 encodeForUrl = encodeForUrl.replace("/", "-");209 //å»é¤æ¢è¡210 encodeForUrl = encodeForUrl.replace("\n", "");211 encodeForUrl = encodeForUrl.replace("\r", "");212 213 //转æ¢*å·ä¸º -x-214 //é²æ¢æè¿ååè®®çå符215 encodeForUrl = this.encodeSpecialLetter1(encodeForUrl);216 217 return encodeForUrl; 218 }219};220 221/*console.group('Test Base64: ');222var b64 = Base64.encode('Hello, oschinaï¼åæ¯ä¸å¹´æ¥æ¥å°~');223console.log(b64);224console.log(Base64.decode(b64));
...
charset-parameter.window.js
Source:charset-parameter.window.js
...16 }17 }18 return "compatible";19}20function encodeForURL(str) {21 let output = "";22 for(let i = 0; i < str.length; i++) {23 const char = str.charCodeAt(i);24 if(char > 0xFF) {25 throw new Error("We cannot deal with input that is not latin1");26 } else {27 output += "%" + char.toString(16).padStart(2, "0");28 }29 }30 return output;31}32function runTests(tests) {33 tests.forEach(val => {34 if(typeof val === "string" || val.navigable === undefined || val.encoding === undefined || isByteCompatible(val.input) !== "compatible") {35 return;36 }37 const mime = val.input;38 async_test(t => {39 const frame = document.createElement("iframe"),40 expectedEncoding = val.encoding === null ? "UTF-8" : val.encoding;41 t.add_cleanup(() => frame.remove());42 frame.onload = t.step_func(() => {43 if(frame.contentWindow.location.href === "about:blank") {44 return;45 }46 // Edge fails all these tests due to not using the correct encoding label.47 assert_equals(frame.contentDocument.characterSet, expectedEncoding);48 t.done();49 });50 frame.src = "resources/mime-charset.py?type=" + encodeForURL(mime);51 document.body.appendChild(frame);52 }, mime);53 });...
2ESAPI.js
Source:2ESAPI.js
...12// following are a few examples:13<script>alert('xy');</script>14console.log('Error: attempt to login with invalid user: %s', ESAPI.encoder().encodeForHTML('<script>alert(\'xy\');</script>'));15console.log('Error: attempt to login with invalid user: %s', ESAPI.encoder().encodeForJavaScript('<script>'));16console.log('Error: attempt to login with invalid user: %s', ESAPI.encoder().encodeForURL('<script>'));17console.log('Error: attempt to login with invalid user: %s', ESAPI.encoder().encodeForHTML("u<ntrus>te'd'"));18console.log('Error: attempt to login with invalid user: %s', ESAPI.encoder().encodeForJavaScript("u<ntrus>te'd'"));19console.log('Error: attempt to login with invalid user: %s', ESAPI.encoder().encodeForURL("u<ntrus>te'd'"));20/*21// HTML Context22String html = Encoder.forHtml("u<ntrus>te'd'");23// HTML Attribute Context24String htmlAttr = Encoder.forHtmlAttribute("u<ntrus>te'd'");25// Javascript Attribute Context...
Using AI Code Generation
1var wptools = require('wptools');2var options = {3};4wptools.api(options, function(err, resp) {5 if (err) {6 console.log(err);7 } else {8 console.log(JSON.stringify(resp, null, 2));9 }10});11#### wptools.api(options, callback)12var wptools = require('wptools');13var options = {14};15wptools.api(options, function(err, resp) {16 if (err) {17 console.log(err);18 } else {19 console.log(JSON.stringify(resp, null, 2));20 }21});22#### wptools.encodeForURL(str)23var wptools = require('wptools');24var encoded = wptools.encodeForURL('Albert Einstein');25#### wptools.decodeFromURL(str)
Using AI Code Generation
1var wptools = require('wptools');2wptools.page(url).then(function(page) {3 console.log(page.encodeForURL(url));4}).catch(function(err) {5 console.log(err);6});7var wptools = require('wptools');8wptools.page(url).then(function(page) {9 console.log(page.encodeForURL(url));10}).catch(function(err) {11 console.log(err);12});13var wptools = require('wptools');14wptools.page(url).then(function(page) {15 console.log(page.encodeForURL(url));16}).catch(function(err) {17 console.log(err);18});19var wptools = require('wptools');20wptools.page(url).then(function(page) {21 console.log(page.encodeForURL(url));22}).catch(function(err) {23 console.log(err);24});
Using AI Code Generation
1var wptools = require('wptools');2var encodedString = wptools.encodeForURL('test string');3console.log(encodedString);4var wptools = require('wptools');5var decodedString = wptools.decodeFromURL('test_string');6console.log(decodedString);7var wptools = require('wptools');8wptools.getArticleContent('test string', function(err, data){9 if(err){10 console.log(err);11 }else{12 console.log(data);13 }14});15var wptools = require('wptools');16wptools.getArticleContent('test string', function(err, data){17 if(err){18 console.log(err);19 }else{20 console.log(data);21 }22});23var wptools = require('wptools');24wptools.getArticleContent('test string', function(err, data){25 if(err){26 console.log(err);27 }else{28 console.log(data);29 }30});31var wptools = require('wptools');32wptools.getArticleContent('test string', function(err, data){33 if(err){34 console.log(err);35 }else{36 console.log(data);37 }38});39var wptools = require('wptools');40wptools.getArticleContent('test string', function(err, data){41 if(err){42 console.log(err);43 }else{44 console.log(data);45 }46});47var wptools = require('wptools');48wptools.getArticleContent('test string', function(err, data){
Using AI Code Generation
1var wptools = require('wptools');2console.log('Encoded URL: ' + url);3var wptools = require('wptools');4console.log('Decoded URL: ' + url);5var wptools = require('wptools');6var url = wptools.getSummary('Albert Einstein');7console.log('Summary: ' + url);8var wptools = require('wptools');9var url = wptools.getExtract('Albert Einstein');10console.log('Extract: ' + url);11var wptools = require('wptools');12var url = wptools.getCategories('Albert Einstein');13console.log('Categories: ' + url);14var wptools = require('wptools');15var url = wptools.getImages('Albert Einstein');16console.log('Images: ' + url);17var wptools = require('wptools');18var url = wptools.getLinks('Albert Einstein');19console.log('Links: ' + url);20var wptools = require('wptools');21var url = wptools.getReferences('Albert Einstein');22console.log('References: ' + url);23var wptools = require('wptools');24var url = wptools.getCoordinates('Albert Einstein');25console.log('Coordinates: ' + url);26var wptools = require('wptools');27var url = wptools.getRedirects('Albert Einstein');
Using AI Code Generation
1var wptools = require('wptools');2var encodeForURL = wptools.encodeForURL;3var encoded = encodeForURL('Albert Einstein');4console.log(encoded);5var wptools = require('wptools');6var decodeFromURL = wptools.decodeFromURL;7var decoded = decodeFromURL('Albert_Einstein');8console.log(decoded);9var wptools = require('wptools');10var makeRequest = wptools.makeRequest;11makeRequest(url, function (err, res, body) {12 if (err) {13 console.log(err);14 } else {15 console.log(body);16 }17});18var wptools = require('wptools');19var makeRequestWithQuery = wptools.makeRequestWithQuery;20var query = {21};22makeRequestWithQuery(url, query, function (err, res, body) {23 if (err) {24 console.log(err);25 } else {26 console.log(body);27 }28});29var wptools = require('wptools');
Using AI Code Generation
1var wptools = require('wptools');2var title = 'Albert Einstein';3var page = wptools.page(title);4page.get(function(err, resp) {5 console.log(resp);6});7{ error:8 { code: 'missingtitle',9var wptools = require('wptools');10var title = 'Albert Einstein';11var page = wptools.page(title);12page.get(function(err, resp) {13 console.log(resp);14});
Using AI Code Generation
1var wptools = require('wptools');2var wikiUrl = wptools.encodeForURL('test');3console.log(wikiUrl);4var wptools = require('wptools');5var wikiUrl = wptools.encodeForURL('test test');6console.log(wikiUrl);7var wptools = require('wptools');8var wikiUrl = wptools.encodeForURL('test test test');9console.log(wikiUrl);10var wptools = require('wptools');11var wikiUrl = wptools.encodeForURL('test test test test');12console.log(wikiUrl);13var wptools = require('wptools');14var wikiUrl = wptools.encodeForURL('test test test test test');15console.log(wikiUrl);16var wptools = require('wptools');17var wikiUrl = wptools.encodeForURL('test test test test test test');18console.log(wikiUrl);19var wptools = require('wptools');20var wikiUrl = wptools.encodeForURL('test test test test test test test');21console.log(wikiUrl);22var wptools = require('wptools');23var wikiUrl = wptools.encodeForURL('test test test test test test test test');24console.log(wikiUrl);
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!!