content.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // entrypoints/content.ts
  2. import PageAnalyzer, { cleanPage } from '../utils/page-analyzer'
  3. export default defineContentScript({
  4. matches: ["<all_urls>"],
  5. main(ctx) {
  6. let page = document.getElementsByTagName('body')[0];
  7. const src = chrome.runtime.getURL('images/begin.png')
  8. window.pageAnalyzer = new PageAnalyzer();
  9. window.onload = () => {
  10. chrome.runtime.sendMessage(getPageInfo())
  11. console.log(document.title);
  12. }
  13. let form = null
  14. let formChildren = []
  15. let excelDataA = {}
  16. chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
  17. if (message.type === 'GET_PAGE_INFO') {
  18. sendResponse({
  19. data: getPageInfo()
  20. })
  21. }
  22. let dom = null
  23. if (message.type === "GET_TAG_ACTION") {
  24. const data = message.data
  25. if (0) {
  26. dom = document.getElementById(data.id)
  27. } else {
  28. dom = [...document.getElementsByTagName(data.tag.toLowerCase())]
  29. // .filter(_ => _.className.includes(data.class))
  30. .filter(_ => _.innerText.includes(data.text))[0]
  31. }
  32. console.log(dom);
  33. if (!dom) {
  34. sendResponse({ data: '未找到元素,请重试', statue: 'error' })
  35. return
  36. }
  37. // 添加红色边框,500ms后移除边框,然后点击
  38. const originalBorder = dom.style.border;
  39. dom.style.border = '2px solid red';
  40. setTimeout(() => {
  41. dom.style.border = originalBorder;
  42. dom.click();
  43. }, 1000);
  44. sendResponse({ data: '完成' })
  45. return true
  46. }
  47. if (message.type === 'GET_PAGE_FORM') {
  48. const len = document.querySelectorAll("form").length
  49. if (document.title === '智能招采' && len === 1) {
  50. sendResponse({
  51. status: 'error',
  52. message: '没有找到表单'
  53. })
  54. return
  55. }
  56. if (len) {
  57. const forms = document.querySelectorAll("form")
  58. if (len > 1) {
  59. form = forms[len - 1]
  60. } else form = forms[0]
  61. form.querySelectorAll("svg")
  62. .forEach((el) => el.remove());
  63. formChildren = [...form.elements]
  64. }
  65. // if (!form && document.querySelector("input")) {
  66. // const arr = []
  67. // const inputs = document.querySelectorAll("input")
  68. // formChildren = [...inputs]
  69. // for (const element of inputs) {
  70. // arr.push(element.outerHTML)
  71. // }
  72. // form = { outerHTML: JSON.stringify(arr) }
  73. // }
  74. if (!form) {
  75. sendResponse({
  76. status: 'error',
  77. message: '没有找到表单'
  78. })
  79. return
  80. }
  81. sendResponse({
  82. status: 'ok',
  83. data: form.outerHTML
  84. })
  85. }
  86. if (message.type === "INPUT_FORM") {
  87. const { formData, excelData } = message.data
  88. excelDataA = excelData
  89. console.log(formData, excelDataA);
  90. await handleFillInput(formData, 0)
  91. }
  92. return true
  93. });
  94. function getPageInfo() {
  95. const favIconUrl = getFavicon()
  96. return {
  97. type: "PAGE_INFO",
  98. data: {
  99. favIconUrl,
  100. title: document.title,
  101. url: window.location.href,
  102. content: window.pageAnalyzer.analyzePage()
  103. },
  104. }
  105. }
  106. function getFavicon() {
  107. // 尝试获取动态favicon
  108. const iconLinks = Array.from(
  109. document.querySelectorAll('link[rel*="icon"]')
  110. );
  111. const favIconUrl = iconLinks
  112. .sort((a, b) => {
  113. // 优先使用大尺寸图标
  114. const sizeA = parseInt(a.sizes?.value) || 0;
  115. const sizeB = parseInt(b.sizes?.value) || 0;
  116. return sizeB - sizeA;
  117. })
  118. .map((link) => link.href)
  119. .find(Boolean);
  120. if (favIconUrl) return favIconUrl;
  121. // 如果没有找到,返回网站根目录的favicon.ico
  122. const url = new URL(window.location.href);
  123. return `${url.protocol}//${url.hostname}/favicon.ico`;
  124. }
  125. const handleFillInput = async (data, index) => {
  126. console.log(data);
  127. for (let i = 0; i < data.length; i++) {
  128. const item = data[i]
  129. if (item.findBy === 'id') {
  130. const input = formChildren.find(child => child.id === item.findByValue)
  131. if (item.type === 'text' || item.type === 'textarea' || item.type === 'number') {
  132. if (!input) {
  133. if (item.label) {
  134. const label = [...form.getElementsByTagName('label')].find(label => label.innerText.includes(item.label))
  135. if (label) {
  136. const input = findLabelForInput(label)
  137. if (input) {
  138. await simulateUserInput(input, excelDataA[item.excelColumn])
  139. }
  140. }
  141. }
  142. }
  143. if (input) {
  144. await simulateUserInput(input, excelDataA[item.excelColumn])
  145. }
  146. }
  147. if (item.type === 'radio' || item.type === 'checkbox') {
  148. if (item.label) {
  149. const label = [...form.getElementsByTagName('label')].find(label => label.innerText.includes(item.label))
  150. if (label) {
  151. const span = findLabelForSpan(label)
  152. span.forEach(span => {
  153. span.innerText === excelDataA[item.excelColumn] && span.click()
  154. })
  155. }
  156. }
  157. }
  158. if (item.type === 'date') {
  159. const input = formChildren.find(child => child.id === item.findByValue)
  160. if (excelDataA[item.excelColumn]) {
  161. await simulateCompleteUserAction(input, input, formatDate(excelDataA[item.excelColumn]), formatDate(excelDataA[item.excelColumn]))
  162. }
  163. }
  164. }
  165. if (item.findBy === 'placeholder') {
  166. const input = formChildren.find(child => child.placeholder === item.findByValue)
  167. if (input) {
  168. simulateUserInput(input, excelDataA[item.excelColumn])
  169. }
  170. }
  171. if (item.findBy === 'label') {
  172. if (!excelDataA[item.excelColumn]) continue
  173. const label = [...form.getElementsByTagName('label')].find(label => label.innerText.includes(item.findByValue) || item.findByValue.includes(label.innerText))
  174. console.log(label)
  175. if (!label) continue
  176. if (item.type === 'radio' || item.type === 'checkbox') {
  177. console.log(excelDataA[item.excelColumn]);
  178. if (label) {
  179. const span = findLabelForSpan(label)
  180. span.forEach(span => {
  181. console.log(span.innerText);
  182. if (span.innerText) {
  183. if (span.innerText.includes(excelDataA[item.excelColumn]) || excelDataA[item.excelColumn].includes(span.innerText)) {
  184. console.log(span);
  185. span.click()
  186. }
  187. }
  188. })
  189. }
  190. }
  191. if (item.type === 'date') {
  192. const input = findLabelForInput(label)
  193. console.log(input, excelDataA[item.excelColumn]);
  194. if (input) {
  195. await simulateCompleteUserAction(input, input, formatDate(excelDataA[item.excelColumn]), formatDate(excelDataA[item.excelColumn]))
  196. }
  197. }
  198. if (item.type === 'input' || item.type === 'text' || item.type === 'number' || item.type === 'tel' || item.type === 'email') {
  199. const input = findLabelForInput(label)
  200. if (input) {
  201. console.log(excelDataA[item.excelColumn]);
  202. await simulateUserInput(input, excelDataA[item.excelColumn])
  203. }
  204. }
  205. if (item.type === 'select') {
  206. const input = findLabelForInput(label)
  207. if (input) {
  208. await simulateUserInput(input, excelDataA[item.excelColumn])
  209. }
  210. }
  211. }
  212. }
  213. }
  214. const simulateUserInput = async (element, value) => {
  215. // 设置值
  216. // if (element.tagName.toLowerCase() === 'textarea') {
  217. // element.focus()
  218. // element.value = value
  219. // element.blur()
  220. // return
  221. // }
  222. element.value = value;
  223. // 创建并触发 input 事件
  224. const inputEvent = new Event('input', { bubbles: true });
  225. element.dispatchEvent(inputEvent);
  226. // 创建并触发 change 事件
  227. const changeEvent = new Event('change', { bubbles: true });
  228. element.dispatchEvent(changeEvent);
  229. // 可选:如果表单使用了 React/Vue 等框架,可能还需要触发以下事件
  230. element.dispatchEvent(new Event('blur'));
  231. element.dispatchEvent(new KeyboardEvent('keyup', { key: 'Enter' }));
  232. }
  233. const findLabelForInput = (label) => {
  234. let p = label.parentElement
  235. for (let i = 0; i < 10; i++) {
  236. const input = p.getElementsByTagName('input')
  237. if (input.length > 0) {
  238. return input[0]
  239. }
  240. p = p.parentElement
  241. }
  242. return null
  243. }
  244. const findLabelForTag = (label, tag) => {
  245. let p = label.parentElement
  246. for (let i = 0; i < 10; i++) {
  247. const input = p.getElementsByTagName('input')
  248. if (input.length > 0) {
  249. return input[0]
  250. }
  251. p = p.parentElement
  252. }
  253. return null
  254. }
  255. const findLabelForSpan = (label) => {
  256. let p = label.parentElement
  257. for (let i = 0; i < 10; i++) {
  258. const span = p.getElementsByTagName('span')
  259. if (span.length > 0) {
  260. return [...span]
  261. }
  262. p = p.parentElement
  263. }
  264. return null
  265. }
  266. const simulateCompleteUserAction = async (clickElement, inputElement, inputText, tdTitle) => {
  267. // 1. 模拟鼠标弹起事件
  268. const simulateMouseUp = (element) => {
  269. const mouseUpEvent = new MouseEvent('mouseup', {
  270. bubbles: true,
  271. cancelable: true,
  272. view: window,
  273. button: 0,
  274. buttons: 0,
  275. clientX: 0,
  276. clientY: 0,
  277. detail: 1
  278. });
  279. element.dispatchEvent(mouseUpEvent);
  280. };
  281. // 123456qq?
  282. // 2. 模拟键盘输入
  283. const simulateTyping = async (element, text, delay = 50) => {
  284. element.focus();
  285. for (let char of text) {
  286. await new Promise(resolve => setTimeout(resolve, delay));
  287. // 按键按下
  288. const keydownEvent = new KeyboardEvent('keydown', {
  289. key: char,
  290. code: `Key${char.toUpperCase()}`,
  291. bubbles: true,
  292. cancelable: true
  293. });
  294. element.dispatchEvent(keydownEvent);
  295. // 更新输入值
  296. element.value += char;
  297. // 触发输入事件
  298. const inputEvent = new InputEvent('input', {
  299. bubbles: true,
  300. cancelable: true,
  301. data: char,
  302. inputType: 'insertText'
  303. });
  304. element.dispatchEvent(inputEvent);
  305. // 按键弹起
  306. const keyupEvent = new KeyboardEvent('keyup', {
  307. key: char,
  308. code: `Key${char.toUpperCase()}`,
  309. bubbles: true,
  310. cancelable: true
  311. });
  312. element.dispatchEvent(keyupEvent);
  313. }
  314. // 触发change事件
  315. element.dispatchEvent(new Event('change', { bubbles: true }));
  316. };
  317. // 3. 查找td元素
  318. const findTdByTitle = (title, timeout = 5000) => {
  319. return new Promise((resolve, reject) => {
  320. const startTime = Date.now();
  321. const find = () => {
  322. const td = document.querySelector(`td[title="${title}"]`);
  323. if (td) {
  324. resolve(td);
  325. return;
  326. }
  327. if (Date.now() - startTime > timeout) {
  328. reject(new Error(`未找到title为"${title}"的td元素`));
  329. return;
  330. }
  331. requestAnimationFrame(find);
  332. };
  333. find();
  334. });
  335. };
  336. // 4. 模拟点击事件
  337. const simulateClick = (element) => {
  338. const clickEvent = new MouseEvent('click', {
  339. bubbles: true,
  340. cancelable: true,
  341. view: window,
  342. detail: 1
  343. });
  344. element.dispatchEvent(clickEvent);
  345. };
  346. try {
  347. // 执行操作序列
  348. // 1. 触发鼠标弹起
  349. simulateMouseUp(clickElement);
  350. await new Promise(resolve => setTimeout(resolve, 100));
  351. // 2. 模拟键盘输入
  352. await simulateTyping(inputElement, inputText);
  353. await new Promise(resolve => setTimeout(resolve, 200));
  354. // 3. 查找并点击td元素
  355. // const tdElement = await findTdByTitle(tdTitle);
  356. const enterKeyEvent = new KeyboardEvent('keydown', {
  357. key: 'Enter', // 事件键名
  358. code: 'Enter', // 物理按键编码
  359. keyCode: 13, // 传统键码(Enter键为13)
  360. which: 13, // 同keyCode
  361. bubbles: true, // 允许事件冒泡
  362. cancelable: true // 允许事件被取消
  363. });
  364. setTimeout(() => {
  365. inputElement.dispatchEvent(enterKeyEvent);
  366. }, 0)
  367. await new Promise(resolve => setTimeout(resolve, 500));
  368. return true;
  369. } catch (error) {
  370. throw error;
  371. }
  372. };
  373. function formatDate(date) {
  374. // 直接创建北京时间的日期对象
  375. const d = new Date(date);
  376. // 获取年、月、日
  377. const year = d.getFullYear();
  378. const month = String(d.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
  379. const day = String(d.getDate()).padStart(2, '0');
  380. return `${year}-${month}-${day}`;
  381. }
  382. },
  383. });