Skip to content

Commit

Permalink
支持批量转换
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbuyi2019 committed Feb 24, 2024
1 parent 8f49319 commit d136acf
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 43 deletions.
27 changes: 24 additions & 3 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
margin: 5px;
}

#txtOutput {
textarea {
display: block;
margin: 5px;
width: 80%;
Expand Down Expand Up @@ -94,7 +94,28 @@ <h1>Steam ID 转换</h1>
<textarea id="txtOutput" readonly="readonly"></textarea>
</div>
<div>
<h2>各种 Steam ID</h2>
<h2>批量转换</h2>
输入的类型:
<div>
<input type="radio" name="steamIdType2" value="id64" checked="checked">id64
<input type="radio" name="steamIdType2" value="id32">id32
<input type="radio" name="steamIdType2" value="id3">id3
<input type="radio" name="steamIdType2" value="id3num">好友代码
<input type="radio" name="steamIdType2" value="id3numgroup">群组代码
</div>
输出的类型:
<div>
<input type="radio" name="steamIdType3" value="id64" checked="checked">id64
<input type="radio" name="steamIdType3" value="id32">id32
<input type="radio" name="steamIdType3" value="id3">id3
<input type="radio" name="steamIdType3" value="id3num">好友/群组代码
</div>
<button id="butConvertBatch">转换</button>
<br><br>
<textarea id="txtBatch"></textarea>
</div>
<div>
<h2>各种 Steam ID 示例</h2>
<table id="tableIdExample">
<tr>
<td>名字</td>
Expand Down Expand Up @@ -145,4 +166,4 @@ <h2>各种 Steam ID</h2>
<script src="./steam-id.js"></script>
</body>

</html>
</html>
126 changes: 106 additions & 20 deletions docs/steam-id.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,89 @@
"use strict";
const butConvertBatch = document.getElementById("butConvertBatch");
const txtBatch = document.getElementById("txtBatch");
function ConvertBatch() {
let inputType = SteamId.SteamIdType.id64;
let radios = document.getElementsByName("steamIdType2");
let inputv = "";
for (const element of radios) {
const radio = element;
if (radio.checked) {
inputv = radio.value;
inputType = getTypeFromTypeName(radio.value);
break;
}
}
let outputType = "";
radios = document.getElementsByName("steamIdType3");
for (const element of radios) {
const radio = element;
if (radio.checked) {
outputType = radio.value;
break;
}
}
let raw = txtBatch.value.trim();
if (raw.length < 1) {
return "";
}
let lines = raw.split("\n");
let output = "";
function addLine(str) {
if (output.length > 0) {
output += "\n";
}
output += str;
}
for (const lineraw of lines) {
let line = lineraw.trim();
if (line.length < 1) {
addLine("");
continue;
}
switch (inputv) {
case 'id3num':
line = `U:1:${line}`;
break;
case 'id3numgroup':
line = `g:1:${line}`;
break;
}
try {
const id = new SteamId.SteamIdInfo(line, inputType);
switch (outputType) {
case "id64":
addLine(id.GetId64());
break;
case "id32":
addLine(id.GetId32());
break;
case "id3":
addLine(id.GetId3());
break;
case "id3num":
addLine(id.GetId3Number().toString());
break;
default:
addLine("未知类型 " + outputType);
break;
}
}
catch (error) {
addLine(`错误 ${line} ${error}`);
}
}
return output.trim();
}
butConvertBatch.addEventListener("click", function () {
let str;
try {
str = ConvertBatch();
}
catch (error) {
str = "出错: " + String(error);
}
txtBatch.value = str;
});
var SteamId;
(function (SteamId) {
let SteamIdType;
Expand Down Expand Up @@ -124,6 +209,22 @@ const butConvert = document.getElementById('butConvert');
const txtOutput = document.getElementById('txtOutput');
const butGotoURL = document.getElementById('butGotoURL');
let lastUrl = "";
function getTypeFromTypeName(s) {
switch (s) {
case 'id3num':
return SteamId.SteamIdType.id3;
case 'id3numgroup':
return SteamId.SteamIdType.id3;
case 'id3':
return SteamId.SteamIdType.id3;
case 'id64':
return SteamId.SteamIdType.id64;
case 'id32':
return SteamId.SteamIdType.id32;
default:
throw `无法识别的输入类型: ${s}`;
}
}
function doWork() {
lastUrl = "";
let inputType = SteamId.SteamIdType.id64;
Expand All @@ -133,25 +234,7 @@ function doWork() {
const radio = element;
if (radio.checked) {
inputv = radio.value;
switch (inputv) {
case 'id3num':
inputType = SteamId.SteamIdType.id3;
break;
case 'id3numgroup':
inputType = SteamId.SteamIdType.id3;
break;
case 'id3':
inputType = SteamId.SteamIdType.id3;
break;
case 'id64':
inputType = SteamId.SteamIdType.id64;
break;
case 'id32':
inputType = SteamId.SteamIdType.id32;
break;
default:
throw `无法识别的输入类型: ${inputv}`;
}
inputType = getTypeFromTypeName(inputv);
break;
}
}
Expand Down Expand Up @@ -186,7 +269,10 @@ function doWork() {
addLine(`id32: ${id.GetId32()}`);
addLine(`id3: ${id.GetId3()}`);
if (id.IsGroup) {
addLine("群组");
addLine(`群组代码: ${id.GetId3Number()}`);
}
else {
addLine(`好友代码: ${id.GetId3Number()}`);
}
return out;
}
Expand Down
83 changes: 83 additions & 0 deletions src/batch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const butConvertBatch = document.getElementById("butConvertBatch") as HTMLButtonElement
const txtBatch = document.getElementById("txtBatch") as HTMLTextAreaElement

function ConvertBatch(): string {
let inputType: SteamId.SteamIdType = SteamId.SteamIdType.id64
let radios = document.getElementsByName("steamIdType2")
let inputv: string = ""
for (const element of radios) {
const radio = element as HTMLInputElement
if (radio.checked) {
inputv = radio.value
inputType = getTypeFromTypeName(radio.value)
break;
}
}
let outputType: string = ""
radios = document.getElementsByName("steamIdType3")
for (const element of radios) {
const radio = element as HTMLInputElement
if (radio.checked) {
outputType = radio.value
break;
}
}
let raw = txtBatch.value.trim()
if (raw.length < 1) { return ""; }
let lines = raw.split("\n")
let output = ""
function addLine(str: string) {
if (output.length > 0) {
output += "\n"
}
output += str
}
for (const lineraw of lines) {
let line = lineraw.trim()
if (line.length < 1) {
addLine("")
continue
}
switch (inputv) {
case 'id3num':
line = `U:1:${line}`
break
case 'id3numgroup':
line = `g:1:${line}`
break
}
try {
const id = new SteamId.SteamIdInfo(line, inputType)
switch (outputType) {
case "id64":
addLine(id.GetId64())
break
case "id32":
addLine(id.GetId32())
break
case "id3":
addLine(id.GetId3())
break
case "id3num":
addLine(id.GetId3Number().toString())
break
default:
addLine("未知类型 " + outputType)
break
}
} catch (error) {
addLine(`错误 ${line} ${error}`)
}
}
return output.trim()
}

butConvertBatch.addEventListener("click", function () {
let str: string
try {
str = ConvertBatch()
} catch (error) {
str = "出错: " + String(error)
}
txtBatch.value = str
})
41 changes: 21 additions & 20 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ const butGotoURL = document.getElementById('butGotoURL') as HTMLAnchorElement

let lastUrl = ""

function getTypeFromTypeName(s: string): SteamId.SteamIdType {
switch (s) {
case 'id3num':
return SteamId.SteamIdType.id3
case 'id3numgroup':
return SteamId.SteamIdType.id3
case 'id3':
return SteamId.SteamIdType.id3
case 'id64':
return SteamId.SteamIdType.id64
case 'id32':
return SteamId.SteamIdType.id32
default:
throw `无法识别的输入类型: ${s}`
}
}

function doWork(): string {
lastUrl = ""
let inputType: SteamId.SteamIdType = SteamId.SteamIdType.id64
Expand All @@ -17,25 +34,7 @@ function doWork(): string {
const radio = element as HTMLInputElement
if (radio.checked) {
inputv = radio.value
switch (inputv) {
case 'id3num':
inputType = SteamId.SteamIdType.id3
break
case 'id3numgroup':
inputType = SteamId.SteamIdType.id3
break
case 'id3':
inputType = SteamId.SteamIdType.id3
break
case 'id64':
inputType = SteamId.SteamIdType.id64
break
case 'id32':
inputType = SteamId.SteamIdType.id32
break
default:
throw `无法识别的输入类型: ${inputv}`
}
inputType = getTypeFromTypeName(inputv)
break;
}
}
Expand Down Expand Up @@ -67,7 +66,9 @@ function doWork(): string {
addLine(`id32: ${id.GetId32()}`)
addLine(`id3: ${id.GetId3()}`)
if (id.IsGroup) {
addLine("群组")
addLine(`群组代码: ${id.GetId3Number()}`)
} else {
addLine(`好友代码: ${id.GetId3Number()}`)
}
return out;
}
Expand Down

0 comments on commit d136acf

Please sign in to comment.