1 Commits

Author SHA1 Message Date
8af13812fa chore: ignore .idea directory 2025-01-28 08:43:28 +08:00
4 changed files with 158 additions and 347 deletions

BIN
.gitignore vendored

Binary file not shown.

102
README.md
View File

@ -1,17 +1,10 @@
<div align="center"> <img src="https://github.com/Cunninger/ocr-based-qwen/blob/main/ocr.png?raw=true" width="120"> </div>
# 🖼️ QwenLM OCR
# QwenLM OCR
本项目基于 [QwenLM](https://chat.qwenlm.ai/) 。通过调用 QwenLM 的 API你可以从图片中提取文字内容并且该项目支持一键部署到 **Cloudflare Workers** (CF) 上。
本项目是对 [QwenLM](https://chat.qwenlm.ai/) 的 OCR 功能进行逆向工程的实现。通过调用 QwenLM 的 API你可以从图片中提取文字内容并且该项目支持一键部署到 **Cloudflare Workers** (CF) 上。
## 项目展示
![image](https://github.com/user-attachments/assets/9e794686-f914-4fb7-9931-1ada7c22bb2d)
## 测试cookie
```txt
cna=LI8HIAirQyACAWrg3cNOjH8F; _gcl_au=1.1.1200556497.1736728490; xlly_s=1; acw_tc=c2b96feb4d2929a1649ea96dc00590956477696933f61783d23e6c95429ecf74; x-ap=ap-southeast-1; token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjUzZTk0Nzg4LWMwM2QtNDY4Mi05OTNhLWE0ZDNjNGUyZDY0OSIsImV4cCI6MTc0MTE0ODM0MX0.-2hF4l1iJf8r5U6UzoXyc-TFqx0i5luWmtJk0kk8T5o; ssxmod_itna=Yq+hqIxjx=AKi7eitD8IDCWBfbxxl4BtGRDeq7tDRDFqApxDHmLWxoDUxxu2hE80DD0ADnG3BUxGN4tDxiNDAg40iDC4mL37ezTxthtQQhgDCTYCu2rpTCGGE7P3rnTE0M25BA6w3KxxGLDY=DCqhxl4eD4R3Dt4DIDAYDDxDWFeGIDGYD7hb=ymbDm44aYxGy3nbDA3TDbhb5xitYDDUAKeG2igbhDDNheFY49GG7yC0OorHDAqh==GbDjqPD/RxLP+bkXtkh/CTA=BapCeaWxBQD7we3xYE4c=IdqZawfiEwDELx0De/iGKGxdYmPlvzYKGlpVGGoWDqjqQlXnlvnnPDDc=Y9=buiDrP3KYtvS85lmHQDxPoGb0rz0THimDgri0i8B0=nxNCDtYDb30eDBeIG4/g8BvDD; ssxmod_itna2=Yq+hqIxjx=AKi7eitD8IDCWBfbxxl4BtGRDeq7tDRDFqApxDHmLWxoDUxxu2hE80DD0ADnG3BDxDfk7K7e5xDLiARWCTe9A4D/9o1+WeqlSGSWe4dTPhBUq4kzjyE41MPvWM=BtDwWHWiQ4B+GVChGZYlDDoA6gCj2VGnQeqqxeBSiqeT69Cg2Q6qrEGqv/DTQR8MRo3m3E7Gd7mf42Re2lYpNic=beSRp3tCDgWT7khOXGqf2lBpre5BtExlRw6cUwWwDxNSFUl/8Un/RGZz8/jLk2t1XZC79Eb7iRTPTMPn5MTHW2fs1DlsH0z9RxCLW1Rdb2QFhd9biugr+2aTW1oeYAbtORQNMW43+m2Gcb=TuFtQGqhiTqrNfdwSOe8WHSWTr7cRaw833Yzw31d4iN+Q9OO7izUj1uSdYrh4dIvx5CwYD45Vji4EbkiaNQQGbt1hDa8Ymmda3r3lThChY8Nm2IscznS+mS43izuLzUYN94syC3tQi03=u3Q6az931moHSOLY3OFPKoMjqMUDbtebxw=rT2rBAv08uRkWHzFtGp2lUSSb3z41pTZcx/8NB3jD+XnIpLFEw8bv4r58wRLLwubFTQ9cPDv+9k36tvoCYXlso4qGA44E3y737YNBDmUct2Iahxq76iTrlGotG1NBBN=Tx4hkA2Zp887gfc4Tp8V1wGCf49XeA=0Xu3uIKqYQADh0ABmqlQ4w=YVlDPjO9KH2xaG3C8ID5DeY41AxMxR2NIGhSD+5q0UQGx0Ue4dAi0DwbY+f7n1Gwz014LQ2DY+42xI2zG5TmhfDtXehC40aWxfDi0xIGNQ3gGkYeu2fAfADwY31mkAooDxD; SERVERID=da7472215188c88fe194f138f1242089|1738557149|1738556339; SERVERCORSID=da7472215188c88fe194f138f1242089|1738557149|1738556339; isg=BBcXHLltPM7zv7hi-FnT0HW6pothXOu-LIccEmlG4eZGmDrabS7mDq_--ziGPsM2; tfstk=gxYniE986HSQfEpYpd7Icypow5otjW_5cLUReaBrbOW6JLnCehbygKjRyBdPr4XN8gtde9BlrQ9m6x3xkBOCPgkxHqIXCrTVlurzY8QNb_blTz_06LRCPaktd3lv5B9qU3M64alG7_fz8aWFTPlGZsXPz9SzQl5VQT7yU9SaQ617YJzUzRlGNOWPzaJP7jQ8r9CV1FkgVOPK4CrD_6jh3BWH9KLadGrBiO4zzFjfxtuAIzzyS6Ot3kbuo0B2c9IACduT-aANZKfDKRkGzCYBoG8ECzvDIn7G1hMuqtxy9ntPjXueswXhmFI0Zk7H4pTlAHVSGC7eCn6fYcM1seLAqtsgQvR9s9SDqpH_z9KD_djvWRUA7I-GSgozbo5IVz1ZwFr7VM51stK4FEPS3FZLvfcg0MIF16r-sfq7VM51sthisoJlY11U2
```
## 🚀 功能特性
- **图片 OCR**:使用 QwenLM 强大的 OCR 功能从图片中提取文字。
@ -21,7 +14,7 @@ cna=LI8HIAirQyACAWrg3cNOjH8F; _gcl_au=1.1.1200556497.1736728490; xlly_s=1; acw_t
- **历史记录**:保存每次识别的结果和图片,方便查看。
- **一键复制**:轻松复制识别结果到剪贴板。
- **数学公式识别**:特别优化了对数学公式的提取,支持 LaTeX 格式输出。
- **API 支持**:提供 `curl` 接口调用,支持通过图片文件、base64 和图片 URL 3种方式。(Apifox调用文档示例(仅作为代码示例,尝试本地调试)https://we4q3dzb3r.apifox.cn/)
- **API 支持**:提供 `curl` 接口调用,支持 base64 和图片 URL 种方式。
- **验证码识别**:新增验证码识别功能,支持常见类型的验证码(如数字、字母、混合字符等),提升自动化处理能力。
## 🛠️ 部署指南
@ -38,12 +31,12 @@ cna=LI8HIAirQyACAWrg3cNOjH8F; _gcl_au=1.1.1200556497.1736728490; xlly_s=1; acw_t
## 🧩 使用说明
1. **设置 Cookie**
- 前往 [QwenLM](https://chat.qwenlm.ai/) 获取对话请求中的 Cookie
![alt text](image.png)
1. **设置 Token**
- 前往 [QwenLM](https://chat.qwenlm.ai/) 获取 Bearer后面的Token
![image](https://github.com/user-attachments/assets/9fa3ea04-da2b-4756-a091-83c0665f62f3)
- 点击右上角的 **⚙️ Cookie设置** 按钮。
- 输入你的 Cookie或者使用测试Cookie)。
- 点击右上角的 **⚙️ Token设置** 按钮。
- 输入你的 QwenLM API Token多个 Token 用英文逗号分隔)。
- 点击 **保存**
2. **上传图片**
@ -61,22 +54,20 @@ cna=LI8HIAirQyACAWrg3cNOjH8F; _gcl_au=1.1.1200556497.1736728490; xlly_s=1; acw_t
5. **API 调用**
- **支持 base64**
```bash
curl -X POST \
'https://test-qwen-cor.aughumes8.workers.dev/api/recognize/base64' \
-H 'Content-Type: application/json' \
-H 'x-custom-cookie: YOUR_COOKIE_STRING' \
-d '{
"base64Image": "YOUR_BASE64_IMAGE_STRING"
}'
curl --location 'https://ocr.doublefenzhuan.me/api/recognize/base64' \
--header 'Content-Type: application/json' \
--data '{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjUzZTk0Nzg4LWMwM2QtNDY4Mi05OTNhLWE0ZDNjNGUyZDY0OSIsImV4cCI6MTczOTA3NTE0MX0.FtwG6xDLYd2rngWUhuldg56WXCiLSTL0RI6xJJQ4vHM",
"base64Image": "xxx"
}'
```
- **支持图片 URL**:
```bash
curl -X POST \
'https://test-qwen-cor.aughumes8.workers.dev/api/recognize/url' \
-H 'Content-Type: application/json' \
-H 'x-custom-cookie: YOUR_COOKIE_STRING' \
-d '{
"imageUrl": "YOUR_IMAGE_URL"
curl --location 'https://ocr.doublefenzhuan.me/api/recognize/url' \
--header 'Content-Type: application/json' \
--data '{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjUzZTk0Nzg4LWMwM2QtNDY4Mi05OTNhLWE0ZDNjNGUyZDY0OSIsImV4cCI6MTczOTA3NTE0MX0.FtwG6xDLYd2rngWUhuldg56WXCiLSTL0RI6xJJQ4vHM",
"imageUrl": "xxxx"
}'
```
6. **验证码识别**
@ -121,50 +112,31 @@ cna=LI8HIAirQyACAWrg3cNOjH8F; _gcl_au=1.1.1200556497.1736728490; xlly_s=1; acw_t
### 2025/01/13 18点34分 支持`curl`接口调用
- **支持base64**
```bash
curl -X POST \
'https://test-qwen-cor.aughumes8.workers.dev/api/recognize/base64' \
-H 'Content-Type: application/json' \
-H 'x-custom-cookie: YOUR_COOKIE_STRING' \
-d '{
"base64Image": "YOUR_BASE64_IMAGE_STRING"
}'
```
curl --location 'https://ocr.doublefenzhuan.me/api/recognize/base64' \
--header 'Content-Type: application/json' \
--data '{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjUzZTk0Nzg4LWMwM2QtNDY4Mi05OTNhLWE0ZDNjNGUyZDY0OSIsImV4cCI6MTczOTA3NTE0MX0.FtwG6xDLYd2rngWUhuldg56WXCiLSTL0RI6xJJQ4vHM",
"base64Image": "xxx"
}'
```
- 效果图:
![image](https://github.com/user-attachments/assets/363afb39-444b-4308-a2bd-55831df81b6f)
![image](https://github.com/user-attachments/assets/ef160aae-e741-49d3-96f0-a0969b883f1a)
- **支持图片URL**:
```bash
curl -X POST \
'https://test-qwen-cor.aughumes8.workers.dev/api/recognize/url' \
-H 'Content-Type: application/json' \
-H 'x-custom-cookie: YOUR_COOKIE_STRING' \
-d '{
"imageUrl": "YOUR_IMAGE_URL"
}'
curl --location 'https://ocr.doublefenzhuan.me/api/recognize/url' \
--header 'Content-Type: application/json' \
--data '{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjUzZTk0Nzg4LWMwM2QtNDY4Mi05OTNhLWE0ZDNjNGUyZDY0OSIsImV4cCI6MTczOTA3NTE0MX0.FtwG6xDLYd2rngWUhuldg56WXCiLSTL0RI6xJJQ4vHM",
"imageUrl": "xxxx"
}'
```
- 效果图:
![image](https://github.com/user-attachments/assets/a816085e-8a52-4425-b02c-94ea543bf95b)
- **通过图片文件识别需要先上传获取imageId**
```bash
# 1. 先上传文件
curl -X POST \
'https://test-qwen-cor.aughumes8.workers.dev/proxy/upload' \
-H 'x-custom-cookie: YOUR_COOKIE_STRING' \
-F 'file=@/path/to/your/image.jpg'
# 2. 使用返回的imageId进行识别
curl -X POST \
'https://test-qwen-cor.aughumes8.workers.dev/recognize' \
-H 'Content-Type: application/json' \
-H 'x-custom-cookie: YOUR_COOKIE_STRING' \
-d '{
"imageId": "RETURNED_IMAGE_ID"
}'
```
![image](https://github.com/user-attachments/assets/db0c89f9-96f1-45b1-b1e9-88ac3d01e196)
## Cloudflare访问数据
![image](https://github.com/user-attachments/assets/bb456075-6107-47ee-a361-a0edba532c38)
## 趋势
[![Star History Chart](https://api.star-history.com/svg?repos=cunninger/ocr-based-qwen&type=Date)](https://star-history.com/#Cunninger/ocr-based-qwen&Date)
[![Star History Chart](https://api.star-history.com/svg?repos=cunninger/ocr-based-qwen&type=Date&v=20251114)](https://star-history.com/#Cunninger/ocr-based-qwen&Date)

BIN
image.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 327 KiB

403
worker.js
View File

@ -4,7 +4,6 @@ addEventListener('fetch', event => {
async function handleRequest(request) {
const url = new URL(request.url);
const cookie = request.headers.get('x-custom-cookie') || ''; // 从自定义请求头获取 cookie
// 处理 CORS 预检请求
if (request.method === 'OPTIONS') {
@ -12,7 +11,7 @@ async function handleRequest(request) {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, x-custom-cookie', // 添加自定义请求头
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
});
}
@ -60,30 +59,17 @@ async function handleRequest(request) {
// 处理图片URL识别
async function handleImageUrlRecognition(request) {
try {
const { imageUrl } = await request.json();
const cookie = request.headers.get('x-custom-cookie');
const { token, imageUrl } = await request.json();
if (!cookie || !imageUrl) {
if (!token || !imageUrl) {
return new Response(JSON.stringify({
error: 'Missing cookie or imageUrl'
error: 'Missing token or imageUrl'
}), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}
// 从cookie中提取token
const tokenMatch = cookie.match(/token=([^;]+)/);
if (!tokenMatch) {
return new Response(JSON.stringify({
error: 'Invalid cookie format: missing token'
}), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}
const token = tokenMatch[1];
// 下载图片
const imageResponse = await fetch(imageUrl);
const imageBlob = await imageResponse.blob();
@ -97,7 +83,6 @@ async function handleImageUrlRecognition(request) {
headers: {
'accept': 'application/json',
'authorization': `Bearer ${token}`,
'cookie': cookie
},
body: formData,
});
@ -105,8 +90,8 @@ async function handleImageUrlRecognition(request) {
const uploadData = await uploadResponse.json();
if (!uploadData.id) throw new Error('File upload failed');
// 调用通用识别函数
return await recognizeImage(token, uploadData.id, request);
// 调用识别API
return await recognizeImage(token, uploadData.id);
} catch (error) {
return new Response(JSON.stringify({
error: error.message || 'Internal Server Error'
@ -120,32 +105,22 @@ async function handleImageUrlRecognition(request) {
// 处理Base64识别
async function handleBase64Recognition(request) {
try {
const { base64Image } = await request.json();
const cookie = request.headers.get('x-custom-cookie');
const { token, base64Image } = await request.json();
if (!cookie || !base64Image) {
if (!token || !base64Image) {
return new Response(JSON.stringify({
error: 'Missing cookie or base64Image'
error: 'Missing token or base64Image'
}), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}
// 从cookie中提取token
const tokenMatch = cookie.match(/token=([^;]+)/);
if (!tokenMatch) {
return new Response(JSON.stringify({
error: 'Invalid cookie format: missing token'
}), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}
const token = tokenMatch[1];
// 转换Base64为Blob
const imageData = base64Image.startsWith('data:') ? base64Image : 'data:image/png;base64,' + base64Image;
const imageData = base64Image.startsWith('data:') ?
base64Image :
'data:image/png;base64,' + base64Image;
const response = await fetch(imageData);
const blob = await response.blob();
@ -158,7 +133,6 @@ async function handleBase64Recognition(request) {
headers: {
'accept': 'application/json',
'authorization': `Bearer ${token}`,
'cookie': cookie
},
body: formData,
});
@ -166,8 +140,8 @@ async function handleBase64Recognition(request) {
const uploadData = await uploadResponse.json();
if (!uploadData.id) throw new Error('File upload failed');
// 调用通用识别函数
return await recognizeImage(token, uploadData.id, request);
// 调用识别API
return await recognizeImage(token, uploadData.id);
} catch (error) {
return new Response(JSON.stringify({
error: error.message || 'Internal Server Error'
@ -181,23 +155,18 @@ async function handleBase64Recognition(request) {
// 处理文件识别 (原有功能)
async function handleFileRecognition(request) {
try {
const { imageId } = await request.json();
const cookie = request.headers.get('x-custom-cookie') || '';
const { token, imageId } = await request.json();
if (!cookie || !imageId) {
if (!token || !imageId) {
return new Response(JSON.stringify({
error: 'Missing cookie or imageId'
error: 'Missing token or imageId'
}), {
status: 400,
headers: { 'Content-Type': 'application/json' },
});
}
// 从cookie中提取token
const tokenMatch = cookie.match(/token=([^;]+)/);
const token = tokenMatch ? tokenMatch[1] : '';
return await recognizeImage(token, imageId, request);
return await recognizeImage(token, imageId);
} catch (error) {
return new Response(JSON.stringify({
error: error.message || 'Internal Server Error'
@ -212,18 +181,13 @@ async function handleFileRecognition(request) {
async function handleProxyUpload(request) {
try {
const formData = await request.formData();
const cookie = request.headers.get('x-custom-cookie') || '';
// 从cookie中提取token
const tokenMatch = cookie.match(/token=([^;]+)/);
const token = tokenMatch ? tokenMatch[1] : '';
const token = request.headers.get('Authorization').replace('Bearer ', '');
const response = await fetch('https://chat.qwenlm.ai/api/v1/files/', {
method: 'POST',
headers: {
'accept': 'application/json',
'authorization': `Bearer ${token}`,
'cookie': cookie
},
body: formData,
});
@ -250,20 +214,16 @@ async function handleProxyUpload(request) {
}
// 通用的识别函数
async function recognizeImage(token, imageId, request) {
const cookie = request.headers.get('x-custom-cookie') || '';
async function recognizeImage(token, imageId) {
const response = await fetch('https://chat.qwenlm.ai/api/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': 'PostmanRuntime/7.43.0',
'accept': '*/*',
'authorization': `Bearer ${token}`,
'cookie': cookie,
'Content-Type': 'application/json',
},
body: JSON.stringify({
stream: false,
chat_type: "t2t",
model: 'qwen2.5-vl-72b-instruct',
messages: [
{
@ -285,14 +245,9 @@ async function recognizeImage(token, imageId, request) {
'2. 忽略干扰线和噪点\n' +
'3. 注意区分相似字符如0和O、1和l、2和Z等\n' +
'4. 验证码通常为4-6位字母数字组合\n\n' +
'不要输出任何额外的解释或说明',
chat_type: "t2t"
},
{
type: 'image',
image: imageId,
chat_type: "t2t"
'不要输出任何额外的解释或说明'
},
{ type: 'image', image: imageId },
],
},
],
@ -1050,6 +1005,7 @@ function getHTML() {
' background: rgba(255, 255, 255, 0.9);',
' backdrop-filter: blur(5px);',
' z-index: 900;',
' border-top: 1px solid rgba(0, 0, 0, 0.1);',
' display: flex;',
' justify-content: center;',
' align-items: center;',
@ -1059,176 +1015,106 @@ function getHTML() {
' width: 100%;',
' }',
' .powered-by {',
' color: #7f8c8d;',
' font-size: 0.9rem;',
' }',
' color: #7f8c8d;',
' font-size: 0.9rem;',
' }',
' .powered-by a {',
' color: #3498db;',
' text-decoration: none;',
' transition: color 0.3s ease;',
' font-weight: 500;',
' }',
' .powered-by a {',
' color: #3498db;',
' text-decoration: none;',
' transition: color 0.3s ease;',
' font-weight: 500;',
' }',
' .powered-by a:hover {',
' color: #2980b9;',
' }',
' .powered-by a:hover {',
' color: #2980b9;',
' }',
' .powered-by {',
' color: #7f8c8d;',
' font-size: 0.9rem;',
' }',
' .powered-by {',
' color: #7f8c8d;',
' font-size: 0.9rem;',
' }',
' .powered-by a {',
' color: #3498db;',
' text-decoration: none;',
' transition: color 0.3s ease;',
' font-weight: 500;',
' }',
' .powered-by a {',
' color: #3498db;',
' text-decoration: none;',
' transition: color 0.3s ease;',
' font-weight: 500;',
' }',
' .powered-by a:hover {',
' color: #2980b9;',
' }',
' .powered-by a:hover {',
' color: #2980b9;',
' }',
' /* 输入控件组样式 */',
' .input-controls {',
' margin-top: 15px;',
' width: 100%;',
' }',
' /* 输入控件组样式 */',
' .input-controls {',
' margin-top: 15px;',
' width: 100%;',
' }',
' .button-group {',
' display: flex;',
' gap: 10px;',
' margin-top: 10px;',
' justify-content: center;',
' }',
' .button-group {',
' display: flex;',
' gap: 10px;',
' margin-top: 10px;',
' justify-content: center;',
' }',
' #urlInput {',
' width: 100%;',
' padding: 10px;',
' border: 1px solid #dcdde1;',
' border-radius: 8px;',
' resize: none;',
' font-size: 14px;',
' }',
' #urlInput {',
' width: 100%;',
' padding: 10px;',
' border: 1px solid #dcdde1;',
' border-radius: 8px;',
' resize: none;',
' font-size: 14px;',
' }',
' .github-link {',
' position: fixed;',
' right: 150px;',
' top: 20px;',
' background: #333;',
' color: white;',
' border: none;',
' padding: 10px;',
' border-radius: 50%;',
' cursor: pointer;',
' z-index: 1001;',
' width: 40px;',
' height: 40px;',
' display: flex;',
' align-items: center;',
' justify-content: center;',
' transition: background 0.3s ease;',
' }',
' .github-link {',
' position: fixed;',
' right: 150px;', // 放在 token 设置按钮左边
' top: 20px;',
' background: #333;', // GitHub 的深色背景
' color: white;',
' border: none;',
' padding: 10px;',
' border-radius: 50%;', // 圆形按钮
' cursor: pointer;',
' z-index: 1001;',
' width: 40px;',
' height: 40px;',
' display: flex;',
' align-items: center;',
' justify-content: center;',
' transition: background 0.3s ease;',
' }',
' .github-link:hover {',
' background: #24292e;',
' }',
' .github-link:hover {',
' background: #24292e;', // GitHub hover 颜色
' }',
' .github-icon {',
' width: 24px;',
' height: 24px;',
' }',
' .cookie-input-container {',
' margin-bottom: 15px;',
' }',
' #cookieInput {',
' width: 100%;',
' padding: 12px;',
' border: 2px solid #e9ecef;',
' border-radius: 8px;',
' font-size: 0.95rem;',
' resize: vertical;',
' min-height: 120px;',
' font-family: monospace;',
' line-height: 1.4;',
' }',
' #cookieInput:focus {',
' outline: none;',
' border-color: #3498db;',
' }',
' .cookie-info {',
' background: #f8f9fa;',
' padding: 12px;',
' border-radius: 8px;',
' margin-bottom: 15px;',
' }',
' .cookie-info p {',
' margin: 0;',
' color: #2c3e50;',
' font-size: 0.9rem;',
' }',
' #currentTokenDisplay {',
' color: #3498db;',
' font-family: monospace;',
' word-break: break-all;',
' }',
// 添加获取cookie按钮样式
' .get-cookie-link {',
' position: fixed;',
' right: 200px;', // 放在github图标左边
' top: 20px;',
' background: #2ecc71;', // 使用不同的颜色区分
' color: white;',
' border: none;',
' padding: 8px 15px;',
' border-radius: 5px;',
' cursor: pointer;',
' z-index: 1001;',
' text-decoration: none;',
' font-size: 14px;',
' transition: background 0.3s ease;',
' }',
' .get-cookie-link:hover {',
' background: #27ae60;',
' }',
' .github-icon {',
' width: 24px;',
' height: 24px;',
' }',
'</style>',
'</head>',
'<body>',
'<a href="https://chat.qwenlm.ai/" target="_blank" rel="noopener noreferrer" class="get-cookie-link">',
' 获取Cookie',
'</a>',
'<a href="https://github.com/Cunninger/ocr-based-qwen" target="_blank" rel="noopener noreferrer" class="github-link" title="View on GitHub">',
' <svg class="github-icon" viewBox="0 0 16 16" fill="currentColor">',
' <path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"></path>',
' </svg>',
'</a>',
'<button class="sidebar-toggle" id="sidebarToggle">⚙️ Cookie设置</button>',
'<button class="sidebar-toggle" id="sidebarToggle">⚙️ Token设置</button>',
'<div class="sidebar" id="sidebar">',
'<div class="sidebar-header">',
'<h2>Cookie管理</h2>',
'<h2>Token 管理</h2>',
'<button class="close-sidebar" id="closeSidebar">×</button>',
'</div>',
'<div class="token-section">',
'<label for="cookieInput">输入Cookie</label>',
'<div class="cookie-input-container">',
'<textarea id="cookieInput" placeholder="请输入完整的cookie字符串..." rows="8"></textarea>',
'<label for="tokenInput">输入Token</label>',
'<textarea id="tokenInput" placeholder="输入Token多个Token请用英文逗号分隔" rows="4"></textarea>',
'<button class="save-btn" id="saveTokens">保存 Token</button>',
'</div>',
'<div class="cookie-info">',
'<p>当前Token: <span id="currentTokenDisplay">未设置</span></p>',
'</div>',
'<button class="save-btn" id="saveTokens">保存设置</button>',
'</div>',
// 添加 tokenList 容器
'<div class="token-list" id="tokenList"></div>', // 添加这一行
'<div class="token-list" id="tokenList"></div>',
'</div>',
'<div class="container">',
@ -1332,7 +1218,7 @@ function getHTML() {
' const timeStr = timestamp.toLocaleString(\'zh-CN\', {',
' year: \'numeric\',',
' month: \'2-digit\',',
' day: \'2-digit\',', // Add comma here
' day: \'2-digit\',
' hour: \'2-digit\',',
' minute: \'2-digit\'',
' });',
@ -1410,11 +1296,6 @@ function getHTML() {
' // 修改 updateTokenList 函数',
' function updateTokenList() {',
' const tokenList = document.getElementById(\'tokenList\');',
' if (!tokenList) {',
' console.error(\'找不到tokenList元素\');',
' return;',
' }',
' tokenList.innerHTML = "";',
' tokens.forEach(function(token, index) {',
' var truncatedToken = token.slice(0, 10) + "..." + token.slice(-10);',
@ -1428,41 +1309,22 @@ function getHTML() {
' historyManager.displayHistory(currentToken);',
' });',
' tokenList.appendChild(div);',
' });',
' const tokenInput = document.getElementById(\'tokenInput\');',
' if (tokenInput) {',
' tokenInput.value = tokens.join(",");',
' }',
' });',
' tokenInput.value = tokens.join(",");',
' }',
' // 保存tokens',
' saveTokensBtn.addEventListener(\'click\', () => {',
' const cookieValue = document.getElementById(\'cookieInput\').value.trim();',
' if (!cookieValue) {',
' alert(\'请输入Cookie\');',
' return;',
' const inputTokens = tokenInput.value.split(\',\').map(t => t.trim()).filter(t => t);',
' if (inputTokens.length > 0) {',
' tokens = inputTokens;',
' localStorage.setItem(\'imageRecognitionTokens\', tokens.join(\',\'));',
' updateTokenList();',
' currentToken = tokens[0];',
' alert(\'Tokens已保存\');',
' } else {',
' alert(\'请至少输入一个有效的Token\');',
' }',
' ',
' // 从cookie中提取token',
' const tokenMatch = cookieValue.match(/token=([^;]+)/);',
' if (!tokenMatch) {',
' alert(\'无法从Cookie中提取token请确保Cookie中包含token字段\');',
' return;',
' }',
' ',
' const token = tokenMatch[1];',
' tokens = [token];',
' currentToken = token;',
' ',
' // 保存cookie和token',
' localStorage.setItem(\'imageRecognitionCookie\', cookieValue);',
' localStorage.setItem(\'imageRecognitionTokens\', token);',
' ',
' // 更新显示',
' document.getElementById(\'currentTokenDisplay\').textContent = ',
' token.slice(0, 10) + "..." + token.slice(-10);',
' ',
' alert(\'设置已保存\');',
' });',
' // 侧边栏开关',
@ -1478,13 +1340,6 @@ function getHTML() {
' return;',
' }',
' const savedCookie = localStorage.getItem(\'imageRecognitionCookie\');',
' if (!savedCookie) {',
' alert(\'请先设置Cookie\');',
' sidebar.classList.add(\'open\');',
' return;',
' }',
' // 显示图片预览',
' const reader = new FileReader();',
' let imageData;',
@ -1507,7 +1362,7 @@ function getHTML() {
' const uploadResponse = await fetch(\'/proxy/upload\', {',
' method: \'POST\',',
' headers: {',
' \'x-custom-cookie\': savedCookie,',
' \'Authorization\': \'Bearer \' + currentToken,',
' },',
' body: formData,',
' });',
@ -1518,19 +1373,22 @@ function getHTML() {
' // 识别图片',
' const recognizeResponse = await fetch(\'/recognize\', {',
' method: \'POST\',',
' headers: { ',
' \'Content-Type\': \'application/json\',',
' \'x-custom-cookie\': savedCookie,',
' },',
' body: JSON.stringify({ imageId: uploadData.id }),',
' headers: { \'Content-Type\': \'application/json\' },',
' body: JSON.stringify({ ',
' token: currentToken, ',
' imageId: uploadData.id ',
' }),',
' });',
' const recognizeData = await recognizeResponse.json();',
' // 修改这里:使用新的响应格式',
' if (!recognizeData.success) {',
' throw new Error(recognizeData.error || \'识别失败\');',
' }',
' const result = recognizeData.result || \'识别失败\';',
' // 保存原始文本到属性中,确保 LaTeX 格式完整',
' resultDiv.setAttribute(\'data-original-text\', result);',
' resultDiv.innerHTML = result;',
' waitForMathJax(() => {',
@ -1906,25 +1764,6 @@ function getHTML() {
' timeout = setTimeout(later, wait);',
' };',
' }',
' // 加载保存的cookie',
' function loadSettings() {',
' const savedCookie = localStorage.getItem(\'imageRecognitionCookie\');',
' if (savedCookie) {',
' document.getElementById(\'cookieInput\').value = savedCookie;',
' const tokenMatch = savedCookie.match(/token=([^;]+)/);',
' if (tokenMatch) {',
' const token = tokenMatch[1];',
' tokens = [token];',
' currentToken = token;',
' document.getElementById(\'currentTokenDisplay\').textContent = ',
' token.slice(0, 10) + "..." + token.slice(-10);',
' }',
' }',
' }',
' // 初始化时调用loadSettings',
' loadSettings();',
'</script>',
'</body>',
'</html>'