|
|
|
@@ -52,23 +52,125 @@ function localStorage_getBoolean(name, defaultValue = false){ |
|
|
|
}
|
|
|
|
|
|
|
|
// 将字符串转为二进制字符串
|
|
|
|
String.prototype.toUTF16BinaryString = function() {
|
|
|
|
const charCodes16Arr = [...this].map(char=>char.charCodeAt(0)); //将每个字符转为数字
|
|
|
|
const codeUnits = new Uint16Array(charCodes16Arr); //将每个字符存入 2 字节中。警告:仅限 0xFFFF 前的Unicode字符,之后的就得换 Uint32Array
|
|
|
|
const charCodes = new Uint8Array(codeUnits.buffer); //每两个存入中
|
|
|
|
const result = [...charCodes].map(code=>String.fromCharCode(code)).join('');
|
|
|
|
return result;
|
|
|
|
String.prototype.toUTF8Blob = function() {
|
|
|
|
return new Blob([this.valueOf()], {
|
|
|
|
type: 'text/plain'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
String.fromBinaryString = function(binary) {
|
|
|
|
const bytes = new Uint8Array([...binary].map(char=>char.charCodeAt(0)));
|
|
|
|
const charCodes = new Uint16Array(bytes.buffer);
|
|
|
|
const result = [...charCodes].map(code=>String.fromCharCode(code)).join('');
|
|
|
|
return result;
|
|
|
|
Blob.prototype.toBase64 = function() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const fileReader = new FileReader();
|
|
|
|
fileReader.onload = (event) => {
|
|
|
|
resolve(event.target?.result);
|
|
|
|
};
|
|
|
|
// readAsDataURL
|
|
|
|
fileReader.readAsDataURL(this.valueOf());
|
|
|
|
fileReader.onerror = () => {
|
|
|
|
reject(new Error('blobToBase64 error'));
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
String.fromBase64 = function(base64) {
|
|
|
|
return String.fromBinaryString(atob(base64));
|
|
|
|
|
|
|
|
const Base64 = {
|
|
|
|
strToBase64: function(str) {
|
|
|
|
const encoder = new TextEncoder()
|
|
|
|
const view = encoder.encode(str);
|
|
|
|
const base64 = Base64.encodeFromUint8Array(view);
|
|
|
|
return base64;
|
|
|
|
},
|
|
|
|
base64ToStr: function(base64) {
|
|
|
|
const decoder = new TextDecoder()
|
|
|
|
const view = Base64.decodeToUint8Array(base64);
|
|
|
|
const str = decoder.decode(view);
|
|
|
|
return str;
|
|
|
|
},
|
|
|
|
//Base64还原成Uint8Array
|
|
|
|
decodeToUint8Array: function base64DecToArr(sBase64, nBlocksSize) {
|
|
|
|
function b64ToUint6(nChr) {
|
|
|
|
return nChr > 64 && nChr < 91
|
|
|
|
? nChr - 65
|
|
|
|
: nChr > 96 && nChr < 123
|
|
|
|
? nChr - 71
|
|
|
|
: nChr > 47 && nChr < 58
|
|
|
|
? nChr + 4
|
|
|
|
: nChr === 43
|
|
|
|
? 62
|
|
|
|
: nChr === 47
|
|
|
|
? 63
|
|
|
|
: 0;
|
|
|
|
}
|
|
|
|
const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, ""); // Remove any non-base64 characters, such as trailing "=", whitespace, and more.
|
|
|
|
const nInLen = sB64Enc.length;
|
|
|
|
const nOutLen = nBlocksSize
|
|
|
|
? Math.ceil(((nInLen * 3 + 1) >> 2) / nBlocksSize) * nBlocksSize
|
|
|
|
: (nInLen * 3 + 1) >> 2;
|
|
|
|
const taBytes = new Uint8Array(nOutLen);
|
|
|
|
|
|
|
|
let nMod3;
|
|
|
|
let nMod4;
|
|
|
|
let nUint24 = 0;
|
|
|
|
let nOutIdx = 0;
|
|
|
|
for (let nInIdx = 0; nInIdx < nInLen; nInIdx++) {
|
|
|
|
nMod4 = nInIdx & 3;
|
|
|
|
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << (6 * (3 - nMod4));
|
|
|
|
if (nMod4 === 3 || nInLen - nInIdx === 1) {
|
|
|
|
nMod3 = 0;
|
|
|
|
while (nMod3 < 3 && nOutIdx < nOutLen) {
|
|
|
|
taBytes[nOutIdx] = (nUint24 >>> ((16 >>> nMod3) & 24)) & 255;
|
|
|
|
nMod3++;
|
|
|
|
nOutIdx++;
|
|
|
|
}
|
|
|
|
nUint24 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return taBytes;
|
|
|
|
},
|
|
|
|
//Uint8Array编码成Base64
|
|
|
|
encodeFromUint8Array: function base64EncArr(aBytes) {
|
|
|
|
function uint6ToB64(nUint6) {
|
|
|
|
return nUint6 < 26
|
|
|
|
? nUint6 + 65
|
|
|
|
: nUint6 < 52
|
|
|
|
? nUint6 + 71
|
|
|
|
: nUint6 < 62
|
|
|
|
? nUint6 - 4
|
|
|
|
: nUint6 === 62
|
|
|
|
? 43
|
|
|
|
: nUint6 === 63
|
|
|
|
? 47
|
|
|
|
: 65;
|
|
|
|
}
|
|
|
|
let nMod3 = 2;
|
|
|
|
let sB64Enc = "";
|
|
|
|
|
|
|
|
const nLen = aBytes.length;
|
|
|
|
let nUint24 = 0;
|
|
|
|
for (let nIdx = 0; nIdx < nLen; nIdx++) {
|
|
|
|
nMod3 = nIdx % 3;
|
|
|
|
// To break your base64 into several 80-character lines, add:
|
|
|
|
// if (nIdx > 0 && ((nIdx * 4) / 3) % 76 === 0) {
|
|
|
|
// sB64Enc += "\r\n";
|
|
|
|
// }
|
|
|
|
|
|
|
|
nUint24 |= aBytes[nIdx] << ((16 >>> nMod3) & 24);
|
|
|
|
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
|
|
|
|
sB64Enc += String.fromCodePoint(
|
|
|
|
uint6ToB64((nUint24 >>> 18) & 63),
|
|
|
|
uint6ToB64((nUint24 >>> 12) & 63),
|
|
|
|
uint6ToB64((nUint24 >>> 6) & 63),
|
|
|
|
uint6ToB64(nUint24 & 63)
|
|
|
|
);
|
|
|
|
nUint24 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
sB64Enc.substring(0, sB64Enc.length - 2 + nMod3) +
|
|
|
|
(nMod3 === 2 ? "" : nMod3 === 1 ? "=" : "==")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Buffer转16进制字符串
|
|
|
|
Uint8Array.prototype.toHex = function() {
|
|
|
|
return [...this].map(n=>n.toString(16).padStart(2,'0')).join('');
|
|
|
|
|