将分享搜索里的数字数组转为uint16array以base64储存
This commit is contained in:
parent
b78626ab50
commit
c87cd6fb0b
|
@ -48,7 +48,6 @@ const localStorage_getBoolean = function(name, defaultValue = false) {
|
|||
else return Boolean(Number(value));
|
||||
}
|
||||
|
||||
/* 写了,但是暂时不用
|
||||
// 将字符串转为二进制字符串
|
||||
String.prototype.toUTF8Blob = function() {
|
||||
return new Blob([this.valueOf()], {
|
||||
|
@ -69,6 +68,55 @@ Blob.prototype.toBase64 = function() {
|
|||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @readonly
|
||||
* @enum {boolean}
|
||||
*/
|
||||
const Endian = {
|
||||
little: true,
|
||||
big: false
|
||||
};
|
||||
|
||||
const ArrayConvert = {
|
||||
/**
|
||||
* 将数字数组转为 ArrayBuffer
|
||||
* @typedef {(Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array)} TypedArray
|
||||
* @param {number[]} arr - 传入数据数组
|
||||
* @param {TypedArray} typed - 输出什么 TypedArray
|
||||
* @param {boolean=} endian - True 是小端序(Little Endian), False 是大端序(Big Endian)
|
||||
* @returns {TypedArray} 输出 ArrayBuffer
|
||||
*/
|
||||
NumberArrayToBuffer: function(arr, typed, endian = true){
|
||||
const typedArray = new typed(arr.length);
|
||||
const buffer = typedArray.buffer;
|
||||
const typedName = typed.name.replace(/(?:Clamped)?Array$/i,"") ;
|
||||
const dv = new DataView(buffer);
|
||||
arr.forEach((num, idx)=>{
|
||||
dv[`set${typedName}`](idx * typed.BYTES_PER_ELEMENT, num, endian);
|
||||
});
|
||||
return typedArray;
|
||||
},
|
||||
/**
|
||||
* 将 ArrayBuffer 或 TypedArray 转为数字数组
|
||||
* @typedef {(Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array)} TypedArray
|
||||
* @param {(TypedArray|ArrayBuffer)} data - 传入数据
|
||||
* @param {TypedArray} typed - 读取什么 TypedArray
|
||||
* @param {boolean=} endian - True 是小端序(Little Endian), False 是大端序(Big Endian)
|
||||
* @returns {number[]} 输出的数字数组
|
||||
*/
|
||||
BufferToNumberArray: function(data, typed, endian = true){
|
||||
const buffer = (data instanceof ArrayBuffer) ? data : data.buffer;
|
||||
const arr = new Array(buffer.byteLength / typed.BYTES_PER_ELEMENT);
|
||||
const typedName = typed.name.replace(/(?:Clamped)?Array$/i,"") ;
|
||||
const dv = new DataView(buffer);
|
||||
for (let i = 0; i < arr.length; i ++) {
|
||||
arr[i] = dv[`get${typedName}`](i * typed.BYTES_PER_ELEMENT, endian);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
const Base64 = {
|
||||
strToBase64: function(str) {
|
||||
const encoder = new TextEncoder()
|
||||
|
@ -166,14 +214,26 @@ const Base64 = {
|
|||
sB64Enc.substring(0, sB64Enc.length - 2 + nMod3) +
|
||||
(nMod3 === 2 ? "" : nMod3 === 1 ? "=" : "==")
|
||||
);
|
||||
},
|
||||
bytesToBase64DataUrl: async function(bytes, type = "application/octet-stream") {
|
||||
return await new Promise((resolve, reject) => {
|
||||
const reader = Object.assign(new FileReader(), {
|
||||
onload: () => resolve(reader.result),
|
||||
onerror: () => reject(reader.error),
|
||||
});
|
||||
reader.readAsDataURL(new File([bytes], "", { type }));
|
||||
});
|
||||
},
|
||||
dataUrlToBytes: async function(dataUrl) {
|
||||
const res = await fetch(dataUrl);
|
||||
return new Uint8Array(await res.arrayBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
//Buffer转16进制字符串
|
||||
Uint8Array.prototype.toHex = function() {
|
||||
Uint8Array.prototype.toHex1 = function() {
|
||||
return [...this].map(n=>n.toString(16).padStart(2,'0')).join('');
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* 大数字以数字量级分隔符形式输出
|
||||
|
|
27
script.js
27
script.js
|
@ -2188,10 +2188,18 @@ function reloadFormationData(event) {
|
|||
const mid = event?.state?.mid ?? parseInt(getQueryString("id"), 10);
|
||||
const searchArr = event?.state?.searchArr ?? (str=>{
|
||||
try {
|
||||
const arr = JSON.parse(str);
|
||||
if (Array.isArray(arr) && arr.every(item=>Number.isInteger(item))) {
|
||||
return arr;
|
||||
} else return;
|
||||
const obj = JSON.parse(str);
|
||||
//2025年3月27日之前的数字数组
|
||||
if (Array.isArray(obj) && obj.every(item=>Number.isInteger(item))) {
|
||||
return obj;
|
||||
}
|
||||
//2025年3月27日之后的 Uint16 数组转 Base64
|
||||
else if (obj.ui16) {
|
||||
const ui8arr = Base64.decodeToUint8Array(obj.ui16);
|
||||
const idArr = ArrayConvert.BufferToNumberArray(ui8arr, Uint16Array, Endian.little);
|
||||
return idArr;
|
||||
}
|
||||
else return;
|
||||
} catch (error) {
|
||||
return;
|
||||
}
|
||||
|
@ -4978,8 +4986,17 @@ function initialize() {
|
|||
const optionJSON = JSON.stringify(options);
|
||||
const locationURL = new URL(location);
|
||||
locationURL.searchParams.set('search-options', optionJSON);
|
||||
|
||||
const idArr = searchMonList.originalHeads?.map(head=>head.card.id) ?? [];
|
||||
locationURL.searchParams.set('show-search', JSON.stringify(idArr));
|
||||
if (idArr.length > 0) {
|
||||
const ui16Arr = ArrayConvert.NumberArrayToBuffer(idArr, Uint16Array, Endian.little);
|
||||
const b64 = Base64.encodeFromUint8Array(new Uint8Array(ui16Arr.buffer));
|
||||
const outObj = {
|
||||
"ui16": b64
|
||||
};
|
||||
locationURL.searchParams.set('show-search', JSON.stringify(outObj));
|
||||
}
|
||||
|
||||
showAnyStringDialog.showString(locationURL.toString(), true);
|
||||
}
|
||||
const showAnyStringDialog = document.getElementById("dialog-show-any-string");
|
||||
|
|
|
@ -44231,11 +44231,11 @@ const cachesMap = new Map([
|
|||
],
|
||||
[
|
||||
"script-universal_function.js",
|
||||
"0bdc4d583774cd51cf302f7709cc3fb7"
|
||||
"312ac475579456dbff44887ccaceb658"
|
||||
],
|
||||
[
|
||||
"script.js",
|
||||
"270447ead17c33194fb0525cab9744f7"
|
||||
"c6c3de98108ff05bcd1099a8f39907e8"
|
||||
],
|
||||
[
|
||||
"solo.html",
|
||||
|
@ -44479,7 +44479,7 @@ const cachesMap = new Map([
|
|||
],
|
||||
[
|
||||
"library/core-js.min.js",
|
||||
"a650d77560070956aae8559c8cc62f21"
|
||||
"8b28275b4bc0267c494cd46a15a7734f"
|
||||
],
|
||||
[
|
||||
"library/html2canvas.min.js",
|
||||
|
|
Loading…
Reference in New Issue