123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- // entrypoints/content.ts
- import { Loading } from 'element-plus/es/components/loading/src/service.mjs';
- import src1 from '../assets/images/begin.png'
- import PageAnalyzer from '../utils/page-analyzer'
- import { log } from 'console';
- export default defineContentScript({
- matches: ["<all_urls>"],
- main(ctx) {
- let page = document.getElementsByTagName('body')[0];
- const src = chrome.runtime.getURL('images/begin.png')
- window.pageAnalyzer = new PageAnalyzer();
- window.onload = () => {
- chrome.runtime.sendMessage(getPageInfo())
- }
- let form = null
- let formChildren = []
- let excelDataA = {}
- chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
- if (message.type === 'GET_PAGE_INFO') {
- sendResponse({
- data: getPageInfo()
- })
- }
- let dom = null
- if (message.type === "GET_TAG_ACTION") {
- const data = message.data
- if (data.id) {
- dom = document.getElementById(data.id)
- }
- if (data.class) {
- dom = [...document.getElementsByTagName(data.tag)]
- // .filter(_ => _.className.includes(data.class))
- .filter(_ => _.innerText.includes(data.text))
- }
- console.log(dom);
- dom[0].click()
- sendResponse({ data: '完成' })
- return true
- }
- if (message.type === 'GET_PAGE_FORM') {
- const len = document.querySelectorAll("form").length
- if (len) {
- const forms = document.querySelectorAll("form")
- if (len > 1) {
- form = forms[len - 1]
- } else form = forms[0]
- form.querySelectorAll("svg")
- .forEach((el) => el.remove());
- formChildren = [...form.elements]
- }
- // if (!form && document.querySelector("input")) {
- // const arr = []
- // const inputs = document.querySelectorAll("input")
- // formChildren = [...inputs]
- // for (const element of inputs) {
- // arr.push(element.outerHTML)
- // }
- // form = { outerHTML: JSON.stringify(arr) }
- // }
- if (!form) {
- sendResponse({
- status: 'error',
- message: '没有找到表单'
- })
- return
- }
- sendResponse({
- status: 'ok',
- data: form.outerHTML
- })
- }
- if (message.type === "INPUT_FORM") {
- const { formData, excelData } = message.data
- excelDataA = excelData
- console.log(formData, excelData);
- await handleFillInput(formData, 0)
- }
- return true
- });
- function getPageInfo() {
- const favIconUrl = getFavicon()
- return {
- type: "PAGE_INFO",
- data: {
- favIconUrl,
- title: document.title,
- url: window.location.href,
- content: window.pageAnalyzer.analyzePage()
- },
- }
- }
- function getFavicon() {
- // 尝试获取动态favicon
- const iconLinks = Array.from(
- document.querySelectorAll('link[rel*="icon"]')
- );
- const favIconUrl = iconLinks
- .sort((a, b) => {
- // 优先使用大尺寸图标
- const sizeA = parseInt(a.sizes?.value) || 0;
- const sizeB = parseInt(b.sizes?.value) || 0;
- return sizeB - sizeA;
- })
- .map((link) => link.href)
- .find(Boolean);
- if (favIconUrl) return favIconUrl;
- // 如果没有找到,返回网站根目录的favicon.ico
- const url = new URL(window.location.href);
- return `${url.protocol}//${url.hostname}/favicon.ico`;
- }
- const handleFillInput = async (data, index) => {
- console.log(data);
- for (let i = 0; i < data.length; i++) {
- const item = data[i]
- if (item.findBy === 'id') {
- const input = formChildren.find(child => child.id === item.findByValue)
- if (item.type === 'text' || item.type === 'textarea' || item.type === 'number') {
- if (!input) {
- if (item.label) {
- const label = [...form.getElementsByTagName('label')].find(label => label.innerText.includes(item.label))
- if (label) {
- const input = findLabelForInput(label)
- if (input) {
- await simulateUserInput(input, excelDataA[item.excelColumn][index])
- }
- }
- }
- }
- if (input) {
- await simulateUserInput(input, excelDataA[item.excelColumn][index])
- }
- }
- if (item.type === 'radio' || item.type === 'checkbox') {
- if (item.label) {
- const label = [...form.getElementsByTagName('label')].find(label => label.innerText.includes(item.label))
- if (label) {
- const span = findLabelForSpan(label)
- span.forEach(span => {
- span.innerText === excelDataA[item.excelColumn][index] && span.click()
- })
- }
- }
- }
- if (item.type === 'date') {
- const input = formChildren.find(child => child.id === item.findByValue)
- if (excelDataA[item.excelColumn][index]) {
- await simulateCompleteUserAction(input, input, formatDate(excelDataA[item.excelColumn][index]), formatDate(excelDataA[item.excelColumn][index]))
- }
- }
- } else if (item.findBy === 'placeholder') {
- const input = formChildren.find(child => child.placeholder === item.findByValue)
- if (input) {
- simulateUserInput(input, excelDataA[item.excelColumn][index])
- }
- }
- }
- }
- const simulateUserInput = async (element, value) => {
- // 设置值
- // if (element.tagName.toLowerCase() === 'textarea') {
- // element.focus()
- // element.value = value
- // element.blur()
- // return
- // }
- element.value = value;
- // 创建并触发 input 事件
- const inputEvent = new Event('input', { bubbles: true });
- element.dispatchEvent(inputEvent);
- // 创建并触发 change 事件
- const changeEvent = new Event('change', { bubbles: true });
- element.dispatchEvent(changeEvent);
- // 可选:如果表单使用了 React/Vue 等框架,可能还需要触发以下事件
- element.dispatchEvent(new Event('blur'));
- element.dispatchEvent(new KeyboardEvent('keyup', { key: 'Enter' }));
- }
- const findLabelForInput = (label) => {
- let p = label.parentElement
- for (let i = 0; i < 10; i++) {
- const input = p.getElementsByTagName('input')
- if (input.length > 0) {
- return input[0]
- }
- p = p.parentElement
- }
- return null
- }
- const findLabelForSpan = (label) => {
- let p = label.parentElement
- for (let i = 0; i < 10; i++) {
- const span = p.getElementsByTagName('span')
- if (span.length > 0) {
- return [...span]
- }
- p = p.parentElement
- }
- return null
- }
- const simulateCompleteUserAction = async (clickElement, inputElement, inputText, tdTitle) => {
- // 1. 模拟鼠标弹起事件
- const simulateMouseUp = (element) => {
- const mouseUpEvent = new MouseEvent('mouseup', {
- bubbles: true,
- cancelable: true,
- view: window,
- button: 0,
- buttons: 0,
- clientX: 0,
- clientY: 0,
- detail: 1
- });
- element.dispatchEvent(mouseUpEvent);
- };
- // 123456qq?
- // 2. 模拟键盘输入
- const simulateTyping = async (element, text, delay = 50) => {
- element.focus();
- for (let char of text) {
- await new Promise(resolve => setTimeout(resolve, delay));
- // 按键按下
- const keydownEvent = new KeyboardEvent('keydown', {
- key: char,
- code: `Key${char.toUpperCase()}`,
- bubbles: true,
- cancelable: true
- });
- element.dispatchEvent(keydownEvent);
- // 更新输入值
- element.value += char;
- // 触发输入事件
- const inputEvent = new InputEvent('input', {
- bubbles: true,
- cancelable: true,
- data: char,
- inputType: 'insertText'
- });
- element.dispatchEvent(inputEvent);
- // 按键弹起
- const keyupEvent = new KeyboardEvent('keyup', {
- key: char,
- code: `Key${char.toUpperCase()}`,
- bubbles: true,
- cancelable: true
- });
- element.dispatchEvent(keyupEvent);
- }
- // 触发change事件
- element.dispatchEvent(new Event('change', { bubbles: true }));
- };
- // 3. 查找td元素
- const findTdByTitle = (title, timeout = 5000) => {
- return new Promise((resolve, reject) => {
- const startTime = Date.now();
- const find = () => {
- const td = document.querySelector(`td[title="${title}"]`);
- if (td) {
- resolve(td);
- return;
- }
- if (Date.now() - startTime > timeout) {
- reject(new Error(`未找到title为"${title}"的td元素`));
- return;
- }
- requestAnimationFrame(find);
- };
- find();
- });
- };
- // 4. 模拟点击事件
- const simulateClick = (element) => {
- const clickEvent = new MouseEvent('click', {
- bubbles: true,
- cancelable: true,
- view: window,
- detail: 1
- });
- element.dispatchEvent(clickEvent);
- };
- try {
- // 执行操作序列
- // 1. 触发鼠标弹起
- simulateMouseUp(clickElement);
- await new Promise(resolve => setTimeout(resolve, 100));
- // 2. 模拟键盘输入
- await simulateTyping(inputElement, inputText);
- await new Promise(resolve => setTimeout(resolve, 200));
- // 3. 查找并点击td元素
- // const tdElement = await findTdByTitle(tdTitle);
- const enterKeyEvent = new KeyboardEvent('keydown', {
- key: 'Enter', // 事件键名
- code: 'Enter', // 物理按键编码
- keyCode: 13, // 传统键码(Enter键为13)
- which: 13, // 同keyCode
- bubbles: true, // 允许事件冒泡
- cancelable: true // 允许事件被取消
- });
- setTimeout(() => {
- inputElement.dispatchEvent(enterKeyEvent);
- }, 0)
- await new Promise(resolve => setTimeout(resolve, 500));
- return true;
- } catch (error) {
- throw error;
- }
- };
- function formatDate(date) {
- // 直接创建北京时间的日期对象
- const d = new Date(date);
- // 获取年、月、日
- const year = d.getFullYear();
- const month = String(d.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需要加1
- const day = String(d.getDate()).padStart(2, '0');
- return `${year}-${month}-${day}`;
- }
- },
- });
|