MCPcopy Create free account
hub / github.com/debbide/GeoFill / fillFormSmart

Function fillFormSmart

scripts/content.js:798–835  ·  view source on GitHub ↗

* 智能填写表单 (AI)

(mapping)

Source from the content-addressed store, hash-verified

796 * 智能填写表单 (AI)
797 */
798function fillFormSmart(mapping) {
799 let filledCount = 0;
800 const results = {};
801
802 for (const [key, value] of Object.entries(mapping)) {
803 // key 可能是 id 或 name
804 let element = document.getElementById(key);
805 if (!element) {
806 element = document.querySelector(`[name="${key}"]`);
807 }
808
809 // 如果是生成的临时 ID (field_x),尝试通过索引找回(不太可靠,但作为兜底)
810 if (!element && key.startsWith('field_')) {
811 const index = parseInt(key.split('_')[1]);
812 const inputs = Array.from(document.querySelectorAll('input:not([type="hidden"]):not([type="submit"]):not([type="button"]), select, textarea'));
813 if (inputs[index]) element = inputs[index];
814 }
815
816 if (element && isVisible(element)) {
817 if (element.tagName.toLowerCase() === 'select') {
818 fillSelect(element, value);
819 } else if (element.type === 'radio' || element.type === 'checkbox') {
820 // 对于 radio/checkbox,AI 可能会返回 true/false 或 value
821 if (value === true || value === 'true' || value === element.value) {
822 element.checked = true;
823 }
824 } else {
825 simulateInput(element, value);
826 }
827 filledCount++;
828 results[key] = 'filled';
829 } else {
830 results[key] = 'not found';
831 }
832 }
833
834 return { filledCount, results };
835}
836
837// 监听来自 popup 的消息
838chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {

Callers 1

content.jsFile · 0.85

Calls 3

isVisibleFunction · 0.85
fillSelectFunction · 0.85
simulateInputFunction · 0.85

Tested by

no test coverage detected