DesignPic_20250812172612.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import React, { useState } from 'react'
  2. import { Button, Table, Input, Message } from '@alifd/next';
  3. export default function DesignPic() {
  4. // 添加状态管理表格数据
  5. const [tableData, setTableData] = useState(() => {
  6. const result = [];
  7. for (let i = 0; i < 5; i++) {
  8. result.push({
  9. title: { name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible` },
  10. id: 100306660940 + i,
  11. time: 2e3 + i,
  12. // 添加名称和标签字段
  13. picName: '1235',
  14. picLabel: ''
  15. });
  16. }
  17. return result;
  18. });
  19. const pic_render = (value: any, index: any, record: any) => {
  20. return (
  21. <img className='size-20' src={record.url} alt={record.title} />
  22. );
  23. }
  24. // 修改图片名称渲染函数
  25. const name_render = (value: any, index: number, record: any) => {
  26. return <>
  27. <Input.TextArea
  28. value={record.picName}
  29. onChange={(value) => handleNameChange(value, index)}
  30. composition
  31. placeholder={'请输入图片名称'}
  32. />
  33. </>
  34. };
  35. // 修改图片标签渲染函数
  36. const label_render = (value: any, index: number, record: any) => {
  37. return <>
  38. <Input.TextArea
  39. value={record.picLabel}
  40. onChange={(value) => handleLabelChange(value, index)}
  41. composition
  42. placeholder={'请输入图片标签'}
  43. />
  44. </>
  45. };
  46. // 添加名称变化处理函数
  47. const handleNameChange = (value: string, index: number) => {
  48. const newData = [...tableData];
  49. newData[index].picName = value;
  50. setTableData(newData);
  51. };
  52. // 添加标签变化处理函数
  53. const handleLabelChange = (value: string, index: number) => {
  54. const newData = [...tableData];
  55. newData[index].picLabel = value;
  56. setTableData(newData);
  57. };
  58. // 完善删除功能
  59. const handleDelete = (index: number, record: any) => {
  60. // 方案1: 直接删除
  61. const newData = [...tableData];
  62. newData.splice(index, 1);
  63. setTableData(newData);
  64. // 方案2: 带确认的删除 (可选)
  65. // if (window.confirm(`确定要删除 "${record.title.name}" 吗?`)) {
  66. // const newData = [...tableData];
  67. // newData.splice(index, 1);
  68. // setTableData(newData);
  69. // Message.success('删除成功');
  70. // }
  71. };
  72. // 删除操作渲染函数
  73. const option_render = (value: any, index: number, record: any) => {
  74. return (
  75. <a
  76. className='text-red-600 cursor-pointer'
  77. onClick={() => handleDelete(index, record)}
  78. >
  79. 删除
  80. </a>
  81. );
  82. };
  83. // 添加行索引以确保正确删除
  84. const dataSourceWithIndex = tableData.map((item, index) => ({
  85. ...item,
  86. index
  87. }));
  88. const getPicData = async () => {
  89. try {
  90. // 获取当前活动标签页
  91. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  92. if (!tab.id) {
  93. Message.error('无法获取当前标签页信息');
  94. return;
  95. }
  96. // 检查是否是受限制的页面
  97. const restrictedUrls = ['chrome://', 'chrome-extension://', 'about:'];
  98. if (restrictedUrls.some(url => tab.url?.startsWith(url))) {
  99. Message.error('无法在浏览器内部页面或扩展页面上获取图片,请切换到普通网页(如百度、淘宝等)');
  100. return;
  101. }
  102. try {
  103. // 发送消息到 content script 请求获取图片数据
  104. const response = await browser.tabs.sendMessage(tab.id, { action: "getImages" });
  105. if (response.error) {
  106. Message.error('获取图片时出错: ' + response.error);
  107. return;
  108. }
  109. if (response.images && response.images.length > 0) {
  110. // 处理获取到的图片数据并更新表格
  111. const newTableData = response.images.map((img: any, index: number) => ({
  112. key: index,
  113. url: img.url,
  114. title: { name: img.alt || `图片 ${index + 1}` },
  115. id: index,
  116. time: Date.now(),
  117. picName: img.alt || `图片 ${index + 1}`,
  118. picLabel: `${img.element}${img.class ? '.' + img.class : ''}${img.id ? '#' + img.id : ''}`
  119. }));
  120. setTableData(newTableData);
  121. Message.success(`成功获取到 ${response.images.length} 张图片`);
  122. } else {
  123. Message.warning('在当前页面未找到图片');
  124. setTableData([]);
  125. }
  126. } catch (sendMessageError: any) {
  127. console.error("发送消息失败:", sendMessageError);
  128. if (sendMessageError?.message?.includes('Could not establish connection') ||
  129. sendMessageError?.message?.includes('Receiving end does not exist')) {
  130. Message.error(
  131. <div>
  132. <div>无法连接到页面内容脚本,请按以下步骤操作:</div>
  133. <ol className="list-decimal list-inside mt-2">
  134. <li>刷新当前页面后再次尝试</li>
  135. <li>确保您在普通网页上(如百度、淘宝等,非浏览器设置页面)</li>
  136. <li>检查扩展权限:右键扩展图标 → "此扩展程序可访问的网站" → 选择"在所有网站上"</li>
  137. <li>如果问题仍然存在,请重新加载扩展或重启浏览器</li>
  138. </ol>
  139. </div>
  140. );
  141. } else {
  142. Message.error('获取图片数据失败: ' + sendMessageError.message);
  143. }
  144. }
  145. } catch (error: any) {
  146. console.error("获取标签页信息失败:", error);
  147. Message.error('获取标签页信息失败: ' + error.message);
  148. }
  149. };
  150. const testConnection = async () => {
  151. try {
  152. const [tab] = await browser.tabs.query({ active: true, currentWindow: true });
  153. if (!tab.id) {
  154. Message.error('无法获取当前标签页信息');
  155. return;
  156. }
  157. // 发送一个简单的测试消息
  158. await browser.tabs.sendMessage(tab.id, { action: "test" });
  159. Message.success('连接测试成功!Content script 正常工作');
  160. } catch (error: any) {
  161. if (error?.message?.includes('Could not establish connection')) {
  162. Message.error('连接测试失败:Content script 未在当前页面加载');
  163. } else {
  164. Message.error('连接测试失败:' + error.message);
  165. }
  166. }
  167. };
  168. return (
  169. <div className='w-3xl h-fit'>
  170. <Button style={{ marginBottom: '10px' }} onClick={() => {
  171. getPicData()
  172. }}>获取图片数据</Button>
  173. <Button onClick={() => testConnection()}>测试连接</Button>
  174. <Table dataSource={dataSourceWithIndex}>
  175. <Table.Column title="图片" cell={pic_render} />
  176. <Table.Column title="图片名称" cell={name_render} />
  177. <Table.Column title="图片标签" cell={label_render} />
  178. <Table.Column title="操作" cell={option_render} />
  179. </Table>
  180. </div>
  181. )
  182. }