I found a very creative gadget website. As shown in the cover picture, its function is very simple. It converts a URL address into ooooooooo. You can convert it back to the original address by accessing the converted address. The simple process is shown in the figure below. The conversion logic is a bit like the short link platform, except that this one makes your URL address very long, but it looks like ooooooooo. I was curious about how it was implemented, so I checked the source code. This article interprets its core implementation logic, which is very interesting and clever to implement this function. PrerequisitesBefore we start, let's first learn some knowledge points. Because it involves the conversion between two addresses, which is actually the conversion between strings, some encoding and decoding capabilities will be used. "Convert characters to utf8 array". Each character after conversion has a specific unique value. For example, the utf8 format array after http conversion is [104, 116, 116, 112]. The above is encoding, and the corresponding one below is decoding, "converting utf8 array to string", for example [99, 111, 109] The converted utf8 format array is com. "Represent a Number object as a string in base 4." toString is used more frequently, but there are fewer scenarios where parameters are passed in. The radix parameter is an optional parameter used to specify the base number to be converted, ranging from 2 to 36. If this parameter is not passed in, the decimal base is used by default. "Pad the specified characters on the left side of the string until the string reaches the specified length." The basic syntax is str.padStart(targetLength [, padString]).
URL encoding/decodingNow we will start the URL encoding logic. The core logic is as follows:
There are two key points to explain above. First, what does it mean to map to different forms of o? In fact, the converted o is not one "o", but 4 kinds, but the effect we see with the naked eye is very similar, which can be seen by the characters converted by encodeURI. This actually explains why the above code converts to 4-digit system and pads the left side with 0 to 4 digits. Because the above code defines this.enc as follows, there are only four types of "o" in total, and 4-digit system will only produce 0, 1, 2, and 3, so the converted utf8 characters can be matched one by one to these special "o". The final effect is an example of converting the http character:
The whole conversion process is over. Do you think the design is very good after reading it? After encoding, it is decoding. Decoding is to reverse the above process and restore it to the original URL address. One thing to note here is that parseInt parses 4 characters each time in 4-base mode. at lastThis is the end of sharing the core implementation code. After reading it, you may feel that it is not very complicated. Based on this design, other character effects may be extended. If you are interested, you can try it. Sharing the transcoded address with your friends will definitely bring different surprises. The following is an AI gadget address I converted. Click to see the effect~ Official website address: "ooooooooooooooooooooooooo.ooo" ooooooooooooooooooooo.ooo/ooooоооoоᴏοoοᴏοoοᴏooοᴏoᴏoᴏооoоᴏᴏoо ᴏᴏоооᴏооoᴏoоᴏоооᴏоооoоооᴏоооoᴏоооoоооοооᴏооооᴏoоᴏооооооᴏооооᴏо |
<<: Six advantages of single-pair Ethernet technology
>>: I am confused. If I want to store IP addresses, what data type is better?
Attack and start again! As we bid farewell to the...
1. Overview In the previous article, why TCP need...
[[398109]] On May 7, the three major operators is...
Weak current people have one thing in common: mos...
[[398639]] This article is reprinted from the WeC...
Yesterday, the news that "Tencent's apps...
Technology has changed the way we conduct diagnos...
From the perspective of package value, the curren...
Today, the rapidly developing IoT devices can rea...
With the development and popularization of popula...
Digital-VM is a foreign VPS service provider esta...
This article will serve as a guide for using MTP ...
1. Introduction to iPerf3 iPerf3 is a widely used...
On November 10, IDC recently released the "T...
Aruba, a Hewlett Packard Enterprise company (NYSE...