🏠 返回首頁 

Greasy Fork is available in English.

AI Image Description Generator

使用AI生成网页图片描述


Installer dette script?
  1. // ==UserScript==
  2. // @name AI Image Description Generator
  3. // @namespace http://tampermonkey.net/
  4. // @version 4.2
  5. // @description 使用AI生成网页图片描述
  6. // @author AlphaCat
  7. // @match *://*/*
  8. // @grant GM_xmlhttpRequest
  9. // @grant GM_addStyle
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @grant GM_registerMenuCommand
  13. // @grant GM_setClipboard
  14. // @license MIT
  15. // ==/UserScript==
  16. (function() {
  17. 'use strict';
  18. // 全局变量
  19. let isSelectionMode = false;
  20. // 定义支持的视觉模型列表
  21. const supportedVLModels = [
  22. 'Qwen/Qwen2-VL-72B-Instruct',
  23. 'Pro/Qwen/Qwen2-VL-7B-Instruct',
  24. 'OpenGVLab/InternVL2-Llama3-76B',
  25. 'OpenGVLab/InternVL2-26B',
  26. 'Pro/OpenGVLab/InternVL2-8B',
  27. 'deepseek-ai/deepseek-vl2'
  28. ];
  29. // 定义GLM-4V系列模型
  30. const glm4vModels = [
  31. 'glm-4v',
  32. 'glm-4v-flash'
  33. ];
  34. // 添加样式
  35. GM_addStyle(`
  36. .ai-config-modal {
  37. position: fixed;
  38. top: 50%;
  39. left: 50%;
  40. transform: translate(-50%, -50%);
  41. background: white;
  42. padding: 20px;
  43. border-radius: 8px;
  44. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  45. z-index: 10000;
  46. min-width: 500px;
  47. height: auto;
  48. }
  49. .ai-config-modal h3 {
  50. margin: 0 0 15px 0;
  51. font-size: 14px;
  52. font-weight: bold;
  53. color: #333;
  54. }
  55. .ai-config-modal label {
  56. display: inline-block;
  57. font-size: 12px;
  58. font-weight: bold;
  59. color: #333;
  60. margin: 0;
  61. line-height: normal;
  62. height: auto;
  63. }
  64. .ai-config-modal .input-wrapper {
  65. position: relative;
  66. display: flex;
  67. align-items: center;
  68. }
  69. .ai-config-modal input {
  70. display: block;
  71. width: 100%;
  72. padding: 2px 24px 2px 2px;
  73. margin: 2px;
  74. border: 1px solid #ddd;
  75. border-radius: 4px;
  76. font-size: 13px;
  77. line-height: normal;
  78. height: auto;
  79. box-sizing: border-box;
  80. }
  81. .ai-config-modal .input-icon {
  82. position: absolute;
  83. right: 4px;
  84. width: 16px;
  85. height: 16px;
  86. cursor: pointer;
  87. display: flex;
  88. align-items: center;
  89. justify-content: center;
  90. color: #666;
  91. font-size: 12px;
  92. user-select: none;
  93. }
  94. .ai-config-modal .clear-icon {
  95. right: 24px;
  96. }
  97. .ai-config-modal .toggle-password {
  98. right: 4px;
  99. }
  100. .ai-config-modal .input-icon:hover {
  101. color: #333;
  102. }
  103. .ai-config-modal .input-group {
  104. margin-bottom: 12px;
  105. height: auto;
  106. display: flex;
  107. flex-direction: column;
  108. }
  109. .ai-config-modal .button-row {
  110. display: flex;
  111. gap: 10px;
  112. align-items: center;
  113. margin-top: 5px;
  114. }
  115. .ai-config-modal .check-button {
  116. padding: 4px 8px;
  117. border: none;
  118. border-radius: 4px;
  119. background: #007bff;
  120. color: white;
  121. cursor: pointer;
  122. font-size: 12px;
  123. }
  124. .ai-config-modal .check-button:hover {
  125. background: #0056b3;
  126. }
  127. .ai-config-modal .check-button:disabled {
  128. background: #cccccc;
  129. cursor: not-allowed;
  130. }
  131. .ai-config-modal select {
  132. width: 100%;
  133. padding: 4px;
  134. border: 1px solid #ddd;
  135. border-radius: 4px;
  136. font-size: 13px;
  137. margin-top: 2px;
  138. }
  139. .ai-config-modal .status-text {
  140. font-size: 12px;
  141. margin-left: 10px;
  142. }
  143. .ai-config-modal .status-success {
  144. color: #28a745;
  145. }
  146. .ai-config-modal .status-error {
  147. color: #dc3545;
  148. }
  149. .ai-config-modal button {
  150. margin: 10px 5px;
  151. padding: 8px 15px;
  152. border: none;
  153. border-radius: 4px;
  154. cursor: pointer;
  155. font-size: 14px;
  156. }
  157. .ai-config-modal button#ai-save-config {
  158. background: #4CAF50;
  159. color: white;
  160. }
  161. .ai-config-modal button#ai-cancel-config {
  162. background: #dc3545;
  163. color: white;
  164. }
  165. .ai-config-modal button:hover {
  166. opacity: 0.9;
  167. }
  168. .ai-floating-btn {
  169. position: fixed;
  170. width: 32px;
  171. height: 32px;
  172. background: #4CAF50;
  173. color: white;
  174. border-radius: 50%;
  175. cursor: move;
  176. z-index: 9999;
  177. box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  178. display: flex;
  179. align-items: center;
  180. justify-content: center;
  181. user-select: none;
  182. transition: background-color 0.3s;
  183. }
  184. .ai-floating-btn:hover {
  185. background: #45a049;
  186. }
  187. .ai-floating-btn svg {
  188. width: 20px;
  189. height: 20px;
  190. fill: white;
  191. }
  192. .ai-menu {
  193. position: absolute;
  194. background: white;
  195. border-radius: 5px;
  196. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  197. padding: 8px;
  198. z-index: 10000;
  199. display: flex;
  200. gap: 8px;
  201. }
  202. .ai-menu-item {
  203. width: 32px;
  204. height: 32px;
  205. padding: 6px;
  206. cursor: pointer;
  207. border-radius: 50%;
  208. display: flex;
  209. align-items: center;
  210. justify-content: center;
  211. transition: background-color 0.3s;
  212. }
  213. .ai-menu-item:hover {
  214. background: #f5f5f5;
  215. }
  216. .ai-menu-item svg {
  217. width: 20px;
  218. height: 20px;
  219. fill: #666;
  220. }
  221. .ai-menu-item:hover svg {
  222. fill: #4CAF50;
  223. }
  224. .ai-image-options {
  225. display: flex;
  226. flex-direction: column;
  227. gap: 10px;
  228. margin: 15px 0;
  229. }
  230. .ai-image-options button {
  231. padding: 8px 15px;
  232. border: none;
  233. border-radius: 4px;
  234. background: #4CAF50;
  235. color: white;
  236. cursor: pointer;
  237. transition: background-color 0.3s;
  238. font-size: 14px;
  239. }
  240. .ai-image-options button:hover {
  241. background: #45a049;
  242. }
  243. #ai-cancel {
  244. background: #dc3545;
  245. color: white;
  246. }
  247. #ai-cancel:hover {
  248. opacity: 0.9;
  249. }
  250. .ai-toast {
  251. position: fixed;
  252. top: 20px;
  253. left: 50%;
  254. transform: translateX(-50%);
  255. padding: 10px 20px;
  256. background: rgba(0, 0, 0, 0.8);
  257. color: white;
  258. border-radius: 4px;
  259. font-size: 14px;
  260. z-index: 10000;
  261. animation: fadeInOut 3s ease;
  262. pointer-events: none;
  263. white-space: pre-line;
  264. text-align: center;
  265. max-width: 80%;
  266. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  267. }
  268. @keyframes fadeInOut {
  269. 0% { opacity: 0; transform: translate(-50%, 10px); }
  270. 10% { opacity: 1; transform: translate(-50%, 0); }
  271. 90% { opacity: 1; transform: translate(-50%, 0); }
  272. 100% { opacity: 0; transform: translate(-50%, -10px); }
  273. }
  274. .ai-config-modal .button-group {
  275. display: flex;
  276. justify-content: flex-end;
  277. gap: 10px;
  278. margin-top: 20px;
  279. }
  280. .ai-config-modal .button-group button {
  281. padding: 6px 16px;
  282. border: none;
  283. border-radius: 4px;
  284. cursor: pointer;
  285. font-size: 14px;
  286. transition: background-color 0.2s;
  287. }
  288. .ai-config-modal .save-button {
  289. background: #007bff;
  290. color: white;
  291. }
  292. .ai-config-modal .save-button:hover {
  293. background: #0056b3;
  294. }
  295. .ai-config-modal .save-button:disabled {
  296. background: #cccccc;
  297. cursor: not-allowed;
  298. }
  299. .ai-config-modal .cancel-button {
  300. background: #f8f9fa;
  301. color: #333;
  302. }
  303. .ai-config-modal .cancel-button:hover {
  304. background: #e2e6ea;
  305. }
  306. .ai-selecting-image {
  307. cursor: crosshair !important;
  308. }
  309. .ai-selecting-image * {
  310. cursor: crosshair !important;
  311. }
  312. .ai-image-description {
  313. position: fixed;
  314. background: rgba(0, 0, 0, 0.8);
  315. color: white;
  316. padding: 8px 12px;
  317. border-radius: 4px;
  318. font-size: 14px;
  319. line-height: 1.4;
  320. max-width: 300px;
  321. text-align: center;
  322. word-wrap: break-word;
  323. z-index: 10000;
  324. pointer-events: none;
  325. animation: fadeIn 0.3s ease;
  326. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  327. }
  328. @keyframes fadeIn {
  329. from { opacity: 0; }
  330. to { opacity: 1; }
  331. }
  332. .ai-modal-overlay {
  333. position: fixed;
  334. top: 0;
  335. left: 0;
  336. width: 100%;
  337. height: 100%;
  338. background: rgba(0, 0, 0, 0.5);
  339. display: flex;
  340. justify-content: center;
  341. align-items: center;
  342. z-index: 9999;
  343. }
  344. .ai-r###lt-modal {
  345. position: fixed;
  346. top: 50%;
  347. left: 50%;
  348. transform: translate(-50%, -50%);
  349. background: white;
  350. padding: 20px;
  351. border-radius: 8px;
  352. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  353. z-index: 1000000;
  354. max-width: 80%;
  355. max-height: 80vh;
  356. overflow-y: auto;
  357. }
  358. .ai-r###lt-modal .r###lt-content {
  359. position: relative;
  360. }
  361. .ai-r###lt-modal .description-code {
  362. background: #1e1e1e;
  363. color: #ffffff;
  364. padding: 6px;
  365. border-radius: 4px;
  366. margin: 5px 0;
  367. cursor: pointer;
  368. white-space: pre-line;
  369. word-wrap: break-word;
  370. font-family: monospace;
  371. border: 1px solid #333;
  372. position: relative;
  373. max-height: 500px;
  374. overflow-y: auto;
  375. font-size: 12px;
  376. line-height: 1.2;
  377. }
  378. .ai-r###lt-modal .description-code * {
  379. color: #ffffff !important;
  380. background: transparent !important;
  381. }
  382. .ai-r###lt-modal .description-code code {
  383. display: block;
  384. width: 100%;
  385. white-space: pre-line;
  386. line-height: 1.2;
  387. }
  388. .ai-r###lt-modal .description-code:hover {
  389. background: #2d2d2d;
  390. }
  391. .ai-r###lt-modal .copy-hint {
  392. font-size: 12px;
  393. color: #666;
  394. text-align: center;
  395. margin-top: 5px;
  396. }
  397. .ai-r###lt-modal .close-button {
  398. position: absolute;
  399. top: -10px;
  400. right: -10px;
  401. width: 24px;
  402. height: 24px;
  403. border-radius: 50%;
  404. background: #ff4444;
  405. color: white;
  406. border: none;
  407. cursor: pointer;
  408. display: flex;
  409. align-items: center;
  410. justify-content: center;
  411. font-size: 16px;
  412. line-height: 1;
  413. padding: 0;
  414. }
  415. .ai-r###lt-modal .close-button:hover {
  416. background: #ff6666;
  417. }
  418. .ai-r###lt-modal .balance-info {
  419. font-size: 9px;
  420. color: #666;
  421. text-align: right;
  422. margin-top: 3px;
  423. padding-top: 3px;
  424. border-top: 1px solid #eee;
  425. }
  426. /* 移动端样式优化 */
  427. @media (max-width: 768px) {
  428. .ai-floating-btn {
  429. width: 40px;
  430. height: 40px;
  431. touch-action: none; /* 防止触屏滚动 */
  432. }
  433. .ai-floating-btn svg {
  434. width: 24px;
  435. height: 24px;
  436. }
  437. .ai-config-modal {
  438. width: 90%;
  439. min-width: auto;
  440. max-width: 400px;
  441. padding: 15px;
  442. margin: 10px;
  443. box-sizing: border-box;
  444. }
  445. .ai-config-modal .button-group {
  446. margin-top: 15px;
  447. flex-direction: row;
  448. justify-content: space-between;
  449. gap: 10px;
  450. }
  451. .ai-config-modal .button-group button {
  452. flex: 1;
  453. min-height: 44px; /* 增加按钮高度,更容易点击 */
  454. font-size: 16px;
  455. padding: 10px;
  456. margin: 0;
  457. }
  458. .ai-r###lt-modal {
  459. width: 95%;
  460. min-width: auto;
  461. max-width: 90%;
  462. margin: 10px;
  463. padding: 15px;
  464. }
  465. .ai-modal-overlay {
  466. padding: 10px;
  467. box-sizing: border-box;
  468. }
  469. /* 确保模态框内的所有可点击元素都有足够的点击区域 */
  470. .ai-config-modal button,
  471. .ai-config-modal .input-icon,
  472. .ai-config-modal select,
  473. .ai-config-modal input {
  474. min-height: 44px;
  475. padding: 10px;
  476. font-size: 16px;
  477. }
  478. .ai-config-modal textarea {
  479. min-height: 100px;
  480. font-size: 16px;
  481. padding: 10px;
  482. }
  483. .ai-config-modal .input-icon {
  484. width: 44px;
  485. height: 44px;
  486. font-size: 20px;
  487. }
  488. /* 修复移动端的滚动问题 */
  489. .ai-config-modal {
  490. max-height: 90vh;
  491. overflow-y: auto;
  492. -webkit-overflow-scrolling: touch;
  493. }
  494. }
  495. `);
  496. // 密码显示切换功能
  497. function togglePassword(element) {
  498. const input = element.parentElement.querySelector('input');
  499. if (input.type === 'password') {
  500. input.type = 'text';
  501. element.textContent = '👁️🗨️';
  502. } else {
  503. input.type = 'password';
  504. element.textContent = '👁️';
  505. }
  506. }
  507. // 检查API配置并获取可用模型
  508. async function checkApiAndGetModels(apiEndpoint, apiKey) {
  509. try {
  510. const response = await fetch(`${apiEndpoint}/v1/models`, {
  511. method: 'GET',
  512. headers: {
  513. 'Authorization': `Bearer ${apiKey}`,
  514. 'Content-Type': 'application/json'
  515. }
  516. });
  517. if (!response.ok) {
  518. throw new Error(`HTTP error! status: ${response.status}`);
  519. }
  520. const r###lt = await response.json();
  521. if (r###lt.data && Array.isArray(r###lt.data)) {
  522. // 过滤出多模态模型
  523. const multimodalModels = r###lt.data
  524. .filter(model => model.id.includes('vision') || model.id.includes('gpt-4-v'))
  525. .map(model => ({
  526. id: model.id,
  527. name: model.id
  528. }));
  529. return multimodalModels;
  530. } else {
  531. throw new Error('Invalid response format');
  532. }
  533. } catch (error) {
  534. console.error('Error fetching models:', error);
  535. throw error;
  536. }
  537. }
  538. // 检查API配置
  539. async function checkApiConfig() {
  540. const apiEndpoint = GM_getValue('apiEndpoint', '').trim();
  541. const apiKey = GM_getValue('apiKey', '').trim();
  542. const selectedModel = GM_getValue('selectedModel', '').trim();
  543. if (!apiEndpoint || !apiKey || !selectedModel) {
  544. alert('请先配置API Endpoint、API Key和模型');
  545. showConfigModal();
  546. return false;
  547. }
  548. try {
  549. // 如果是智谱AI的endpoint,跳过API检查
  550. if(apiEndpoint.includes('bigmodel.cn')) {
  551. return true;
  552. }
  553. // 其他endpoint进行API检查
  554. const models = await checkApiAndGetModels(apiEndpoint, apiKey);
  555. if (models.length === 0) {
  556. alert('无法获取可用模型列表,请检查API配置是否正确');
  557. return false;
  558. }
  559. return true;
  560. } catch (error) {
  561. console.error('Error checking API config:', error);
  562. alert('API配置验证失败,请检查配置是否正确');
  563. return false;
  564. }
  565. }
  566. // 获取图片的Base64内容
  567. async function getImageBase64(imageUrl) {
  568. console.log('[Debug] Starting image to Base64 conversion for:', imageUrl);
  569. // 尝试HTTP URL换为HTTPS
  570. if (imageUrl.startsWith('http:')) {
  571. imageUrl = imageUrl.replace('http:', 'https:');
  572. console.log('[Debug] Converted to HTTPS URL:', imageUrl);
  573. }
  574. // 获取图片的多种方法
  575. async function tryFetchImage(method) {
  576. return new Promise((resolve, reject) => {
  577. switch(method) {
  578. case 'direct':
  579. // 直接请求
  580. GM_xmlhttpRequest({
  581. method: 'GET',
  582. url: imageUrl,
  583. responseType: 'blob',
  584. headers: {
  585. 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
  586. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  587. 'Cache-Control': 'no-cache',
  588. 'Pragma': 'no-cache',
  589. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
  590. },
  591. anonymous: true,
  592. onload: response => resolve(response),
  593. onerror: error => reject(error)
  594. });
  595. break;
  596. case 'withReferer':
  597. // 带原始Referer的请求
  598. GM_xmlhttpRequest({
  599. method: 'GET',
  600. url: imageUrl,
  601. responseType: 'blob',
  602. headers: {
  603. 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
  604. 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
  605. 'Cache-Control': 'no-cache',
  606. 'Pragma': 'no-cache',
  607. 'Referer': new URL(imageUrl).origin,
  608. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
  609. },
  610. anonymous: true,
  611. onload: response => resolve(response),
  612. onerror: error => reject(error)
  613. });
  614. break;
  615. case 'proxy':
  616. // 通过代理服务获取
  617. const proxyUrl = `https://images.weserv.nl/?url=${encodeURIComponent(imageUrl)}`;
  618. GM_xmlhttpRequest({
  619. method: 'GET',
  620. url: proxyUrl,
  621. responseType: 'blob',
  622. headers: {
  623. 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
  624. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
  625. },
  626. anonymous: true,
  627. onload: response => resolve(response),
  628. onerror: error => reject(error)
  629. });
  630. break;
  631. case 'corsProxy':
  632. // 通过CORS代理获取
  633. const corsProxyUrl = `https://corsproxy.io/?${encodeURIComponent(imageUrl)}`;
  634. GM_xmlhttpRequest({
  635. method: 'GET',
  636. url: corsProxyUrl,
  637. responseType: 'blob',
  638. headers: {
  639. 'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
  640. 'Origin': window.location.origin
  641. },
  642. anonymous: true,
  643. onload: response => resolve(response),
  644. onerror: error => reject(error)
  645. });
  646. break;
  647. }
  648. });
  649. }
  650. // 处理响应
  651. async function handleResponse(response) {
  652. if (response.status === 200) {
  653. const blob = response.response;
  654. console.log('[Debug] Image blob size:', blob.size, 'bytes');
  655. return new Promise((resolve, reject) => {
  656. const reader = new FileReader();
  657. reader.onloadend = () => {
  658. const base64 = reader.r###lt.split(',')[1];
  659. console.log('[Debug] Base64 conversion completed, length:', base64.length);
  660. resolve(base64);
  661. };
  662. reader.onerror = error => reject(error);
  663. reader.readAsDataURL(blob);
  664. });
  665. }
  666. throw new Error(`Failed with status: ${response.status}`);
  667. }
  668. // 依次尝试不同的方法
  669. const methods = ['direct', 'withReferer', 'proxy', 'corsProxy'];
  670. for (const method of methods) {
  671. try {
  672. console.log(`[Debug] Trying method: ${method}`);
  673. const response = await tryFetchImage(method);
  674. if (response.status === 200) {
  675. return await handleResponse(response);
  676. }
  677. console.log(`[Debug] Method ${method} failed with status:`, response.status);
  678. } catch (error) {
  679. console.log(`[Debug] Method ${method} failed:`, error);
  680. }
  681. }
  682. throw new Error('All methods to fetch image failed');
  683. }
  684. // 调用API获取图片描述
  685. async function getImageDescription(imageUrl, apiEndpoint, apiKey, selectedModel) {
  686. console.log('[Debug] Starting image description request:', {
  687. apiEndpoint,
  688. selectedModel,
  689. imageUrl,
  690. timestamp: new Date().toISOString()
  691. });
  692. try {
  693. // 获取所有API Keys
  694. const apiKeys = apiKey.split('\n').filter(key => key.trim() !== '');
  695. if (apiKeys.length === 0) {
  696. throw new Error('No valid API keys available');
  697. }
  698. // 使用第一个key
  699. const currentKey = apiKeys[0];
  700. const base64Image = await getImageBase64(imageUrl);
  701. console.log('[Debug] Image converted to base64, length:', base64Image.length);
  702. // 退出选择图片模式
  703. exitImageSelectionMode();
  704. const timeout = 30000; // 30秒超时
  705. const controller = new AbortController();
  706. const timeoutId = setTimeout(() => controller.abort(), timeout);
  707. const imageSize = base64Image.length * 0.75; // 转换为字节数
  708. // 获取当前余额
  709. const userInfo = await checkUserInfo(apiEndpoint, currentKey);
  710. const currentBalance = userInfo.totalBalance;
  711. // 计算每次调用的预估花费(根据图片大小和模型)
  712. const costPerCall = calculateCost(imageSize, selectedModel);
  713. // 计算识别的剩余图片量
  714. const remainingImages = Math.floor(currentBalance / costPerCall);
  715. // 根据不同的API构建不同的请求体和endpoint
  716. let requestBody;
  717. let finalEndpoint;
  718. if(selectedModel.startsWith('glm-')) {
  719. // GLM系列模型的请求格式
  720. requestBody = {
  721. model: selectedModel,
  722. messages: [{
  723. role: "user",
  724. content: [{
  725. type: "text",
  726. text: "请描述这张图片的主要内容。如果是人物图片,请至少用15个字描述人物。"
  727. }, {
  728. type: "image_url",
  729. image_url: {
  730. url: `data:image/jpeg;base64,${base64Image}`
  731. }
  732. }]
  733. }],
  734. stream: true
  735. };
  736. finalEndpoint = 'https://open.bigmodel.cn/api/paas/v4/chat/completions';
  737. } else {
  738. // 原有模型的请求格式
  739. requestBody = {
  740. model: selectedModel,
  741. messages: [{
  742. role: "user",
  743. content: [
  744. {
  745. type: "image_url",
  746. image_url: {
  747. url: `data:image/jpeg;base64,${base64Image}`
  748. }
  749. },
  750. {
  751. type: "text",
  752. text: "Describe the main content of the image. If there is a person, provide a description of the person with some beautiful words. Answer in Chinese."
  753. }
  754. ]
  755. }],
  756. stream: true
  757. };
  758. finalEndpoint = `${apiEndpoint}/chat/completions`;
  759. }
  760. console.log('[Debug] API Request body:', JSON.stringify(requestBody, null, 2));
  761. console.log('[Debug] Sending request to:', finalEndpoint);
  762. console.log('[Debug] Request headers:', {
  763. 'Authorization': 'Bearer ***' + currentKey.slice(-4),
  764. 'Content-Type': 'application/json'
  765. });
  766. console.log('[Debug] Request body:', requestBody);
  767. return new Promise((resolve, reject) => {
  768. GM_xmlhttpRequest({
  769. method: 'POST',
  770. url: finalEndpoint,
  771. headers: {
  772. 'Authorization': `Bearer ${currentKey}`,
  773. 'Content-Type': 'application/json'
  774. },
  775. data: JSON.stringify(requestBody),
  776. onload: async function(response) {
  777. console.log('[Debug] Response received:', {
  778. status: response.status,
  779. statusText: response.statusText,
  780. headers: response.responseHeaders
  781. });
  782. if (response.status === 200) {
  783. try {
  784. let description = '';
  785. const lines = response.responseText.split('\n').filter(line => line.trim() !== '');
  786. for (const line of lines) {
  787. if (line.startsWith('data: ')) {
  788. const jsonStr = line.slice(6);
  789. if (jsonStr === '[DONE]') continue;
  790. try {
  791. const jsonData = JSON.parse(jsonStr);
  792. console.log('[Debug] Parsed chunk:', jsonData);
  793. const content = jsonData.choices[0]?.delta?.content;
  794. if (content) {
  795. description += content;
  796. console.log('[Debug] Current description:', description);
  797. }
  798. } catch (e) {
  799. console.error('[Debug] Error parsing chunk JSON:', e);
  800. }
  801. }
  802. }
  803. console.log('[Debug] Final description:', description);
  804. removeDescriptionTooltip();
  805. const balanceInfo = `剩余额度为:${currentBalance.toFixed(4)},大约还可以识别 ${remainingImages} 张图片`;
  806. showDescriptionModal(description, balanceInfo);
  807. resolve(description);
  808. } catch (error) {
  809. console.error('[Debug] Error processing response:', error);
  810. reject(error);
  811. }
  812. } else {
  813. console.error('[Debug] Error response:', {
  814. status: response.status,
  815. statusText: response.statusText,
  816. response: response.responseText
  817. });
  818. // 检查是否是余额不足错误
  819. try {
  820. const errorResponse = JSON.parse(response.responseText);
  821. if (errorResponse.code === 30001 ||
  822. (errorResponse.message && errorResponse.message.includes('insufficient'))) {
  823. showToast('当前key余不足,正在检测其他key...');
  824. // 自动运行一次key检测
  825. await checkAndUpdateKeys();
  826. // 重新获取更新后的key
  827. const newApiKeys = GM_getValue('apiKey', '').split('\n').filter(key => key.trim() !== '');
  828. if (newApiKeys.length > 0) {
  829. // 使用新的key重试
  830. getImageDescription(imageUrl, apiEndpoint, newApiKeys.join('\n'), selectedModel)
  831. .then(resolve)
  832. .catch(reject);
  833. return;
  834. }
  835. }
  836. } catch (e) {
  837. console.error('[Debug] Error parsing error response:', e);
  838. }
  839. reject(new Error(`Request failed with status ${response.status}`));
  840. }
  841. },
  842. onerror: function(error) {
  843. console.error('[Debug] Request error:', error);
  844. reject(error);
  845. },
  846. onprogress: function(progress) {
  847. // 用于处理流式响应的进度
  848. console.log('[Debug] Progress:', progress);
  849. try {
  850. const lines = progress.responseText.split('\n').filter(line => line.trim() !== '');
  851. let latestContent = '';
  852. for (const line of lines) {
  853. if (line.startsWith('data: ')) {
  854. const jsonStr = line.slice(6);
  855. if (jsonStr === '[DONE]') continue;
  856. try {
  857. const jsonData = JSON.parse(jsonStr);
  858. const content = jsonData.choices[0]?.delta?.content;
  859. if (content) {
  860. latestContent += content;
  861. }
  862. } catch (e) {
  863. console.error('[Debug] Error parsing progress JSON:', e);
  864. }
  865. }
  866. }
  867. if (latestContent) {
  868. updateDescriptionTooltip('正在生成描述: ' + latestContent);
  869. }
  870. } catch (error) {
  871. console.error('[Debug] Error processing progress:', error);
  872. }
  873. }
  874. });
  875. });
  876. } catch (error) {
  877. if (error.name === 'AbortError') {
  878. showToast('请求超时,请重试');
  879. }
  880. removeDescriptionTooltip();
  881. console.error('[Debug] Error in getImageDescription:', {
  882. error,
  883. stack: error.stack,
  884. timestamp: new Date().toISOString()
  885. });
  886. throw error;
  887. }
  888. }
  889. // 显示描述tooltip
  890. function showDescriptionTooltip(description) {
  891. const tooltip = document.createElement('div');
  892. tooltip.className = 'ai-image-description';
  893. tooltip.textContent = description;
  894. // 获取视口宽度
  895. const viewportWidth = window.innerWidth || document.documentElement.clientWidth;
  896. // 计算tooltip位置(水平居中,距顶部20px��
  897. const tooltipX = Math.max(0, (viewportWidth - 300) / 2); // 300是tooltip的max-width
  898. tooltip.style.position = 'fixed';
  899. tooltip.style.left = `${tooltipX}px`;
  900. tooltip.style.top = '20px';
  901. document.body.appendChild(tooltip);
  902. // 添加动态点的动画
  903. let dots = 1;
  904. const updateInterval = setInterval(() => {
  905. if (!document.body.contains(tooltip)) {
  906. clearInterval(updateInterval);
  907. return;
  908. }
  909. dots = dots % 6 + 1;
  910. tooltip.textContent = '正在生成描述' + '.'.repeat(dots);
  911. }, 500); // 每500ms更新一次
  912. return tooltip;
  913. }
  914. // 更新描述tooltip内容
  915. function updateDescriptionTooltip(description) {
  916. const tooltip = document.querySelector('.ai-image-description');
  917. if (tooltip) {
  918. tooltip.textContent = description;
  919. }
  920. }
  921. // 移除描述tooltip
  922. function removeDescriptionTooltip() {
  923. const tooltip = document.querySelector('.ai-image-description');
  924. if (tooltip) {
  925. tooltip.remove();
  926. }
  927. }
  928. // 在全局变量部分添加日志函数
  929. function log(message, data = null) {
  930. const timestamp = new Date().toISOString();
  931. if (data) {
  932. console.log(`[AI Image] ${timestamp} ${message}:`, data);
  933. } else {
  934. console.log(`[AI Image] ${timestamp} ${message}`);
  935. }
  936. }
  937. // 修改 findImage 函数,增强图片元素检测能力
  938. function findImage(target) {
  939. let img = null;
  940. let imgSrc = null;
  941. // 检查是否为图片元素
  942. if (target.nodeName === 'IMG') {
  943. img = target;
  944. // 优先获取 data-src(懒加载原图)
  945. imgSrc = target.getAttribute('data-src') ||
  946. target.getAttribute('data-original') ||
  947. target.getAttribute('data-actualsrc') ||
  948. target.getAttribute('data-url') ||
  949. target.getAttribute('data-echo') ||
  950. target.getAttribute('data-lazy-src') ||
  951. target.getAttribute('data-original-src') ||
  952. target.src; // 最后才使用 src 属性
  953. }
  954. // 检查背景图
  955. else if (target.style && target.style.backgroundImage) {
  956. let bgImg = target.style.backgroundImage.match(/url\(['"]?([^'"]+)['"]?\)/);
  957. if (bgImg) {
  958. imgSrc = bgImg[1];
  959. img = target;
  960. }
  961. }
  962. // 检查父元素的背景图
  963. else {
  964. let parent = target.parentElement;
  965. if (parent && parent.style && parent.style.backgroundImage) {
  966. let bgImg = parent.style.backgroundImage.match(/url\(['"]?([^'"]+)['"]?\)/);
  967. if (bgImg) {
  968. imgSrc = bgImg[1];
  969. img = parent;
  970. }
  971. }
  972. }
  973. // 检查常见的图片容器
  974. if (!img) {
  975. // 检查父元素是否为图片容器
  976. let imgWrapper = target.closest('[class*="img"],[class*="photo"],[class*="image"],[class*="thumb"],[class*="avatar"],[class*="masonry"]');
  977. if (imgWrapper) {
  978. // 在容器中查找图片元素
  979. let possibleImg = imgWrapper.querySelector('img');
  980. if (possibleImg) {
  981. img = possibleImg;
  982. // 同样优先获取懒加载原图
  983. imgSrc = possibleImg.getAttribute('data-src') ||
  984. possibleImg.getAttribute('data-original') ||
  985. possibleImg.getAttribute('data-actualsrc') ||
  986. possibleImg.getAttribute('data-url') ||
  987. possibleImg.getAttribute('data-echo') ||
  988. possibleImg.getAttribute('data-lazy-src') ||
  989. possibleImg.getAttribute('data-original-src') ||
  990. possibleImg.src;
  991. } else {
  992. // 检查容器的背景图
  993. let bgImg = getComputedStyle(imgWrapper).backgroundImage.match(/url\(['"]?([^'"]+)['"]?\)/);
  994. if (bgImg) {
  995. imgSrc = bgImg[1];
  996. img = imgWrapper;
  997. }
  998. }
  999. }
  1000. }
  1001. // 检查特殊情况:某些网站使用自定义属性存储真实图片地址
  1002. if (img && !imgSrc) {
  1003. // 获取元素的所有属性
  1004. const attrs = img.attributes;
  1005. for (let i = 0; i < attrs.length; i++) {
  1006. const attr = attrs[i];
  1007. // 检查属性名中是否包含关键字
  1008. if (attr.name.toLowerCase().includes('src') ||
  1009. attr.name.toLowerCase().includes('url') ||
  1010. attr.name.toLowerCase().includes('img') ||
  1011. attr.name.toLowerCase().includes('thumb') ||
  1012. attr.name.toLowerCase().includes('original') ||
  1013. attr.name.toLowerCase().includes('data')) {
  1014. const value = attr.value;
  1015. if (value && /^https?:\/\//.test(value)) {
  1016. imgSrc = value;
  1017. break;
  1018. }
  1019. }
  1020. }
  1021. }
  1022. // 检查父级链接
  1023. if (img && !imgSrc) {
  1024. let parentLink = img.closest('a');
  1025. if (parentLink && parentLink.href) {
  1026. if (/\.(jpe?g|png|webp|gif)$/i.test(parentLink.href)) {
  1027. imgSrc = parentLink.href;
  1028. }
  1029. }
  1030. }
  1031. // 如果找到了图片但没有找到有效的 URL,记录日志
  1032. if (img && !imgSrc) {
  1033. log('找到图片元素但未找到有效的图片URL', {
  1034. element: img,
  1035. attributes: Array.from(img.attributes).map(attr => `${attr.name}="${attr.value}"`).join(', ')
  1036. });
  1037. }
  1038. return { img, imgSrc };
  1039. }
  1040. // 修改点击处理函数
  1041. function clickHandler(e) {
  1042. if (!isSelectionMode) return;
  1043. const { img, imgSrc } = findImage(e.target);
  1044. if (!img || !imgSrc) return;
  1045. e.preventDefault();
  1046. e.stopPropagation();
  1047. // 检查图片是否有效
  1048. if (img instanceof HTMLImageElement) {
  1049. if (!img.complete || !img.naturalWidth) {
  1050. showToast('图片未加载完成或无效');
  1051. return;
  1052. }
  1053. if (img.naturalWidth < 10 || img.naturalHeight < 10) {
  1054. showToast('图片太小,无法处理');
  1055. return;
  1056. }
  1057. }
  1058. // 开始处理图片
  1059. getImageDescription(imgSrc);
  1060. }
  1061. // 进入图片选择模式
  1062. function enterImageSelectionMode() {
  1063. console.log('[Debug] Entering image selection mode');
  1064. if(isSelectionMode) return; // 防止重复进入选择模式
  1065. isSelectionMode = true;
  1066. // 隐藏悬浮按钮
  1067. const floatingBtn = document.querySelector('.ai-floating-btn');
  1068. if(floatingBtn) {
  1069. floatingBtn.style.display = 'none';
  1070. }
  1071. // 创建遮罩层
  1072. const overlay = document.createElement('div');
  1073. overlay.className = 'ai-selection-overlay';
  1074. document.body.appendChild(overlay);
  1075. // 添加选择状态的类名
  1076. document.body.classList.add('ai-selecting-image');
  1077. // 创建点击事件处理函数
  1078. const clickHandler = async function(e) {
  1079. if (!isSelectionMode) return;
  1080. if (e.target.tagName === 'IMG') {
  1081. console.log('[Debug] Image clicked:', e.target.src);
  1082. e.preventDefault();
  1083. e.stopPropagation();
  1084. // 获取配置
  1085. const endpoint = GM_getValue('apiEndpoint', '');
  1086. const apiKey = GM_getValue('apiKey', '');
  1087. const selectedModel = GM_getValue('selectedModel', '');
  1088. console.log('[Debug] Current configuration:', {
  1089. endpoint,
  1090. selectedModel,
  1091. hasApiKey: !!apiKey
  1092. });
  1093. if (!endpoint || !apiKey || !selectedModel) {
  1094. showToast('请先配置API配置');
  1095. exitImageSelectionMode();
  1096. return;
  1097. }
  1098. // 显示加载中的tooltip
  1099. showDescriptionTooltip('正在生成描述...');
  1100. try {
  1101. await getImageDescription(e.target.src, endpoint, apiKey, selectedModel);
  1102. } catch (error) {
  1103. console.error('[Debug] Description generation failed:', error);
  1104. removeDescriptionTooltip();
  1105. showToast('生成描述失败: ' + error.message);
  1106. }
  1107. }
  1108. };
  1109. // 添加点击事件监听器
  1110. document.addEventListener('click', clickHandler, true);
  1111. // ESC键退选择模式
  1112. const escHandler = (e) => {
  1113. if (e.key === 'Escape') {
  1114. exitImageSelectionMode();
  1115. }
  1116. };
  1117. document.addEventListener('keydown', escHandler);
  1118. // 保存事件理函数以便后续移除
  1119. window._imageSelectionHandlers = {
  1120. click: clickHandler,
  1121. keydown: escHandler
  1122. };
  1123. }
  1124. // 退出图片选择模式
  1125. function exitImageSelectionMode() {
  1126. console.log('[Debug] Exiting image selection mode');
  1127. isSelectionMode = false;
  1128. // 显示悬浮按钮
  1129. const floatingBtn = document.querySelector('.ai-floating-btn');
  1130. if(floatingBtn) {
  1131. floatingBtn.style.display = 'flex';
  1132. }
  1133. // 移除遮罩层
  1134. const overlay = document.querySelector('.ai-selection-overlay');
  1135. if (overlay) {
  1136. overlay.remove();
  1137. }
  1138. // 移除选择状态的类名
  1139. document.body.classList.remove('ai-selecting-image');
  1140. // 移除所有事件监听器
  1141. if (window._imageSelectionHandlers) {
  1142. document.removeEventListener('click', window._imageSelectionHandlers.click, true);
  1143. document.removeEventListener('keydown', window._imageSelectionHandlers.keydown);
  1144. window._imageSelectionHandlers = null;
  1145. }
  1146. }
  1147. // 显示toast提示
  1148. function showToast(message, duration = 3000) {
  1149. const toast = document.createElement('div');
  1150. toast.className = 'ai-toast';
  1151. toast.textContent = message;
  1152. document.body.appendChild(toast);
  1153. setTimeout(() => {
  1154. toast.remove();
  1155. }, duration);
  1156. }
  1157. // 检查用户信息
  1158. async function checkUserInfo(apiEndpoint, apiKey) {
  1159. try {
  1160. // 对谱AI的endpoint返回默认值
  1161. if(apiEndpoint.includes('bigmodel.cn')) {
  1162. const defaultUserData = {
  1163. name: 'GLM User',
  1164. balance: 1000, // 默认余额
  1165. chargeBalance: 0,
  1166. totalBalance: 1000
  1167. };
  1168. console.log('[Debug] Using default user data for GLM:', defaultUserData);
  1169. return defaultUserData;
  1170. }
  1171. // 其他endpoint使用原有逻辑
  1172. return new Promise((resolve, reject) => {
  1173. console.log('[Debug] Sending user info request to:', `${apiEndpoint}/v1/user/info`);
  1174. GM_xmlhttpRequest({
  1175. method: 'GET',
  1176. url: `${apiEndpoint}/v1/user/info`,
  1177. headers: {
  1178. 'Authorization': `Bearer ${apiKey}`,
  1179. 'Content-Type': 'application/json'
  1180. },
  1181. onload: function(response) {
  1182. console.log('[Debug] User Info Raw Response:', {
  1183. status: response.status,
  1184. statusText: response.statusText,
  1185. responseText: response.responseText,
  1186. headers: response.responseHeaders
  1187. });
  1188. if (response.status === 200) {
  1189. try {
  1190. const r###lt = JSON.parse(response.responseText);
  1191. console.log('[Debug] User Info Parsed Response:', r###lt);
  1192. if (r###lt.code === 20000 && r###lt.status && r###lt.data) {
  1193. const { name, balance, chargeBalance, totalBalance } = r###lt.data;
  1194. resolve({
  1195. name,
  1196. balance: parseFloat(balance),
  1197. chargeBalance: parseFloat(chargeBalance),
  1198. totalBalance: parseFloat(totalBalance)
  1199. });
  1200. } else {
  1201. throw new Error(r###lt.message || 'Invalid response format');
  1202. }
  1203. } catch (error) {
  1204. console.error('[Debug] JSON Parse Error:', error);
  1205. reject(error);
  1206. }
  1207. } else {
  1208. console.error('[Debug] HTTP Error Response:', {
  1209. status: response.status,
  1210. statusText: response.statusText,
  1211. response: response.responseText
  1212. });
  1213. reject(new Error(`HTTP error! status: ${response.status}`));
  1214. }
  1215. },
  1216. onerror: function(error) {
  1217. console.error('[Debug] Request Error:', error);
  1218. reject(error);
  1219. }
  1220. });
  1221. });
  1222. } catch (error) {
  1223. console.error('[Debug] User Info Error:', error);
  1224. throw error;
  1225. }
  1226. }
  1227. // 获取可用模型列表
  1228. async function getAvailableModels(apiEndpoint, apiKey) {
  1229. console.log('[Debug] Getting available models from:', apiEndpoint);
  1230. try {
  1231. // 如果是智谱AI的endpoint,直接返回GLM模型列表
  1232. if(apiEndpoint.includes('bigmodel.cn')) {
  1233. const glmModels = [
  1234. {
  1235. id: 'glm-4',
  1236. name: 'GLM-4'
  1237. },
  1238. {
  1239. id: 'glm-4v',
  1240. name: 'GLM-4V'
  1241. },
  1242. {
  1243. id: 'glm-4v-flash',
  1244. name: 'GLM-4V-Flash'
  1245. }
  1246. ];
  1247. console.log('[Debug] Available GLM models:', glmModels);
  1248. return glmModels;
  1249. }
  1250. // 其他endpoint使用原有逻辑
  1251. return new Promise((resolve, reject) => {
  1252. console.log('[Debug] Sending models request to:', `${apiEndpoint}/v1/models`);
  1253. GM_xmlhttpRequest({
  1254. method: 'GET',
  1255. url: `${apiEndpoint}/v1/models`,
  1256. headers: {
  1257. 'Authorization': `Bearer ${apiKey}`,
  1258. 'Content-Type': 'application/json'
  1259. },
  1260. onload: function(response) {
  1261. console.log('[Debug] Models API Raw Response:', {
  1262. status: response.status,
  1263. statusText: response.statusText,
  1264. responseText: response.responseText,
  1265. headers: response.responseHeaders
  1266. });
  1267. if (response.status === 200) {
  1268. try {
  1269. const r###lt = JSON.parse(response.responseText);
  1270. console.log('[Debug] Models API Parsed Response:', r###lt);
  1271. if (r###lt.object === 'list' && Array.isArray(r###lt.data)) {
  1272. const models = r###lt.data
  1273. .filter(model => supportedVLModels.includes(model.id))
  1274. .map(model => ({
  1275. id: model.id,
  1276. name: model.id.split('/').pop()
  1277. .replace('Qwen2-VL-', 'Qwen2-')
  1278. .replace('InternVL2-Llama3-', 'InternVL2-')
  1279. .replace('-Instruct', '')
  1280. }));
  1281. console.log('[Debug] Filtered and processed models:', models);
  1282. resolve(models);
  1283. } else {
  1284. console.error('[Debug] Invalid models response format:', r###lt);
  1285. reject(new Error('Invalid models response format'));
  1286. }
  1287. } catch (error) {
  1288. console.error('[Debug] JSON Parse Error:', error);
  1289. reject(error);
  1290. }
  1291. } else {
  1292. console.error('[Debug] HTTP Error Response:', {
  1293. status: response.status,
  1294. statusText: response.statusText,
  1295. response: response.responseText
  1296. });
  1297. reject(new Error(`HTTP error! status: ${response.status}`));
  1298. }
  1299. },
  1300. onerror: function(error) {
  1301. console.error('[Debug] Models API Request Error:', error);
  1302. reject(error);
  1303. }
  1304. });
  1305. });
  1306. } catch (error) {
  1307. console.error('[Debug] Models API Error:', error);
  1308. throw error;
  1309. }
  1310. }
  1311. // 更新模型拉菜单
  1312. function updateModelSelect(selectElement, models) {
  1313. if (models.length === 0) {
  1314. selectElement.innerHTML = '<option value="">未找到可用的视觉模型</option>';
  1315. selectElement.disabled = true;
  1316. return;
  1317. }
  1318. selectElement.innerHTML = '<option value="">请选择视觉模型</option>' +
  1319. models.map(model =>
  1320. `<option value="${model.id}" title="${model.id}">${model.name}</option>`
  1321. ).join('');
  1322. selectElement.disabled = false;
  1323. }
  1324. // 保存模型列表到GM存储
  1325. function saveModelList(models) {
  1326. GM_setValue('availableModels', models);
  1327. }
  1328. // 从GM存储获取模型列表
  1329. function getStoredModelList() {
  1330. return GM_getValue('availableModels', []);
  1331. }
  1332. // 创建悬浮按钮
  1333. function createFloatingButton() {
  1334. const btn = document.createElement('div');
  1335. btn.className = 'ai-floating-btn';
  1336. btn.innerHTML = `
  1337. <svg viewBox="0 0 24 24">
  1338. <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.42 0-8-3.58-8-8s3.58-8 8-8 8 3.58 8 8-3.58 8-8 8zm0-14c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm0 10c-2.21 0-4-1.79-4-4h2c0-1.1.9-2 2-2s2 .9 2 2c0 2-3 1.75-3 5h2c0-2.25 3-2.5 3-5 0-2.21-1.79-4-4-4z"/>
  1339. </svg>
  1340. `;
  1341. // 设置初始位置
  1342. const savedPos = JSON.parse(GM_getValue('btnPosition', '{"x": 20, "y": 20}'));
  1343. btn.style.left = (savedPos.x || 20) + 'px';
  1344. btn.style.top = (savedPos.y || 20) + 'px';
  1345. btn.style.right = 'auto';
  1346. btn.style.bottom = 'auto';
  1347. // 自动检测key的可用性
  1348. setTimeout(async () => {
  1349. await checkAndUpdateKeys();
  1350. }, 1000);
  1351. let isDragging = false;
  1352. let hasMoved = false;
  1353. let startX, startY;
  1354. let initialLeft, initialTop;
  1355. let longPressTimer;
  1356. let touchStartTime;
  1357. // 触屏事件处理
  1358. btn.addEventListener('touchstart', function(e) {
  1359. e.preventDefault();
  1360. touchStartTime = Date.now();
  1361. // 置长按定时器
  1362. longPressTimer = setTimeout(() => {
  1363. exitImageSelectionMode();
  1364. createConfigUI();
  1365. }, 500); // 500ms长按触发
  1366. const touch = e.touches[0];
  1367. startX = touch.clientX;
  1368. startY = touch.clientY;
  1369. const rect = btn.getBoundingClientRect();
  1370. initialLeft = rect.left;
  1371. initialTop = rect.top;
  1372. });
  1373. btn.addEventListener('touchmove', function(e) {
  1374. e.preventDefault();
  1375. clearTimeout(longPressTimer); // 移动时取消长按
  1376. const touch = e.touches[0];
  1377. const deltaX = touch.clientX - startX;
  1378. const deltaY = touch.clientY - startY;
  1379. if (Math.abs(deltaX) > 5 || Math.abs(deltaY) > 5) {
  1380. hasMoved = true;
  1381. }
  1382. const newLeft = Math.max(0, Math.min(window.innerWidth - btn.offsetWidth, initialLeft + deltaX));
  1383. const newTop = Math.max(0, Math.min(window.innerHeight - btn.offsetHeight, initialTop + deltaY));
  1384. btn.style.left = newLeft + 'px';
  1385. btn.style.top = newTop + 'px';
  1386. });
  1387. btn.addEventListener('touchend', function(e) {
  1388. e.preventDefault();
  1389. clearTimeout(longPressTimer);
  1390. const touchDuration = Date.now() - touchStartTime;
  1391. if (!hasMoved && touchDuration < 500) {
  1392. // 短按进入图片选择模式
  1393. enterImageSelectionMode();
  1394. }
  1395. if (hasMoved) {
  1396. // 保存新位置
  1397. const rect = btn.getBoundingClientRect();
  1398. GM_setValue('btnPosition', JSON.stringify({
  1399. x: rect.left,
  1400. y: rect.top
  1401. }));
  1402. }
  1403. hasMoved = false;
  1404. });
  1405. // 保留原有的鼠标事件处理
  1406. btn.addEventListener('click', function(e) {
  1407. if (e.button === 0 && !hasMoved) { // 左键点击且没有移动
  1408. enterImageSelectionMode();
  1409. e.stopPropagation();
  1410. }
  1411. hasMoved = false;
  1412. });
  1413. btn.addEventListener('contextmenu', function(e) {
  1414. e.preventDefault();
  1415. exitImageSelectionMode();
  1416. createConfigUI();
  1417. });
  1418. // 拖拽相关事件
  1419. function dragStart(e) {
  1420. if (e.target === btn || btn.contains(e.target)) {
  1421. isDragging = true;
  1422. hasMoved = false;
  1423. const rect = btn.getBoundingClientRect();
  1424. startX = e.clientX;
  1425. startY = e.clientY;
  1426. initialLeft = rect.left;
  1427. initialTop = rect.top;
  1428. e.preventDefault();
  1429. }
  1430. }
  1431. function drag(e) {
  1432. if (isDragging) {
  1433. e.preventDefault();
  1434. const deltaX = e.clientX - startX;
  1435. const deltaY = e.clientY - startY;
  1436. if (Math.abs(deltaX) > 5 || Math.abs(deltaY) > 5) {
  1437. hasMoved = true;
  1438. }
  1439. const newLeft = Math.max(0, Math.min(window.innerWidth - btn.offsetWidth, initialLeft + deltaX));
  1440. const newTop = Math.max(0, Math.min(window.innerHeight - btn.offsetHeight, initialTop + deltaY));
  1441. btn.style.left = newLeft + 'px';
  1442. btn.style.top = newTop + 'px';
  1443. }
  1444. }
  1445. function dragEnd(e) {
  1446. if (isDragging) {
  1447. isDragging = false;
  1448. const rect = btn.getBoundingClientRect();
  1449. GM_setValue('btnPosition', JSON.stringify({
  1450. x: rect.left,
  1451. y: rect.top
  1452. }));
  1453. }
  1454. }
  1455. btn.addEventListener('mousedown', dragStart);
  1456. document.addEventListener('mousemove', drag);
  1457. document.addEventListener('mouseup', dragEnd);
  1458. // 将按钮添加到文档中
  1459. document.body.appendChild(btn);
  1460. return btn;
  1461. }
  1462. // 检查并更新key列表
  1463. async function checkAndUpdateKeys() {
  1464. const endpoint = GM_getValue('apiEndpoint', '');
  1465. const apiKeys = GM_getValue('apiKey', '').split('\n').filter(key => key.trim() !== '');
  1466. if (endpoint && apiKeys.length > 0) {
  1467. const validKeys = [];
  1468. const keyBalances = new Map();
  1469. for (const apiKey of apiKeys) {
  1470. try {
  1471. const userInfo = await checkUserInfo(endpoint, apiKey);
  1472. if (userInfo.totalBalance > 0) {
  1473. validKeys.push(apiKey);
  1474. keyBalances.set(apiKey, userInfo.totalBalance);
  1475. } else {
  1476. showToast(`${apiKey.slice(0, 8)}...可用余额为0,被移除。`);
  1477. }
  1478. } catch (error) {
  1479. console.error('Key check failed:', error);
  1480. }
  1481. }
  1482. // 按余额从小到大排序
  1483. validKeys.sort((a, b) => keyBalances.get(a) - keyBalances.get(b));
  1484. // 更新存储的key
  1485. if (validKeys.length > 0) {
  1486. GM_setValue('apiKey', validKeys.join('\n'));
  1487. showToast(`自动检测完成,${validKeys.length}个有效key`);
  1488. } else {
  1489. showToast('没有可用的API Key,请更新配置');
  1490. }
  1491. }
  1492. }
  1493. // 创建配置界面
  1494. function createConfigUI() {
  1495. // 如果已经存在配置界面,先移除
  1496. const existingModal = document.querySelector('.ai-modal-overlay');
  1497. if (existingModal) {
  1498. existingModal.remove();
  1499. }
  1500. const overlay = document.createElement('div');
  1501. overlay.className = 'ai-modal-overlay';
  1502. const modal = document.createElement('div');
  1503. modal.className = 'ai-config-modal';
  1504. modal.innerHTML = `
  1505. <h3>AI图像描述配置</h3>
  1506. <div class="input-group">
  1507. <label>API Endpoint:</label>
  1508. <div class="input-wrapper">
  1509. <input type="text" id="ai-endpoint" placeholder="https://api.openai.com" value="${GM_getValue('apiEndpoint', '')}">
  1510. <span class="input-icon clear-icon" title="清空">✕</span>
  1511. </div>
  1512. </div>
  1513. <div class="input-group">
  1514. <label>API Key (每行一个):</label>
  1515. <div class="input-wrapper">
  1516. <textarea id="ai-apikey" rows="5" style="width: 100%; resize: vertical;">${GM_getValue('apiKey', '')}</textarea>
  1517. <span class="input-icon clear-icon" title="清空">✕</span>
  1518. </div>
  1519. <div class="button-row">
  1520. <button class="check-button" id="check-api">检测可用性</button>
  1521. </div>
  1522. </div>
  1523. <div class="input-group">
  1524. <label>可用模型:</label>
  1525. <select id="ai-model">
  1526. <option value="">加载中...</option>
  1527. </select>
  1528. </div>
  1529. <div class="button-group">
  1530. <button type="button" class="cancel-button" id="ai-cancel-config">取消</button>
  1531. <button type="button" class="save-button" id="ai-save-config">保存</button>
  1532. </div>
  1533. `;
  1534. overlay.appendChild(modal);
  1535. document.body.appendChild(overlay);
  1536. // 初始化模型下拉菜单
  1537. const modelSelect = modal.querySelector('#ai-model');
  1538. const storedModels = getStoredModelList();
  1539. const selectedModel = GM_getValue('selectedModel', '');
  1540. if (storedModels.length > 0) {
  1541. updateModelSelect(modelSelect, storedModels);
  1542. if (selectedModel) {
  1543. modelSelect.value = selectedModel;
  1544. }
  1545. } else {
  1546. modelSelect.innerHTML = '<option value="">请先检测API可用性</option>';
  1547. modelSelect.disabled = true;
  1548. }
  1549. // 添加清空按钮事件
  1550. const clearButtons = modal.querySelectorAll('.clear-icon');
  1551. clearButtons.forEach(button => {
  1552. button.addEventListener('click', function(e) {
  1553. const input = this.parentElement.querySelector('input, textarea');
  1554. if (input) {
  1555. input.value = '';
  1556. input.focus();
  1557. }
  1558. });
  1559. });
  1560. // 检测API可用性
  1561. const checkButton = modal.querySelector('#check-api');
  1562. if (checkButton) {
  1563. checkButton.addEventListener('click', async function() {
  1564. const endpoint = modal.querySelector('#ai-endpoint')?.value?.trim() || '';
  1565. const apiKeys = modal.querySelector('#ai-apikey')?.value?.trim().split('\n').filter(key => key.trim() !== '') || [];
  1566. if (!endpoint || apiKeys.length === 0) {
  1567. showToast('请先填写API Endpoint和至少一个API Key');
  1568. return;
  1569. }
  1570. checkButton.disabled = true;
  1571. modelSelect.disabled = true;
  1572. modelSelect.innerHTML = '<option value="">检测中...</option>';
  1573. try {
  1574. // 检查每个key的可用性
  1575. const validKeys = [];
  1576. const keyBalances = new Map();
  1577. for (const apiKey of apiKeys) {
  1578. try {
  1579. const userInfo = await checkUserInfo(endpoint, apiKey);
  1580. if (userInfo.totalBalance > 0) {
  1581. validKeys.push(apiKey);
  1582. keyBalances.set(apiKey, userInfo.totalBalance);
  1583. } else {
  1584. showToast(`${apiKey.slice(0, 8)}...可用余额为0,被移除。`);
  1585. }
  1586. } catch (error) {
  1587. console.error('Key check failed:', error);
  1588. showToast(`${apiKey.slice(0, 8)}...验证失败,被移除。`);
  1589. }
  1590. }
  1591. // 按余额从小到大排序
  1592. validKeys.sort((a, b) => keyBalances.get(a) - keyBalances.get(b));
  1593. // 更新输入框中的key
  1594. const apiKeyInput = modal.querySelector('#ai-apikey');
  1595. if (apiKeyInput) {
  1596. apiKeyInput.value = validKeys.join('\n');
  1597. }
  1598. // 获取可用模型列表(使用第一个有效的key)
  1599. if (validKeys.length > 0) {
  1600. const models = await getAvailableModels(endpoint, validKeys[0]);
  1601. saveModelList(models);
  1602. updateModelSelect(modelSelect, models);
  1603. showToast(`检测完成,${validKeys.length}个有效key`);
  1604. } else {
  1605. showToast('没有可用的API Key');
  1606. modelSelect.innerHTML = '<option value="">无可用API Key</option>';
  1607. modelSelect.disabled = true;
  1608. }
  1609. } catch (error) {
  1610. showToast('API检测失败:' + error.message);
  1611. modelSelect.innerHTML = '<option value="">获取模型列表失败</option>';
  1612. modelSelect.disabled = true;
  1613. } finally {
  1614. checkButton.disabled = false;
  1615. }
  1616. });
  1617. }
  1618. // 保存配置
  1619. const saveButton = modal.querySelector('#ai-save-config');
  1620. if (saveButton) {
  1621. saveButton.addEventListener('click', function(e) {
  1622. e.preventDefault();
  1623. e.stopPropagation();
  1624. const endpoint = modal.querySelector('#ai-endpoint')?.value?.trim() || '';
  1625. const apiKeys = modal.querySelector('#ai-apikey')?.value?.trim() || '';
  1626. const selectedModel = modelSelect?.value || '';
  1627. if (!endpoint || !apiKeys) {
  1628. showToast('请填写API Endpoint和至少一个API Key');
  1629. return;
  1630. }
  1631. if (!selectedModel) {
  1632. showToast('请选择一个视觉模型');
  1633. return;
  1634. }
  1635. GM_setValue('apiEndpoint', endpoint);
  1636. GM_setValue('apiKey', apiKeys);
  1637. GM_setValue('selectedModel', selectedModel);
  1638. showToast('配置已保存');
  1639. if (overlay && overlay.parentNode) {
  1640. overlay.parentNode.removeChild(overlay);
  1641. }
  1642. });
  1643. }
  1644. // 取消配置
  1645. const cancelButton = modal.querySelector('#ai-cancel-config');
  1646. if (cancelButton) {
  1647. cancelButton.addEventListener('click', function(e) {
  1648. e.preventDefault();
  1649. e.stopPropagation();
  1650. if (overlay && overlay.parentNode) {
  1651. overlay.parentNode.removeChild(overlay);
  1652. }
  1653. });
  1654. }
  1655. // 点击遮罩层关闭
  1656. overlay.addEventListener('click', function(e) {
  1657. if (e.target === overlay) {
  1658. if (overlay.parentNode) {
  1659. overlay.parentNode.removeChild(overlay);
  1660. }
  1661. }
  1662. });
  1663. // 阻止模态框内的点击事件冒泡
  1664. modal.addEventListener('click', function(e) {
  1665. e.stopPropagation();
  1666. });
  1667. }
  1668. // 显示图像选择面
  1669. function showImageSelectionModal() {
  1670. const overlay = document.createElement('div');
  1671. overlay.className = 'ai-modal-overlay';
  1672. const modal = document.createElement('div');
  1673. modal.className = 'ai-config-modal';
  1674. modal.innerHTML = `
  1675. <h3>选择要识别的图像</h3>
  1676. <div class="ai-image-options">
  1677. <button id="ai-all-images">识别所有图片</button>
  1678. <button id="ai-visible-images">仅识别可见图片</button>
  1679. </div>
  1680. <button id="ai-cancel">取消</button>
  1681. `;
  1682. overlay.appendChild(modal);
  1683. document.body.appendChild(overlay);
  1684. // 添加事件监听
  1685. modal.querySelector('#ai-all-images').onclick = () => {
  1686. if (checkApiConfig()) {
  1687. describeAllImages();
  1688. overlay.remove();
  1689. }
  1690. };
  1691. modal.querySelector('#ai-visible-images').onclick = () => {
  1692. if (checkApiConfig()) {
  1693. describeVisibleImages();
  1694. overlay.remove();
  1695. }
  1696. };
  1697. modal.querySelector('#ai-cancel').onclick = () => {
  1698. overlay.remove();
  1699. };
  1700. // 点击遮罩层关闭
  1701. overlay.addEventListener('click', (e) => {
  1702. if (e.target === overlay) {
  1703. overlay.remove();
  1704. }
  1705. });
  1706. }
  1707. function showDescriptionModal(description, balanceInfo) {
  1708. // 移除已存在的结果框
  1709. const existingModal = document.querySelector('.ai-r###lt-modal');
  1710. if (existingModal) {
  1711. existingModal.remove();
  1712. }
  1713. const overlay = document.createElement('div');
  1714. overlay.className = 'ai-modal-overlay';
  1715. const modal = document.createElement('div');
  1716. modal.className = 'ai-r###lt-modal';
  1717. modal.innerHTML = `
  1718. <div class="r###lt-content">
  1719. <div class="description-code">
  1720. <code>${description}</code>
  1721. </div>
  1722. <div class="copy-hint">点击上方文本可复制</div>
  1723. <button class="close-button">×</button>
  1724. ${balanceInfo ? `<div class="balance-info">${balanceInfo}</div>` : ''}
  1725. </div>
  1726. `;
  1727. // 添加复制功能
  1728. const codeBlock = modal.querySelector('.description-code');
  1729. codeBlock.addEventListener('click', async () => {
  1730. try {
  1731. await navigator.clipboard.writeText(description);
  1732. showToast('已复制到剪贴板');
  1733. } catch (err) {
  1734. console.error('[Debug] Copy failed:', err);
  1735. // 如果 clipboard API 失败,使用 GM_setClipboard 作为备选
  1736. GM_setClipboard(description);
  1737. showToast('已复制到剪贴板');
  1738. }
  1739. });
  1740. // 添加关闭功能
  1741. const closeButton = modal.querySelector('.close-button');
  1742. closeButton.addEventListener('click', () => {
  1743. overlay.remove();
  1744. });
  1745. // ESC键关闭
  1746. const escHandler = (e) => {
  1747. if (e.key === 'Escape') {
  1748. overlay.remove();
  1749. document.removeEventListener('keydown', escHandler);
  1750. }
  1751. };
  1752. document.addEventListener('keydown', escHandler);
  1753. overlay.appendChild(modal);
  1754. document.body.appendChild(overlay);
  1755. }
  1756. // 添加计算成本的函数
  1757. function calculateCost(imageSize, modelName) {
  1758. let baseCost;
  1759. switch (modelName) {
  1760. case 'glm-4v':
  1761. baseCost = 0.015; // GLM-4V的基础成本
  1762. break;
  1763. case 'glm-4v-flash':
  1764. baseCost = 0.002; // GLM-4V-Flash的基础成本
  1765. break;
  1766. case 'Qwen/Qwen2-VL-72B-Instruct':
  1767. baseCost = 0.015;
  1768. break;
  1769. case 'Pro/Qwen/Qwen2-VL-7B-Instruct':
  1770. baseCost = 0.005;
  1771. break;
  1772. case 'OpenGVLab/InternVL2-Llama3-76B':
  1773. baseCost = 0.015;
  1774. break;
  1775. case 'OpenGVLab/InternVL2-26B':
  1776. baseCost = 0.008;
  1777. break;
  1778. case 'Pro/OpenGVLab/InternVL2-8B':
  1779. baseCost = 0.003;
  1780. break;
  1781. case 'deepseek-ai/deepseek-vl2':
  1782. baseCost = 0.012; // 设置deepseek-vl2的基础成本
  1783. break;
  1784. default:
  1785. baseCost = 0.01;
  1786. }
  1787. // 图片大小影响因子(每MB增加一定成本)
  1788. const imageSizeMB = imageSize / (#### * ####);
  1789. const sizeMultiplier = 1 + (imageSizeMB * 0.1); // 每MB增加10%成本
  1790. return baseCost * sizeMultiplier;
  1791. }
  1792. // 初始化
  1793. function initialize() {
  1794. // 确保DOM加载成后再创建按钮
  1795. if (document.readyState === 'loading') {
  1796. document.addEventListener('DOMContentLoaded', () => {
  1797. createFloatingButton();
  1798. });
  1799. } else {
  1800. createFloatingButton();
  1801. }
  1802. }
  1803. // 启动脚本
  1804. initialize();
  1805. })();