@kc-one/smart-fill-sdk 0.0.1-beta.1 → 0.0.1-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/index.esm.js +408 -254
- package/dist/index.umd.cjs +6 -6
- package/dist/src/adapters/element.d.ts +3 -0
- package/dist/src/adapters/element.d.ts.map +1 -0
- package/dist/src/core/smart-fill-instance.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/scanner/dom-scanner.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.umd.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
(function(p,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(p=typeof globalThis<"u"?globalThis:p||self,y(p.SmartFillSDK={}))})(this,function(p){"use strict";var nt=Object.defineProperty;var st=(p,y,v)=>y in p?nt(p,y,{enumerable:!0,configurable:!0,writable:!0,value:v}):p[y]=v;var c=(p,y,v)=>st(p,typeof y!="symbol"?y+"":y,v);const y="https://loan.kdbank.cn";class v extends Error{constructor(t){super(t.message);c(this,"smartFillError");this.name="SmartFillException",this.smartFillError=t}}function E(n,e,t,s={}){return new v({code:n,message:e,stage:t,...s})}function oe(n,e,t="RECOGNIZE_FAILED"){return n instanceof v?n.smartFillError:{code:t,message:n instanceof Error?n.message:String(n||"智能录入异常"),stage:e,retryable:e==="recognize"}}function C(n="sf"){return`${n}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,10)}`}class le{constructor(e){c(this,"seToken","");c(this,"baseURL",y);this.config=e}setAccessToken(e){this.seToken=e}async createSession(){if(!/^seKey-[A-Za-z0-9_-]{6,}$/.test(this.config.apiKey))throw E("API_KEY_INVALID","apiKey 格式不正确,应以 seKey- 开头。","setup");return{apiKey:this.config.apiKey,rulesVersion:"0.0.1"}}async getFormConfig(e){return e?this.request(`/sdk/forms/${encodeURIComponent(e)}`,{method:"GET"}):null}async resolveInputText(e){var r,a,o,d;const t=(r=e.images)!=null&&r.length?e.images:[],s=((a=e.text)==null?void 0:a.trim())||"";if(!t.length)return{text:s,usedOcr:!1};(o=e.onStatusChange)==null||o.call(e,"image_uploading");const i=await this.recognizeImages(t);return(d=e.onStatusChange)==null||d.call(e,"image_recognizing"),{text:i,usedOcr:!0}}async recognize(e){var a,o;const t=performance.now(),s=(a=e.text)==null?void 0:a.trim();(o=e.onStatusChange)==null||o.call(e,"recognizing");const i=await this.request("/fcloud/flow-product/agentChat/smartEntry",{method:"POST",body:JSON.stringify({formCode:e.formCode||"",configVersion:e.configVersion||"",formMsg:e.fields.map(({element:d,...l})=>l),userInputMsg:s||""})}),r=de(i,e.fields,e.scanToken);return{scanToken:e.scanToken,suggestions:r,trace:i.trace||{traceId:C("trace"),usedOcr:!!e.usedOcr,usedAi:!0,durationMs:Math.round(performance.now()-t)}}}async recognizeImages(e){const t=new FormData;e.forEach(r=>{t.append("image",r,r.name)});const s=await this.request("/fcloud/flow-product/agentChat/smartEntry/ocr",{method:"POST",body:t}),i=fe(s);if(!i)throw E("RECOGNIZE_FAILED","图片识别未提取到文本内容。","recognize");return i}async request(e,t){const s=new AbortController,i=window.setTimeout(()=>s.abort(),this.config.requestTimeoutMs??3e4),r=new Headers(t.headers);t.body&&!(t.body instanceof FormData)&&!r.has("Content-Type")&&r.set("Content-Type","application/json"),r.set("x-trace-id",C("trace")),this.seToken&&r.set("seToken",`${this.seToken}`);try{const a=await fetch(`${this.baseURL}${e}`,{...t,headers:r,signal:s.signal});if(!a.ok)throw E(pe(a.status),await a.text(),e.includes("session")?"setup":"recognize",{retryable:a.status>=500||a.status===429});return a.json()}catch(a){throw a instanceof DOMException&&a.name==="AbortError"?E("RECOGNIZE_TIMEOUT","识别请求超时,请稍后重试。","recognize",{retryable:!0}):a}finally{window.clearTimeout(i)}}}const ce=.95;function de(n,e,t){var r;const s=new Map(e.map(a=>[a.fieldId,a]));return(((r=n.data)==null?void 0:r.fieldValues)||[]).filter(a=>!!(a!=null&&a.fieldId)).map(a=>{const o=s.get(a.fieldId);return{fieldId:a.fieldId,scanToken:t,label:a.label||(o==null?void 0:o.label)||a.fieldId,value:a.value,displayValue:a.value==null?"":String(a.value),confidence:ue(a.confidence),source:a.source||"ai",warnings:a.warnings}})}function ue(n){return typeof n!="number"||Number.isNaN(n)?ce:n<0?0:n>1?1:n}function fe(n){var t;const e=typeof n.data=="string"?n.data:((t=n.data)==null?void 0:t.text)||n.text||"";return String(e||"").trim()}function pe(n){return n===401?"TOKEN_EXPIRED":n===403?"API_KEY_FORBIDDEN":n===404?"FORM_CONFIG_NOT_FOUND":"RECOGNIZE_FAILED"}class _{constructor(){c(this,"handlers",new Map)}on(e,t){const s=this.handlers.get(e)??new Set;return s.add(t),this.handlers.set(e,s),()=>this.off(e,t)}off(e,t){var s;(s=this.handlers.get(e))==null||s.delete(t)}emit(e,t){var s;(s=this.handlers.get(e))==null||s.forEach(i=>i(t))}clear(){this.handlers.clear()}}class he{constructor(){c(this,"instances",new Set);c(this,"active",null)}add(e){this.instances.add(e)}activate(e){this.active&&this.active!==e&&this.active.close(),this.active=e}remove(e){this.instances.delete(e),this.active===e&&(this.active=null)}destroyAll(){this.instances.forEach(e=>e.destroy()),this.instances.clear(),this.active=null}}const ge=[/^(el|rc|ant|radix|headlessui|mui|chakra)-/i,/[0-9a-f]{8,}/i,/\d{6,}/];function w(n){const e=String(n||"").trim();return!e||ge.some(t=>t.test(e))?"":e}function O(n){const e=(n.options||[]).slice(0,20).map(t=>`${t.label}:${String(t.value)}`).join("|");return A([n.fieldId,n.tagName,n.type,w(n.name),w(n.id),n.label,n.placeholder,n.section,e].join("::"))}function A(n){return String(n??"").replace(/\s+/g," ").trim().toLowerCase()}const $=new WeakMap;class D{constructor(e,t,s=[]){this.fields=e,this.schemas=t,this.adapters=s}async apply(e){const t=[],s=[];for(const i of e.values){const r=this.fields.find(l=>l.fieldId===i.fieldId);if(!r){s.push(L(i.fieldId,"",i.value,"字段不在当前扫描结果中","FIELD_NOT_FOUND"));continue}if(r.scanToken!==e.scanToken){s.push(L(r.fieldId,r.label,i.value,"页面扫描已过期,请重新扫描","SCAN_TOKEN_EXPIRED"));continue}const a=this.schemas.find(l=>l.fieldId===i.fieldId),o=a!=null&&a.transform?a.transform(i.value):i.value,d=await this.getValue(r,a);try{const l=a!=null&&a.validate?await a.validate(o):!0;if(l!==!0){s.push(L(r.fieldId,r.label,o,typeof l=="string"?l:"字段校验未通过","VALIDATE_FAILED"));continue}await this.setValue(r,o,a),t.push({fieldId:r.fieldId,label:r.label,value:o,previousValue:d})}catch(l){s.push(L(r.fieldId,r.label,o,l instanceof Error?l.message:"字段回填失败","SET_VALUE_FAILED"))}}return{applied:t,skipped:s,warnings:s.length?["部分字段未回填,请查看跳过原因。"]:void 0}}async getValue(e,t){if(t!=null&&t.getValue)return t.getValue();const s=this.matchAdapter(e);if(s!=null&&s.getValue)return s.getValue(e);const i=R(e);if(i)return i instanceof HTMLInputElement&&i.type==="checkbox"?i.checked:i instanceof HTMLInputElement&&i.type==="radio"?i.checked?i.value:void 0:i instanceof HTMLInputElement||i instanceof HTMLTextAreaElement||i instanceof HTMLSelectElement?i.value:i.textContent}async setValue(e,t,s){if(s!=null&&s.setValue){await s.setValue(t);return}const i=R(e);if(!i)throw new Error("页面中未找到对应字段");if(e.fingerprint&&e.fingerprint!==O({...e,tagName:i.tagName.toLowerCase()}))throw new Error("字段结构已变化,请重新扫描");if(e.disabled||e.readonly||i.hasAttribute("disabled")||i.hasAttribute("readonly"))throw new Error("字段不可编辑");const r=this.matchAdapter(e);if(r){await r.setValue(e,t);return}me(i,t),ye(i)}matchAdapter(e){const t=R(e);return t?this.adapters.find(s=>s.match(t,e)):void 0}}function R(n){var e;return(e=n.element)!=null&&e.isConnected?n.element:document.querySelector(`[data-smart-fill-id="${P(n.fieldId)}"]`)}function me(n,e){if(n instanceof HTMLInputElement&&n.type==="checkbox")n.checked=!!e;else if(n instanceof HTMLInputElement&&n.type==="radio"){const t=document.querySelector(`input[type="radio"][name="${P(n.name)}"][value="${P(String(e))}"]`);(t||n).checked=!0}else n instanceof HTMLInputElement||n instanceof HTMLTextAreaElement||n instanceof HTMLSelectElement?n.value=String(e??""):n.isContentEditable&&(n.textContent=String(e??""));n.dispatchEvent(new Event("input",{bubbles:!0})),n.dispatchEvent(new Event("change",{bubbles:!0}))}function ye(n){var s;n.setAttribute("data-smart-fill-highlighted","true"),(s=$.get(n))==null||s.abort();const e=new AbortController,t=i=>{i&&"isTrusted"in i&&!i.isTrusted||(n.removeAttribute("data-smart-fill-highlighted"),e.abort(),$.delete(n))};$.set(n,e),n.addEventListener("focus",t,{signal:e.signal}),n.addEventListener("pointerdown",t,{signal:e.signal}),n.addEventListener("keydown",t,{signal:e.signal}),n.addEventListener("input",t,{signal:e.signal}),n.addEventListener("change",t,{signal:e.signal})}function L(n,e,t,s,i){return{fieldId:n,label:e,attemptedValue:t,reason:s,reasonCode:i}}function P(n){return typeof CSS<"u"&&CSS.escape?CSS.escape(n):n.replace(/["\\]/g,"\\$&")}const be=[{key:"mobile",pattern:new RegExp("(?<!\\d)1[3-9]\\d{9}(?!\\d)","g"),confidence:.98,reason:"手机号正则命中"},{key:"idCard",pattern:new RegExp("(?<!\\d)\\d{17}[\\dXx](?!\\d)","g"),confidence:.96,reason:"身份证号正则命中"},{key:"email",pattern:/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g,confidence:.95,reason:"邮箱正则命中"},{key:"bankCard",pattern:new RegExp("(?<!\\d)\\d{16,19}(?!\\d)","g"),confidence:.9,reason:"银行卡号正则命中"},{key:"amount",pattern:/(?:金额|价格|费用|合计|总计)[::\s]*([0-9]+(?:\.[0-9]{1,2})?)/g,confidence:.88,reason:"金额关键词命中"},{key:"date",pattern:/\d{4}[-/.年]\d{1,2}[-/.月]\d{1,2}日?/g,confidence:.86,reason:"日期格式命中"}],V={legalPerson:["法人","法定代表人","法定代表","企业法人","legal person","legal representative"],emergencyContact:["紧急联系人","紧急联络人","emergency contact"],contact:["联系人","联络人","联系人员","contact"],spouse:["配偶","爱人","夫妻","spouse"]},g={mobile:["手机","手机号","电话","联系电话","mobile","phone"],legalPersonMobile:["法人手机号","法人电话","法定代表人手机号","法人手机","legal person mobile"],emergencyContactMobile:["紧急联系人手机号","紧急联系人电话","紧急联系电话","emergency contact mobile"],contactMobile:["联系人手机号","联系人电话","联系手机","contact mobile"],spouseMobile:["配偶手机号","配偶电话","爱人手机号","spouse mobile"],idCard:["身份证","证件号","身份证号","idcard","id card"],legalPersonIdCard:["法人身份证","法人身份证号","法定代表人身份证号"],spouseIdCard:["配偶身份证","配偶身份证号","爱人身份证号"],email:["邮箱","邮件","email","mail"],contactEmail:["联系人邮箱","联系邮箱","contact email"],name:["姓名","名字","称呼","name"],customerName:["客户姓名","申请人姓名","用户姓名","借款人姓名","客户名称"],legalPersonName:["法人姓名","法定代表人姓名","企业法人姓名"],emergencyContactName:["紧急联系人姓名","紧急联系人名称"],contactName:["联系人姓名","联系人名称"],spouseName:["配偶姓名","爱人姓名"],companyName:["公司名称","企业名称","单位名称","商户名称","公司","企业"],address:["地址","住址","通讯地址","联系地址","现住址","办公地址"],detailAddress:["详细地址","街道地址","门牌地址","详细住址"],amount:["金额","费用","价格","合计","总计","amount","price"],applyAmount:["申请金额","贷款金额","借款金额","授信金额","申请额度"],date:["日期","时间","有效期","date"],applyDate:["申请日期","申请时间","受理日期","进件日期"],bankCard:["银行卡","卡号","bank","bank card"]},q={mobile:"mobile",legalPersonMobile:"mobile",emergencyContactMobile:"mobile",contactMobile:"mobile",spouseMobile:"mobile",idCard:"idCard",legalPersonIdCard:"idCard",spouseIdCard:"idCard",email:"email",contactEmail:"email",name:"name",customerName:"name",legalPersonName:"name",emergencyContactName:"name",contactName:"name",spouseName:"name",companyName:"companyName",address:"address",detailAddress:"address",amount:"amount",applyAmount:"amount",date:"date",applyDate:"date",bankCard:"bankCard"};class U{recognize(e,t,s){const i=Le([...Ee(e),...xe(e)]),r=new Set,a=[];for(const o of i.sort((d,l)=>l.confidence-d.confidence)){const d=we(o,t,r);d&&(a.push({fieldId:d.fieldId,scanToken:s,label:d.label,value:Ie(o.value,d),displayValue:o.value,confidence:o.confidence,source:"local_rule",reason:o.reason}),r.add(d.fieldId))}return a}}function Ee(n){const e=[];for(const t of be)for(const s of n.matchAll(t.pattern)){const i=B(s[1]||s[0]);e.push({key:t.key,value:i,confidence:t.confidence,reason:t.reason,baseKey:q[t.key]})}return e}function xe(n){const e=[],t=n.split(/\r?\n|[;,;]/).map(s=>s.trim()).filter(Boolean);for(const s of t){const i=s.match(/^[“"'`]?([^::=]{2,30})[”"'`]?[::=]\s*(.+)$/);if(!i)continue;const r=Ce(i[1]),a=B(i[2]);if(!(!r||!a))for(const o of ve(r,a))e.push(o)}return e}function ve(n,e){const t=A(n),s=Te(e),i=[],r=Ae(t);return h(t,g.applyDate)&&i.push("applyDate"),h(t,g.applyAmount)&&i.push("applyAmount"),h(t,g.companyName)&&i.push("companyName"),h(t,g.detailAddress)&&i.push("detailAddress"),h(t,g.address)&&i.push("address"),(h(t,g.mobile)||s==="mobile")&&r&&z(i,r,"mobile"),(h(t,g.idCard)||s==="idCard")&&r&&z(i,r,"idCard"),(h(t,g.email)||s==="email")&&r&&z(i,r,"email"),(h(t,g.mobile)||s==="mobile")&&i.push("mobile"),(h(t,g.idCard)||s==="idCard")&&i.push("idCard"),(h(t,g.email)||s==="email")&&i.push("email"),(h(t,g.bankCard)||s==="bankCard")&&i.push("bankCard"),(h(t,g.amount)||s==="amount")&&i.push("amount"),(h(t,g.date)||s==="date")&&i.push("date"),Fe(i).map((a,o)=>({key:a,value:e,confidence:Math.max(.84,.96-o*.04),reason:r?`键值对文本命中(${n},角色增强)`:`键值对文本命中(${n})`,baseKey:q[a]}))}function we(n,e,t){let s=null;for(const i of e){if(t.has(i.fieldId))continue;const r=Se(n,i);r>((s==null?void 0:s.score)??0)&&(s={field:i,score:r})}return s&&s.score>=.45?s.field:null}function Se(n,e){const t=A([e.fieldId,e.label,e.placeholder,e.name,e.id,e.section].join(" ")),s=g[n.key]||[n.key],i=g[n.baseKey]||[n.baseKey];let r=0;return h(t,s)?r+=.95:n.key!==n.baseKey&&h(t,i)?r+=.68:h(t,i)&&(r+=.82),t.includes(A(n.key))&&(r+=.22),e.required&&(r+=.05),n.baseKey==="amount"&&(e.type==="amount"||e.type==="number")&&(r+=.18),n.baseKey==="date"&&e.type==="date"&&(r+=.18),n.baseKey==="address"&&(e.type==="textarea"||e.type==="text")&&(r+=.12),["name","companyName","email","mobile","idCard","bankCard"].includes(n.baseKey)&&e.type==="text"&&(r+=.08),Math.min(r,1)}function Ie(n,e){var t;return e.type==="date"?n.replace(/[年月/.]/g,"-").replace(/日/g,"").replace(/--/g,"-"):e.type==="number"||e.type==="amount"?((t=n.match(/[0-9]+(?:\.[0-9]+)?/))==null?void 0:t[0])??n:e.name&&/mobile|phone|idcard|bankcard/i.test(e.name)||e.id&&/mobile|phone|idcard|bankcard/i.test(e.id)||/mobile|phone|idcard|bankcard/i.test(e.fieldId)?n.replace(/\s+/g,""):n}function Ae(n){const e=Object.keys(V).filter(t=>h(n,V[t]));return e.length?e.includes("emergencyContact")?"emergencyContact":e.includes("legalPerson")?"legalPerson":e.includes("spouse")?"spouse":"contact":null}function Te(n){const e=n.replace(/\s+/g,"");return/^1[3-9]\d{9}$/.test(e)?"mobile":/^\d{17}[\dXx]$/.test(e)?"idCard":/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(n)?"email":/^\d{16,19}$/.test(e)?"bankCard":/^\d{4}[-/.年]\d{1,2}[-/.月]\d{1,2}日?$/.test(n)?"date":/^[0-9]+(?:\.[0-9]{1,2})?$/.test(e)?"amount":/[省市区县路街道号栋室乡镇]/.test(n)?"address":/^[\u4e00-\u9fa5a-zA-Z·]{2,20}$/.test(n)?"name":"unknown"}function z(n,e,t){var i;const s=(i=ke[e])==null?void 0:i[t];s&&n.push(s)}const ke={legalPerson:{mobile:"legalPersonMobile",idCard:"legalPersonIdCard",name:"legalPersonName"},emergencyContact:{mobile:"emergencyContactMobile",name:"emergencyContactName"},contact:{mobile:"contactMobile",email:"contactEmail",name:"contactName"},spouse:{mobile:"spouseMobile",idCard:"spouseIdCard",name:"spouseName"}};function Ce(n){return n.replace(/[“”"'`]/g,"").replace(/\s+/g," ").trim()}function B(n){return n.replace(/[,。;;]+$/g,"").replace(/^[“”"'`\s]+|[“”"'`\s]+$/g,"").trim()}function h(n,e){const t=A(n);return e.some(s=>t.includes(A(s)))}function Le(n){const e=new Map;for(const t of n){const s=`${t.key}::${t.value}`,i=e.get(s);(!i||i.confidence<t.confidence)&&e.set(s,t)}return[...e.values()]}function Fe(n){return[...new Set(n)]}const Me=["input","textarea","select","[contenteditable]:not([contenteditable='false'])"].join(", "),j=["dialog[open]","[role='dialog']","[aria-modal='true']",".modal",".modal-dialog",".ant-modal",".ant-drawer",".el-dialog",".el-drawer",".n-dialog",".n-modal",".popup",".drawer"].join(", "),Ne=new Set(["hidden","submit","button","image","reset","file","password"]);class Y{constructor(e=document){this.root=e}scan(e={}){var d;const t=C("scan");if((d=e.registered)!=null&&d.length)return{scanToken:t,fields:e.registered.map(l=>this.fromSchema(l,t))};const s=this.resolveRoot(e.scanContainer),i=this.collectVisibleFields(s),r=this.detectTopLayerContainer(i),a=i.filter(l=>r.contains(l)),o=(a.length?a:i).slice(0,e.maxFields??200);return{scanToken:t,fields:o.map((l,u)=>this.fromElement(l,u,t))}}fromSchema(e,t){const s=Pe(e.element,this.root),i={fieldId:e.rowKey==null?e.fieldId:`${e.fieldId}:${e.rowKey}`,label:e.label,type:e.type,localRuleMode:e.localRuleMode??"inherit",required:e.required,section:e.section,options:e.options,source:"registered",scanToken:t,element:s||void 0,fingerprint:""};return i.fingerprint=O({...i,tagName:s==null?void 0:s.tagName.toLowerCase()}),i}fromElement(e,t,s){const i=_e(e,t),r=De(e),a={fieldId:i,fingerprint:"",scanToken:s,type:Oe(e),localRuleMode:"inherit",label:Z(e),placeholder:G(e),name:w(e.getAttribute("name"))||void 0,id:w(e.id)||void 0,section:$e(e),options:r,required:e.hasAttribute("required")||e.getAttribute("aria-required")==="true",readonly:e.hasAttribute("readonly"),disabled:e.hasAttribute("disabled"),source:"form_scan",element:e};return a.fingerprint=O({...a,tagName:e.tagName.toLowerCase()}),a}resolveRoot(e){if(e){const t=X(this.root,e);if(t)return t}return this.root}collectVisibleFields(e){return Array.from(e.querySelectorAll(Me)).filter(t=>!(t instanceof HTMLElement)||!Re(t)||t instanceof HTMLInputElement&&Ne.has((t.type||"").toLowerCase())?!1:!t.hasAttribute("disabled")&&!t.hasAttribute("readonly"))}detectTopLayerContainer(e){const t=document.activeElement instanceof HTMLElement?document.activeElement:null,s=t==null?void 0:t.closest(j);if(s instanceof HTMLElement&&e.some(r=>s.contains(r)))return s;const i=e.map(r=>r.closest(j)).filter(r=>r instanceof HTMLElement);return i.length?i.sort((r,a)=>e.filter(o=>a.contains(o)).length-e.filter(o=>r.contains(o)).length)[0]:document.body}}function _e(n,e){const t=n.getAttribute("data-smart-fill-id");if(t)return t;const i=w(n.getAttribute("data-smart-fill-key"))||w(n.getAttribute("name"))||w(n.id)||`smart-fill-${Date.now()}-${e}`;return n.setAttribute("data-smart-fill-id",i),i}function Oe(n){if(n instanceof HTMLTextAreaElement)return"textarea";if(n instanceof HTMLSelectElement)return"select";if(n instanceof HTMLInputElement){const e=(n.type||"text").toLowerCase();if(e==="radio")return"radio";if(e==="checkbox")return"checkbox";if(e==="date"||e==="month")return"date";if(e==="number")return"number"}return"text"}function Z(n){const e=n.getAttribute("aria-label");if(e)return e.trim();if(n.id){const r=document.querySelector(`label[for="${W(n.id)}"]`);if(r!=null&&r.textContent)return T(r.textContent)}const t=n.closest("label");if(t!=null&&t.textContent)return T(t.textContent);const s=n.closest(".form-item, .ant-form-item, .el-form-item, .field, .form-row"),i=s==null?void 0:s.querySelector("label, .ant-form-item-label, .el-form-item__label, .label");return i!=null&&i.textContent?T(i.textContent):G(n)||n.getAttribute("name")||n.id||"未命名字段"}function G(n){return n.getAttribute("placeholder")||n.getAttribute("aria-placeholder")||void 0}function $e(n){const e=n.closest("fieldset, section, .panel, .card, .form-section"),t=e==null?void 0:e.querySelector("legend, h1, h2, h3, .title, .section-title");return t!=null&&t.textContent?T(t.textContent):void 0}function De(n){if(n instanceof HTMLSelectElement)return Array.from(n.options).map(e=>({label:T(e.textContent||e.label),value:e.value}));if(n instanceof HTMLInputElement&&(n.type==="radio"||n.type==="checkbox")&&n.name)return Array.from(document.querySelectorAll(`input[name="${W(n.name)}"]`)).map(e=>({label:Z(e),value:e.value||!0}))}function Re(n){const e=window.getComputedStyle(n);return e.display==="none"||e.visibility==="hidden"||Number(e.opacity)===0?!1:!!(n.offsetWidth||n.offsetHeight||n.getClientRects().length)}function Pe(n,e){return n instanceof HTMLElement?n:typeof n=="string"?X(e,n):null}function X(n,e){try{const t=n.querySelector(e);return t instanceof HTMLElement?t:null}catch{return null}}function T(n){return n.replace(/[*::]/g,"").replace(/\s+/g," ").trim()}function W(n){return typeof CSS<"u"&&CSS.escape?CSS.escape(n):n.replace(/["\\]/g,"\\$&")}const K=5,ze=10*1024*1024,Ke=50*1024*1024;class He{constructor(e){c(this,"host",null);c(this,"root",null);c(this,"autoApplyState",null);c(this,"selectedFiles",[]);c(this,"inputText","");c(this,"localPriorityEnabled");c(this,"isOpen",!1);c(this,"dragState",null);c(this,"suppressOpenClick",!1);c(this,"isUploadHovering",!1);c(this,"handleDragMove",e=>{if(!this.dragState||!this.host||e.pointerId!==this.dragState.pointerId)return;const t=e.clientX-this.dragState.startX,s=e.clientY-this.dragState.startY;(Math.abs(t)>3||Math.abs(s)>3)&&(this.dragState.moved=!0);const i=this.host.getBoundingClientRect(),r=Math.max(8,window.innerWidth-i.width-8),a=Math.max(8,window.innerHeight-i.height-8),o=F(this.dragState.startLeft+t,8,r),d=F(this.dragState.startTop+s,8,a);this.host.style.left=`${o}px`,this.host.style.top=`${d}px`});c(this,"handleDragEnd",e=>{!this.dragState||e.pointerId!==this.dragState.pointerId||(this.suppressOpenClick=this.dragState.moved,this.dragState=null,document.removeEventListener("pointermove",this.handleDragMove),document.removeEventListener("pointerup",this.handleDragEnd),document.removeEventListener("pointercancel",this.handleDragEnd),this.suppressOpenClick&&window.setTimeout(()=>{this.suppressOpenClick=!1},0))});c(this,"handleDocumentPaste",e=>{const t=qe(e);if(!t.length||!this.isOpen||!this.host)return;const s=document.activeElement,i=this.root instanceof ShadowRoot?this.root.activeElement:null;!(s===this.host||this.host.contains(s)||i)&&!this.isUploadHovering||(e.preventDefault(),this.handleImages(t))});this.options=e,this.localPriorityEnabled=e.localPriorityEnabled??!1}mount(e){this.host=document.createElement("div"),this.host.className=`sf-sdk-host sf-sdk-host-${this.options.mode}`,e.appendChild(this.host),this.root=this.host.attachShadow?this.host.attachShadow({mode:"open"}):this.host,this.isOpen=this.options.initialOpen??this.options.mode==="inline",document.addEventListener("paste",this.handleDocumentPaste),this.render(this.isOpen)}setOpen(e){this.isOpen=e,this.render(this.isOpen),this.keepHostInViewport()}setLocalPriorityEnabled(e){this.localPriorityEnabled=e;const t=this.query('[data-role="local-priority-toggle"]');t&&(t.checked=e)}setBusy(e,t=""){const s=this.query('[data-role="recognize"]');s&&(s.disabled=e,s.textContent=e?t:this.t("recognize","智能识别"))}setStatus(e){const t=this.query('[data-role="status"]');t&&(t.textContent=e,t.removeAttribute("data-status"))}setError(e){const t=this.query('[data-role="status"]');t&&(t.textContent=e,t.setAttribute("data-status","error"))}setAutoApplyState(e){this.autoApplyState=e,this.render(!0),this.keepHostInViewport(),this.setStatus(this.t("recognized","识别完成,正在自动回填..."))}setApplyResult(e){const t=this.query('[data-role="status"]');t&&(t.textContent=`已回填 ${e.applied.length} 项,跳过 ${e.skipped.length} 项。`,t.setAttribute("data-status",e.skipped.length?"warning":"success"))}destroy(){var e;document.removeEventListener("pointermove",this.handleDragMove),document.removeEventListener("pointerup",this.handleDragEnd),document.removeEventListener("pointercancel",this.handleDragEnd),document.removeEventListener("paste",this.handleDocumentPaste),(e=this.host)==null||e.remove(),this.host=null,this.root=null,this.dragState=null,this.isUploadHovering=!1}render(e){this.root&&(this.root.innerHTML=`
|
|
2
|
-
<style>${
|
|
1
|
+
(function(p,b){typeof exports=="object"&&typeof module<"u"?b(exports):typeof define=="function"&&define.amd?define(["exports"],b):(p=typeof globalThis<"u"?globalThis:p||self,b(p.SmartFillSDK={}))})(this,function(p){"use strict";var At=Object.defineProperty;var It=(p,b,S)=>b in p?At(p,b,{enumerable:!0,configurable:!0,writable:!0,value:S}):p[b]=S;var c=(p,b,S)=>It(p,typeof b!="symbol"?b+"":b,S);const b="https://loan.kdbank.cn";class S extends Error{constructor(t){super(t.message);c(this,"smartFillError");this.name="SmartFillException",this.smartFillError=t}}function E(n,e,t,s={}){return new S({code:n,message:e,stage:t,...s})}function Ee(n,e,t="RECOGNIZE_FAILED"){return n instanceof S?n.smartFillError:{code:t,message:n instanceof Error?n.message:String(n||"智能录入异常"),stage:e,retryable:e==="recognize"}}function L(n="sf"){return`${n}_${Date.now().toString(36)}_${Math.random().toString(36).slice(2,10)}`}class xe{constructor(e){c(this,"seToken","");c(this,"baseURL",b);this.config=e}setAccessToken(e){this.seToken=e}async createSession(){if(!/^seKey-[A-Za-z0-9_-]{6,}$/.test(this.config.apiKey))throw E("API_KEY_INVALID","apiKey 格式不正确,应以 seKey- 开头。","setup");return{apiKey:this.config.apiKey,rulesVersion:"0.0.1"}}async getFormConfig(e){return e?this.request(`/sdk/forms/${encodeURIComponent(e)}`,{method:"GET"}):null}async resolveInputText(e){var r,a,o,d;const t=(r=e.images)!=null&&r.length?e.images:[],s=((a=e.text)==null?void 0:a.trim())||"";if(!t.length)return{text:s,usedOcr:!1};(o=e.onStatusChange)==null||o.call(e,"image_uploading");const i=await this.recognizeImages(t);return(d=e.onStatusChange)==null||d.call(e,"image_recognizing"),{text:i,usedOcr:!0}}async recognize(e){var a,o;const t=performance.now(),s=(a=e.text)==null?void 0:a.trim();(o=e.onStatusChange)==null||o.call(e,"recognizing");const i=await this.request("/fcloud/flow-product/agentChat/smartEntry",{method:"POST",body:JSON.stringify({formCode:e.formCode||"",configVersion:e.configVersion||"",formMsg:e.fields.map(({element:d,...l})=>l),userInputMsg:s||""})}),r=we(i,e.fields,e.scanToken);return{scanToken:e.scanToken,suggestions:r,trace:i.trace||{traceId:L("trace"),usedOcr:!!e.usedOcr,usedAi:!0,durationMs:Math.round(performance.now()-t)}}}async recognizeImages(e){const t=new FormData;e.forEach(r=>{t.append("image",r,r.name)});const s=await this.request("/fcloud/flow-product/agentChat/smartEntry/ocr",{method:"POST",body:t}),i=ke(s);if(!i)throw E("RECOGNIZE_FAILED","图片识别未提取到文本内容。","recognize");return i}async request(e,t){const s=new AbortController,i=window.setTimeout(()=>s.abort(),this.config.requestTimeoutMs??3e4),r=new Headers(t.headers);t.body&&!(t.body instanceof FormData)&&!r.has("Content-Type")&&r.set("Content-Type","application/json"),r.set("x-trace-id",L("trace")),this.seToken&&r.set("seToken",`${this.seToken}`);try{const a=await fetch(`${this.baseURL}${e}`,{...t,headers:r,signal:s.signal});if(!a.ok)throw E(Ae(a.status),await a.text(),e.includes("session")?"setup":"recognize",{retryable:a.status>=500||a.status===429});return a.json()}catch(a){throw a instanceof DOMException&&a.name==="AbortError"?E("RECOGNIZE_TIMEOUT","识别请求超时,请稍后重试。","recognize",{retryable:!0}):a}finally{window.clearTimeout(i)}}}const ve=.95;function we(n,e,t){var r;const s=new Map(e.map(a=>[a.fieldId,a]));return(((r=n.data)==null?void 0:r.fieldValues)||[]).filter(a=>!!(a!=null&&a.fieldId)).map(a=>{const o=s.get(a.fieldId);return{fieldId:a.fieldId,scanToken:t,label:a.label||(o==null?void 0:o.label)||a.fieldId,value:a.value,displayValue:a.value==null?"":String(a.value),confidence:Se(a.confidence),source:a.source||"ai",warnings:a.warnings}})}function Se(n){return typeof n!="number"||Number.isNaN(n)?ve:n<0?0:n>1?1:n}function ke(n){var t;const e=typeof n.data=="string"?n.data:((t=n.data)==null?void 0:t.text)||n.text||"";return String(e||"").trim()}function Ae(n){return n===401?"TOKEN_EXPIRED":n===403?"API_KEY_FORBIDDEN":n===404?"FORM_CONFIG_NOT_FOUND":"RECOGNIZE_FAILED"}class _{constructor(){c(this,"handlers",new Map)}on(e,t){const s=this.handlers.get(e)??new Set;return s.add(t),this.handlers.set(e,s),()=>this.off(e,t)}off(e,t){var s;(s=this.handlers.get(e))==null||s.delete(t)}emit(e,t){var s;(s=this.handlers.get(e))==null||s.forEach(i=>i(t))}clear(){this.handlers.clear()}}class Ie{constructor(){c(this,"instances",new Set);c(this,"active",null)}add(e){this.instances.add(e)}activate(e){this.active&&this.active!==e&&this.active.close(),this.active=e}remove(e){this.instances.delete(e),this.active===e&&(this.active=null)}destroyAll(){this.instances.forEach(e=>e.destroy()),this.instances.clear(),this.active=null}}const Ce=[".el-select",".el-date-editor",".el-time-picker",".el-switch",".el-checkbox-group",".el-radio-group",".el-cascader",".el-input-number"].join(", "),Y={name:"element",match:n=>n.matches(Ce),getValue:n=>{var t;const e=n.element;if(e)return e.matches(".el-switch")?e.classList.contains("is-checked")||e.getAttribute("aria-checked")==="true":e.matches(".el-radio-group")?Me(e,"radio"):e.matches(".el-checkbox-group")?Oe(e):((t=Z(e))==null?void 0:t.value)||e.textContent||void 0},setValue:(n,e)=>{const t=n.element;if(!t)return;if(t.matches(".el-switch")){Te(t,e);return}if(t.matches(".el-radio-group")){Fe(t,"radio",e);return}if(t.matches(".el-checkbox-group")){Le(t,e);return}const s=Z(t);if(s){Ne(s,e);return}t.dispatchEvent(new Event("change",{bubbles:!0}))}};function Te(n,e){(n.classList.contains("is-checked")||n.getAttribute("aria-checked")==="true")!==$e(e)&&R(n)}function Le(n,e){var i;const t=new Set(_e(e).map(I)),s=F(n,"checkbox");for(const r of s){const a=((i=r.input)==null?void 0:i.checked)||r.root.classList.contains("is-checked")||r.root.getAttribute("aria-checked")==="true",o=t.has(I(r.value))||t.has(I(r.label));a!==o&&R(r.root)}}function Fe(n,e,t){const s=I(String(t??"")),i=F(n,e).find(r=>I(r.value)===s||I(r.label)===s);i&&R(i.root)}function Me(n,e){var t;return(t=F(n,e).find(s=>{var i;return((i=s.input)==null?void 0:i.checked)||s.root.classList.contains("is-checked")}))==null?void 0:t.value}function Oe(n){return F(n,"checkbox").filter(e=>{var t;return((t=e.input)==null?void 0:t.checked)||e.root.classList.contains("is-checked")}).map(e=>e.value)}function F(n,e){return Array.from(n.querySelectorAll(`.el-${e}, [role="${e}"], input[type="${e}"]`)).map(t=>{const s=t instanceof HTMLInputElement?t:t.querySelector(`input[type="${e}"]`),i=t instanceof HTMLInputElement&&t.closest(`.el-${e}, [role="${e}"]`)||t,r=G(i.textContent||i.getAttribute("aria-label")||(s==null?void 0:s.value)||"");return{root:i,input:s,label:r,value:i.getAttribute("value")||(s==null?void 0:s.value)||r}})}function Z(n){return n.querySelector('input:not([type="hidden"]), textarea')}function Ne(n,e){n.value=String(e??""),n.dispatchEvent(new Event("input",{bubbles:!0})),n.dispatchEvent(new Event("change",{bubbles:!0})),n.dispatchEvent(new Event("blur",{bubbles:!0}))}function R(n){n.dispatchEvent(new MouseEvent("click",{bubbles:!0,cancelable:!0,view:window}))}function $e(n){return typeof n=="boolean"?n:["true","1","是","开启","启用","yes","y"].includes(String(n??"").trim().toLowerCase())}function _e(n){return Array.isArray(n)?n.map(String):String(n??"").split(/[,,;;、]/).map(e=>e.trim()).filter(Boolean)}function I(n){return G(n).toLowerCase()}function G(n){return n.replace(/\s+/g," ").trim()}const Re=[/^(el|rc|ant|radix|headlessui|mui|chakra)-/i,/[0-9a-f]{8,}/i,/\d{6,}/];function v(n){const e=String(n||"").trim();return!e||Re.some(t=>t.test(e))?"":e}function D(n){const e=(n.options||[]).slice(0,20).map(t=>`${t.label}:${String(t.value)}`).join("|");return C([n.fieldId,n.tagName,n.type,v(n.name),v(n.id),n.label,n.placeholder,n.section,e].join("::"))}function C(n){return String(n??"").replace(/\s+/g," ").trim().toLowerCase()}const P=new WeakMap;class z{constructor(e,t,s=[]){this.fields=e,this.schemas=t,this.adapters=s}async apply(e){const t=[],s=[];for(const i of e.values){const r=this.fields.find(l=>l.fieldId===i.fieldId);if(!r){s.push(M(i.fieldId,"",i.value,"字段不在当前扫描结果中","FIELD_NOT_FOUND"));continue}if(r.scanToken!==e.scanToken){s.push(M(r.fieldId,r.label,i.value,"页面扫描已过期,请重新扫描","SCAN_TOKEN_EXPIRED"));continue}const a=this.schemas.find(l=>l.fieldId===i.fieldId),o=a!=null&&a.transform?a.transform(i.value):i.value,d=await this.getValue(r,a);try{const l=a!=null&&a.validate?await a.validate(o):!0;if(l!==!0){s.push(M(r.fieldId,r.label,o,typeof l=="string"?l:"字段校验未通过","VALIDATE_FAILED"));continue}await this.setValue(r,o,a),t.push({fieldId:r.fieldId,label:r.label,value:o,previousValue:d})}catch(l){s.push(M(r.fieldId,r.label,o,l instanceof Error?l.message:"字段回填失败","SET_VALUE_FAILED"))}}return{applied:t,skipped:s,warnings:s.length?["部分字段未回填,请查看跳过原因。"]:void 0}}async getValue(e,t){if(t!=null&&t.getValue)return t.getValue();const s=this.matchAdapter(e);if(s!=null&&s.getValue)return s.getValue(e);const i=H(e);if(i)return i instanceof HTMLInputElement&&i.type==="checkbox"?i.checked:i instanceof HTMLInputElement&&i.type==="radio"?i.checked?i.value:void 0:i instanceof HTMLInputElement||i instanceof HTMLTextAreaElement||i instanceof HTMLSelectElement?i.value:i.textContent}async setValue(e,t,s){if(s!=null&&s.setValue){await s.setValue(t);return}const i=H(e);if(!i)throw new Error("页面中未找到对应字段");if(e.fingerprint&&e.fingerprint!==D({...e,tagName:i.tagName.toLowerCase()}))throw new Error("字段结构已变化,请重新扫描");if(e.disabled||e.readonly||i.hasAttribute("disabled")||i.hasAttribute("readonly"))throw new Error("字段不可编辑");const r=this.matchAdapter(e);if(r){await r.setValue(e,t);return}De(i,t),Pe(i)}matchAdapter(e){const t=H(e);return t?this.adapters.find(s=>s.match(t,e)):void 0}}function H(n){var e;return(e=n.element)!=null&&e.isConnected?n.element:document.querySelector(`[data-smart-fill-id="${K(n.fieldId)}"]`)}function De(n,e){if(n instanceof HTMLInputElement&&n.type==="checkbox")n.checked=!!e;else if(n instanceof HTMLInputElement&&n.type==="radio"){const t=document.querySelector(`input[type="radio"][name="${K(n.name)}"][value="${K(String(e))}"]`);(t||n).checked=!0}else n instanceof HTMLInputElement||n instanceof HTMLTextAreaElement||n instanceof HTMLSelectElement?n.value=String(e??""):n.isContentEditable&&(n.textContent=String(e??""));n.dispatchEvent(new Event("input",{bubbles:!0})),n.dispatchEvent(new Event("change",{bubbles:!0}))}function Pe(n){var s;n.setAttribute("data-smart-fill-highlighted","true"),(s=P.get(n))==null||s.abort();const e=new AbortController,t=i=>{i&&"isTrusted"in i&&!i.isTrusted||(n.removeAttribute("data-smart-fill-highlighted"),e.abort(),P.delete(n))};P.set(n,e),n.addEventListener("focus",t,{signal:e.signal}),n.addEventListener("pointerdown",t,{signal:e.signal}),n.addEventListener("keydown",t,{signal:e.signal}),n.addEventListener("input",t,{signal:e.signal}),n.addEventListener("change",t,{signal:e.signal})}function M(n,e,t,s,i){return{fieldId:n,label:e,attemptedValue:t,reason:s,reasonCode:i}}function K(n){return typeof CSS<"u"&&CSS.escape?CSS.escape(n):n.replace(/["\\]/g,"\\$&")}const ze=[{key:"mobile",pattern:new RegExp("(?<!\\d)1[3-9]\\d{9}(?!\\d)","g"),confidence:.98,reason:"手机号正则命中"},{key:"idCard",pattern:new RegExp("(?<!\\d)\\d{17}[\\dXx](?!\\d)","g"),confidence:.96,reason:"身份证号正则命中"},{key:"email",pattern:/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g,confidence:.95,reason:"邮箱正则命中"},{key:"bankCard",pattern:new RegExp("(?<!\\d)\\d{16,19}(?!\\d)","g"),confidence:.9,reason:"银行卡号正则命中"},{key:"amount",pattern:/(?:金额|价格|费用|合计|总计)[::\s]*([0-9]+(?:\.[0-9]{1,2})?)/g,confidence:.88,reason:"金额关键词命中"},{key:"date",pattern:/\d{4}[-/.年]\d{1,2}[-/.月]\d{1,2}日?/g,confidence:.86,reason:"日期格式命中"}],X={legalPerson:["法人","法定代表人","法定代表","企业法人","legal person","legal representative"],emergencyContact:["紧急联系人","紧急联络人","emergency contact"],contact:["联系人","联络人","联系人员","contact"],spouse:["配偶","爱人","夫妻","spouse"]},g={mobile:["手机","手机号","电话","联系电话","mobile","phone"],legalPersonMobile:["法人手机号","法人电话","法定代表人手机号","法人手机","legal person mobile"],emergencyContactMobile:["紧急联系人手机号","紧急联系人电话","紧急联系电话","emergency contact mobile"],contactMobile:["联系人手机号","联系人电话","联系手机","contact mobile"],spouseMobile:["配偶手机号","配偶电话","爱人手机号","spouse mobile"],idCard:["身份证","证件号","身份证号","idcard","id card"],legalPersonIdCard:["法人身份证","法人身份证号","法定代表人身份证号"],spouseIdCard:["配偶身份证","配偶身份证号","爱人身份证号"],email:["邮箱","邮件","email","mail"],contactEmail:["联系人邮箱","联系邮箱","contact email"],name:["姓名","名字","称呼","name"],customerName:["客户姓名","申请人姓名","用户姓名","借款人姓名","客户名称"],legalPersonName:["法人姓名","法定代表人姓名","企业法人姓名"],emergencyContactName:["紧急联系人姓名","紧急联系人名称"],contactName:["联系人姓名","联系人名称"],spouseName:["配偶姓名","爱人姓名"],companyName:["公司名称","企业名称","单位名称","商户名称","公司","企业"],address:["地址","住址","通讯地址","联系地址","现住址","办公地址"],detailAddress:["详细地址","街道地址","门牌地址","详细住址"],amount:["金额","费用","价格","合计","总计","amount","price"],applyAmount:["申请金额","贷款金额","借款金额","授信金额","申请额度"],date:["日期","时间","有效期","date"],applyDate:["申请日期","申请时间","受理日期","进件日期"],bankCard:["银行卡","卡号","bank","bank card"]},W={mobile:"mobile",legalPersonMobile:"mobile",emergencyContactMobile:"mobile",contactMobile:"mobile",spouseMobile:"mobile",idCard:"idCard",legalPersonIdCard:"idCard",spouseIdCard:"idCard",email:"email",contactEmail:"email",name:"name",customerName:"name",legalPersonName:"name",emergencyContactName:"name",contactName:"name",spouseName:"name",companyName:"companyName",address:"address",detailAddress:"address",amount:"amount",applyAmount:"amount",date:"date",applyDate:"date",bankCard:"bankCard"};class J{recognize(e,t,s){const i=Xe([...He(e),...Ke(e)]),r=new Set,a=[];for(const o of i.sort((d,l)=>l.confidence-d.confidence)){const d=qe(o,t,r);d&&(a.push({fieldId:d.fieldId,scanToken:s,label:d.label,value:Be(o.value,d),displayValue:o.value,confidence:o.confidence,source:"local_rule",reason:o.reason}),r.add(d.fieldId))}return a}}function He(n){const e=[];for(const t of ze)for(const s of n.matchAll(t.pattern)){const i=Q(s[1]||s[0]);e.push({key:t.key,value:i,confidence:t.confidence,reason:t.reason,baseKey:W[t.key]})}return e}function Ke(n){const e=[],t=n.split(/\r?\n|[;,;]/).map(s=>s.trim()).filter(Boolean);for(const s of t){const i=s.match(/^[“"'`]?([^::=]{2,30})[”"'`]?[::=]\s*(.+)$/);if(!i)continue;const r=Ge(i[1]),a=Q(i[2]);if(!(!r||!a))for(const o of Ve(r,a))e.push(o)}return e}function Ve(n,e){const t=C(n),s=Ye(e),i=[],r=je(t);return h(t,g.applyDate)&&i.push("applyDate"),h(t,g.applyAmount)&&i.push("applyAmount"),h(t,g.companyName)&&i.push("companyName"),h(t,g.detailAddress)&&i.push("detailAddress"),h(t,g.address)&&i.push("address"),(h(t,g.mobile)||s==="mobile")&&r&&V(i,r,"mobile"),(h(t,g.idCard)||s==="idCard")&&r&&V(i,r,"idCard"),(h(t,g.email)||s==="email")&&r&&V(i,r,"email"),(h(t,g.mobile)||s==="mobile")&&i.push("mobile"),(h(t,g.idCard)||s==="idCard")&&i.push("idCard"),(h(t,g.email)||s==="email")&&i.push("email"),(h(t,g.bankCard)||s==="bankCard")&&i.push("bankCard"),(h(t,g.amount)||s==="amount")&&i.push("amount"),(h(t,g.date)||s==="date")&&i.push("date"),We(i).map((a,o)=>({key:a,value:e,confidence:Math.max(.84,.96-o*.04),reason:r?`键值对文本命中(${n},角色增强)`:`键值对文本命中(${n})`,baseKey:W[a]}))}function qe(n,e,t){let s=null;for(const i of e){if(t.has(i.fieldId))continue;const r=Ue(n,i);r>((s==null?void 0:s.score)??0)&&(s={field:i,score:r})}return s&&s.score>=.45?s.field:null}function Ue(n,e){const t=C([e.fieldId,e.label,e.placeholder,e.name,e.id,e.section].join(" ")),s=g[n.key]||[n.key],i=g[n.baseKey]||[n.baseKey];let r=0;return h(t,s)?r+=.95:n.key!==n.baseKey&&h(t,i)?r+=.68:h(t,i)&&(r+=.82),t.includes(C(n.key))&&(r+=.22),e.required&&(r+=.05),n.baseKey==="amount"&&(e.type==="amount"||e.type==="number")&&(r+=.18),n.baseKey==="date"&&e.type==="date"&&(r+=.18),n.baseKey==="address"&&(e.type==="textarea"||e.type==="text")&&(r+=.12),["name","companyName","email","mobile","idCard","bankCard"].includes(n.baseKey)&&e.type==="text"&&(r+=.08),Math.min(r,1)}function Be(n,e){var t;return e.type==="date"?n.replace(/[年月/.]/g,"-").replace(/日/g,"").replace(/--/g,"-"):e.type==="number"||e.type==="amount"?((t=n.match(/[0-9]+(?:\.[0-9]+)?/))==null?void 0:t[0])??n:e.name&&/mobile|phone|idcard|bankcard/i.test(e.name)||e.id&&/mobile|phone|idcard|bankcard/i.test(e.id)||/mobile|phone|idcard|bankcard/i.test(e.fieldId)?n.replace(/\s+/g,""):n}function je(n){const e=Object.keys(X).filter(t=>h(n,X[t]));return e.length?e.includes("emergencyContact")?"emergencyContact":e.includes("legalPerson")?"legalPerson":e.includes("spouse")?"spouse":"contact":null}function Ye(n){const e=n.replace(/\s+/g,"");return/^1[3-9]\d{9}$/.test(e)?"mobile":/^\d{17}[\dXx]$/.test(e)?"idCard":/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(n)?"email":/^\d{16,19}$/.test(e)?"bankCard":/^\d{4}[-/.年]\d{1,2}[-/.月]\d{1,2}日?$/.test(n)?"date":/^[0-9]+(?:\.[0-9]{1,2})?$/.test(e)?"amount":/[省市区县路街道号栋室乡镇]/.test(n)?"address":/^[\u4e00-\u9fa5a-zA-Z·]{2,20}$/.test(n)?"name":"unknown"}function V(n,e,t){var i;const s=(i=Ze[e])==null?void 0:i[t];s&&n.push(s)}const Ze={legalPerson:{mobile:"legalPersonMobile",idCard:"legalPersonIdCard",name:"legalPersonName"},emergencyContact:{mobile:"emergencyContactMobile",name:"emergencyContactName"},contact:{mobile:"contactMobile",email:"contactEmail",name:"contactName"},spouse:{mobile:"spouseMobile",idCard:"spouseIdCard",name:"spouseName"}};function Ge(n){return n.replace(/[“”"'`]/g,"").replace(/\s+/g," ").trim()}function Q(n){return n.replace(/[,。;;]+$/g,"").replace(/^[“”"'`\s]+|[“”"'`\s]+$/g,"").trim()}function h(n,e){const t=C(n);return e.some(s=>t.includes(C(s)))}function Xe(n){const e=new Map;for(const t of n){const s=`${t.key}::${t.value}`,i=e.get(s);(!i||i.confidence<t.confidence)&&e.set(s,t)}return[...e.values()]}function We(n){return[...new Set(n)]}const Je=["input","textarea","select","[contenteditable]:not([contenteditable='false'])"].join(", "),ee=[".el-select",".el-date-editor",".el-time-picker",".el-switch",".el-checkbox-group",".el-radio-group",".el-cascader",".el-input-number"].join(", "),te=["dialog[open]","[role='dialog']","[aria-modal='true']",".modal",".modal-dialog",".ant-modal",".ant-drawer",".el-dialog",".el-drawer",".n-dialog",".n-modal",".popup",".drawer"].join(", "),Qe=new Set(["hidden","submit","button","image","reset","file","password"]);class ne{constructor(e=document){this.root=e}scan(e={}){var d;const t=L("scan");if((d=e.registered)!=null&&d.length)return{scanToken:t,fields:e.registered.map(l=>this.fromSchema(l,t))};const s=this.resolveRoot(e.scanContainer),i=this.collectVisibleFields(s),r=this.detectTopLayerContainer(i),a=i.filter(l=>r.contains(l)),o=(a.length?a:i).slice(0,e.maxFields??200);return{scanToken:t,fields:o.map((l,u)=>this.fromElement(l,u,t))}}fromSchema(e,t){const s=ot(e.element,this.root),i={fieldId:e.rowKey==null?e.fieldId:`${e.fieldId}:${e.rowKey}`,label:e.label,type:e.type,localRuleMode:e.localRuleMode??"inherit",required:e.required,section:e.section,options:e.options,source:"registered",scanToken:t,element:s||void 0,fingerprint:""};return i.fingerprint=D({...i,tagName:s==null?void 0:s.tagName.toLowerCase()}),i}fromElement(e,t,s){const i=et(e,t),r=st(e),a={fieldId:i,fingerprint:"",scanToken:s,type:tt(e),localRuleMode:"inherit",label:q(e),placeholder:se(e),name:v(oe(e))||void 0,id:v(e.id)||void 0,section:nt(e),options:r,required:rt(e),readonly:it(e),disabled:U(e),source:"form_scan",element:e};return a.fingerprint=D({...a,tagName:e.tagName.toLowerCase()}),a}resolveRoot(e){if(e){const t=le(this.root,e);if(t)return t}return this.root}collectVisibleFields(e){const t=Array.from(e.querySelectorAll(ee)).filter(i=>!(i instanceof HTMLElement)||!ie(i)||U(i)?!1:re(i));return[...Array.from(e.querySelectorAll(Je)).filter(i=>!(i instanceof HTMLElement)||!ie(i)||at(i)||i instanceof HTMLInputElement&&Qe.has((i.type||"").toLowerCase())?!1:!U(i)&&!i.hasAttribute("readonly")),...t]}detectTopLayerContainer(e){const t=document.activeElement instanceof HTMLElement?document.activeElement:null,s=t==null?void 0:t.closest(te);if(s instanceof HTMLElement&&e.some(r=>s.contains(r)))return s;const i=e.map(r=>r.closest(te)).filter(r=>r instanceof HTMLElement);return i.length?i.sort((r,a)=>e.filter(o=>a.contains(o)).length-e.filter(o=>r.contains(o)).length)[0]:document.body}}function et(n,e){const t=n.getAttribute("data-smart-fill-id");if(t)return t;const i=v(n.getAttribute("data-smart-fill-key"))||v(oe(n))||v(n.id)||v(q(n))||`smart-fill-${Date.now()}-${e}`;return n.setAttribute("data-smart-fill-id",i),i}function tt(n){const e=ae(n);if(e)return e;if(n instanceof HTMLTextAreaElement)return"textarea";if(n instanceof HTMLSelectElement)return"select";if(n instanceof HTMLInputElement){const t=(n.type||"text").toLowerCase();if(t==="radio")return"radio";if(t==="checkbox")return"checkbox";if(t==="date"||t==="month")return"date";if(t==="number")return"number"}return"text"}function q(n){const e=n.getAttribute("aria-label");if(e)return e.trim();if(n.id){const r=document.querySelector(`label[for="${ce(n.id)}"]`);if(r!=null&&r.textContent)return x(r.textContent)}const t=n.closest("label");if(t!=null&&t.textContent)return x(t.textContent);const s=n.closest(".form-item, .ant-form-item, .el-form-item, .field, .form-row"),i=s==null?void 0:s.querySelector("label, .ant-form-item-label, .el-form-item__label, .label");return i!=null&&i.textContent?x(i.textContent):se(n)||n.getAttribute("name")||n.id||"未命名字段"}function se(n){var e;return n.getAttribute("placeholder")||n.getAttribute("aria-placeholder")||((e=n.querySelector("[placeholder]"))==null?void 0:e.getAttribute("placeholder"))||void 0}function nt(n){const e=n.closest("fieldset, section, .panel, .card, .form-section"),t=e==null?void 0:e.querySelector("legend, h1, h2, h3, .title, .section-title");return t!=null&&t.textContent?x(t.textContent):void 0}function st(n){if(n.matches(".el-radio-group"))return Array.from(n.querySelectorAll('.el-radio, [role="radio"], input[type="radio"]')).map(e=>{var t;return{label:x(e.textContent||e.getAttribute("aria-label")||e.getAttribute("value")||""),value:e.getAttribute("value")||((t=e.querySelector('input[type="radio"]'))==null?void 0:t.value)||x(e.textContent||"")}}).filter(e=>e.label||e.value);if(n.matches(".el-checkbox-group"))return Array.from(n.querySelectorAll('.el-checkbox, [role="checkbox"], input[type="checkbox"]')).map(e=>{var t;return{label:x(e.textContent||e.getAttribute("aria-label")||e.getAttribute("value")||""),value:e.getAttribute("value")||((t=e.querySelector('input[type="checkbox"]'))==null?void 0:t.value)||x(e.textContent||"")}}).filter(e=>e.label||e.value);if(n instanceof HTMLSelectElement)return Array.from(n.options).map(e=>({label:x(e.textContent||e.label),value:e.value}));if(n instanceof HTMLInputElement&&(n.type==="radio"||n.type==="checkbox")&&n.name)return Array.from(document.querySelectorAll(`input[name="${ce(n.name)}"]`)).map(e=>({label:q(e),value:e.value||!0}))}function ie(n){const e=window.getComputedStyle(n);return e.display==="none"||e.visibility==="hidden"||Number(e.opacity)===0?!1:!!(n.offsetWidth||n.offsetHeight||n.getClientRects().length)}function U(n){return n.hasAttribute("disabled")||n.getAttribute("aria-disabled")==="true"||n.classList.contains("is-disabled")||!!n.querySelector('[disabled], [aria-disabled="true"], .is-disabled')}function it(n){return n.hasAttribute("readonly")||!!n.querySelector("[readonly]")}function rt(n){return n.hasAttribute("required")||n.getAttribute("aria-required")==="true"||!!n.querySelector('[required], [aria-required="true"]')}function at(n){const e=n.closest(ee);return e instanceof HTMLElement?re(e):!1}function re(n){return!!ae(n)}function ae(n){return n.matches(".el-select, .el-cascader")?"select":n.matches(".el-date-editor, .el-time-picker")?"date":n.matches(".el-switch")||n.matches(".el-checkbox-group")?"checkbox":n.matches(".el-radio-group")?"radio":n.matches(".el-input-number")?"number":null}function oe(n){var e;return n.getAttribute("name")||((e=n.querySelector("[name]"))==null?void 0:e.getAttribute("name"))||n.getAttribute("data-smart-fill-key")}function ot(n,e){return n instanceof HTMLElement?n:typeof n=="string"?le(e,n):null}function le(n,e){try{const t=n.querySelector(e);return t instanceof HTMLElement?t:null}catch{return null}}function x(n){return n.replace(/[*::]/g,"").replace(/\s+/g," ").trim()}function ce(n){return typeof CSS<"u"&&CSS.escape?CSS.escape(n):n.replace(/["\\]/g,"\\$&")}const B=5,lt=10*1024*1024,ct=50*1024*1024;class dt{constructor(e){c(this,"host",null);c(this,"root",null);c(this,"autoApplyState",null);c(this,"selectedFiles",[]);c(this,"inputText","");c(this,"localPriorityEnabled");c(this,"isOpen",!1);c(this,"dragState",null);c(this,"suppressOpenClick",!1);c(this,"isUploadHovering",!1);c(this,"handleDragMove",e=>{if(!this.dragState||!this.host||e.pointerId!==this.dragState.pointerId)return;const t=e.clientX-this.dragState.startX,s=e.clientY-this.dragState.startY;(Math.abs(t)>3||Math.abs(s)>3)&&(this.dragState.moved=!0);const i=this.host.getBoundingClientRect(),r=Math.max(8,window.innerWidth-i.width-8),a=Math.max(8,window.innerHeight-i.height-8),o=O(this.dragState.startLeft+t,8,r),d=O(this.dragState.startTop+s,8,a);this.host.style.left=`${o}px`,this.host.style.top=`${d}px`});c(this,"handleDragEnd",e=>{!this.dragState||e.pointerId!==this.dragState.pointerId||(this.suppressOpenClick=this.dragState.moved,this.dragState=null,document.removeEventListener("pointermove",this.handleDragMove),document.removeEventListener("pointerup",this.handleDragEnd),document.removeEventListener("pointercancel",this.handleDragEnd),this.suppressOpenClick&&window.setTimeout(()=>{this.suppressOpenClick=!1},0))});c(this,"handleDocumentPaste",e=>{const t=ft(e);if(!t.length||!this.isOpen||!this.host)return;const s=document.activeElement,i=this.root instanceof ShadowRoot?this.root.activeElement:null;!(s===this.host||this.host.contains(s)||i)&&!this.isUploadHovering||(e.preventDefault(),this.handleImages(t))});this.options=e,this.localPriorityEnabled=e.localPriorityEnabled??!1}mount(e){this.host=document.createElement("div"),this.host.className=`sf-sdk-host sf-sdk-host-${this.options.mode}`,e.appendChild(this.host),this.root=this.host.attachShadow?this.host.attachShadow({mode:"open"}):this.host,this.isOpen=this.options.initialOpen??this.options.mode==="inline",document.addEventListener("paste",this.handleDocumentPaste),this.render(this.isOpen)}setOpen(e){this.isOpen=e,this.render(this.isOpen),this.keepHostInViewport()}setLocalPriorityEnabled(e){this.localPriorityEnabled=e;const t=this.query('[data-role="local-priority-toggle"]');t&&(t.checked=e)}setBusy(e,t=""){const s=this.query('[data-role="recognize"]');s&&(s.disabled=e,s.textContent=e?t:this.t("recognize","智能识别"))}setStatus(e){const t=this.query('[data-role="status"]');t&&(t.textContent=e,t.removeAttribute("data-status"))}setError(e){const t=this.query('[data-role="status"]');t&&(t.textContent=e,t.setAttribute("data-status","error"))}setAutoApplyState(e){this.autoApplyState=e,this.render(!0),this.keepHostInViewport(),this.setStatus(this.t("recognized","识别完成,正在自动回填..."))}setApplyResult(e){const t=this.query('[data-role="status"]');t&&(t.textContent=`已回填 ${e.applied.length} 项,跳过 ${e.skipped.length} 项。`,t.setAttribute("data-status",e.skipped.length?"warning":"success"))}destroy(){var e;document.removeEventListener("pointermove",this.handleDragMove),document.removeEventListener("pointerup",this.handleDragEnd),document.removeEventListener("pointercancel",this.handleDragEnd),document.removeEventListener("paste",this.handleDocumentPaste),(e=this.host)==null||e.remove(),this.host=null,this.root=null,this.dragState=null,this.isUploadHovering=!1}render(e){this.root&&(this.root.innerHTML=`
|
|
2
|
+
<style>${pt}</style>
|
|
3
3
|
${this.options.mode==="floating"?`<button class="sf-float" type="button" data-role="open">${this.t("entry","智能录入")}</button>`:""}
|
|
4
4
|
<section class="sf-panel ${e?"is-open":""} ${this.options.mode==="inline"?"is-inline":"is-floating"}" aria-label="智能录入面板">
|
|
5
5
|
<header class="sf-header">
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
</label>
|
|
17
17
|
<div class="sf-entry-grid">
|
|
18
18
|
<div class="sf-textarea-wrap">
|
|
19
|
-
<textarea class="sf-textarea" data-role="text" placeholder="${this.t("placeholder","粘贴文本,如:姓名:张三 手机号:13800000000")}">${
|
|
19
|
+
<textarea class="sf-textarea" data-role="text" placeholder="${this.t("placeholder","粘贴文本,如:姓名:张三 手机号:13800000000")}">${ut(this.inputText)}</textarea>
|
|
20
20
|
<div class="sf-textarea-actions">
|
|
21
21
|
<button class="sf-btn sf-btn-secondary" type="button" data-role="clear">${this.t("clear","清空")}</button>
|
|
22
22
|
<button class="sf-btn sf-btn-primary" type="button" data-role="recognize">${this.t("recognize","智能识别")}</button>
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
<circle cx="10" cy="10.5" r="1" fill="currentColor"/>
|
|
34
34
|
</svg>
|
|
35
35
|
</span>
|
|
36
|
-
<span class="sf-upload-hint">${this.t("uploadHint",`点击、拖拽、Ctrl + V 粘贴图片至此(最多 ${
|
|
36
|
+
<span class="sf-upload-hint">${this.t("uploadHint",`点击、拖拽、Ctrl + V 粘贴图片至此(最多 ${B} 张)`)}</span>
|
|
37
37
|
</span>
|
|
38
38
|
<span class="sf-upload-btn" data-role="file-label">图片识别</span>
|
|
39
39
|
</label>
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
</div>
|
|
44
44
|
</div>
|
|
45
45
|
</section>
|
|
46
|
-
`,this.bindEvents())}bindEvents(){var s,i,r,a,o,d,l;const e=this.query('[data-role="open"]');e==null||e.addEventListener("pointerdown",u=>this.startDrag(u)),e==null||e.addEventListener("click",()=>{this.suppressOpenClick||this.options.onOpen()}),(s=this.query(".sf-header"))==null||s.addEventListener("pointerdown",u=>this.startDrag(u)),(i=this.query('[data-role="close"]'))==null||i.addEventListener("click",()=>{if(this.options.mode==="inline"){this.isOpen?this.options.onClose():this.options.onOpen();return}this.options.onClose()}),(r=this.query('[data-role="file"]'))==null||r.addEventListener("change",u=>{const m=u.target;this.handleImages(Array.from(m.files||[]))});const t=this.query('[data-role="upload"]');t==null||t.addEventListener("pointerenter",()=>{this.isUploadHovering=!0}),t==null||t.addEventListener("pointerleave",()=>{this.isUploadHovering=!1,t.removeAttribute("data-dragover")}),t==null||t.addEventListener("dragenter",u=>{u.preventDefault(),this.isUploadHovering=!0,t.setAttribute("data-dragover","true")}),t==null||t.addEventListener("dragover",u=>{u.preventDefault(),t.setAttribute("data-dragover","true")}),t==null||t.addEventListener("dragleave",u=>{u.currentTarget.contains(u.relatedTarget)||(this.isUploadHovering=!1,t.removeAttribute("data-dragover"))}),t==null||t.addEventListener("drop",u=>{var m;u.preventDefault(),this.isUploadHovering=!1,t.removeAttribute("data-dragover"),this.handleImages(Array.from(((m=u.dataTransfer)==null?void 0:m.files)||[]))}),(a=this.query('[data-role="text"]'))==null||a.addEventListener("input",u=>{this.inputText=u.target.value}),(o=this.query('[data-role="local-priority-toggle"]'))==null||o.addEventListener("change",u=>{var m,
|
|
46
|
+
`,this.bindEvents())}bindEvents(){var s,i,r,a,o,d,l;const e=this.query('[data-role="open"]');e==null||e.addEventListener("pointerdown",u=>this.startDrag(u)),e==null||e.addEventListener("click",()=>{this.suppressOpenClick||this.options.onOpen()}),(s=this.query(".sf-header"))==null||s.addEventListener("pointerdown",u=>this.startDrag(u)),(i=this.query('[data-role="close"]'))==null||i.addEventListener("click",()=>{if(this.options.mode==="inline"){this.isOpen?this.options.onClose():this.options.onOpen();return}this.options.onClose()}),(r=this.query('[data-role="file"]'))==null||r.addEventListener("change",u=>{const m=u.target;this.handleImages(Array.from(m.files||[]))});const t=this.query('[data-role="upload"]');t==null||t.addEventListener("pointerenter",()=>{this.isUploadHovering=!0}),t==null||t.addEventListener("pointerleave",()=>{this.isUploadHovering=!1,t.removeAttribute("data-dragover")}),t==null||t.addEventListener("dragenter",u=>{u.preventDefault(),this.isUploadHovering=!0,t.setAttribute("data-dragover","true")}),t==null||t.addEventListener("dragover",u=>{u.preventDefault(),t.setAttribute("data-dragover","true")}),t==null||t.addEventListener("dragleave",u=>{u.currentTarget.contains(u.relatedTarget)||(this.isUploadHovering=!1,t.removeAttribute("data-dragover"))}),t==null||t.addEventListener("drop",u=>{var m;u.preventDefault(),this.isUploadHovering=!1,t.removeAttribute("data-dragover"),this.handleImages(Array.from(((m=u.dataTransfer)==null?void 0:m.files)||[]))}),(a=this.query('[data-role="text"]'))==null||a.addEventListener("input",u=>{this.inputText=u.target.value}),(o=this.query('[data-role="local-priority-toggle"]'))==null||o.addEventListener("change",u=>{var m,k;this.localPriorityEnabled=u.target.checked,(k=(m=this.options).onLocalPriorityChange)==null||k.call(m,this.localPriorityEnabled)}),(d=this.query('[data-role="clear"]'))==null||d.addEventListener("click",()=>{this.clearFormState()}),(l=this.query('[data-role="recognize"]'))==null||l.addEventListener("click",()=>{const u=this.query('[data-role="text"]'),m=u==null?void 0:u.value.trim();if(this.inputText=(u==null?void 0:u.value)||this.inputText,!m){this.setError(this.t("emptyInput","请输入文本内容。"));return}this.options.onRecognize({text:m})})}clearFormState(){this.inputText="",this.resetSelectedFiles(),this.autoApplyState=null,this.render(!0),this.setStatus(this.t("empty","暂无识别结果"))}resetSelectedFiles(){var t;this.selectedFiles=[];const e=this.query('[data-role="file"]');e&&(e.value=""),this.isUploadHovering=!1,(t=this.query('[data-role="upload"]'))==null||t.removeAttribute("data-dragover")}async handleImages(e){const t=e.filter(r=>r.type.startsWith("image/"));if(!t.length){this.resetSelectedFiles(),this.setError(this.t("invalidImageError","请选择图片文件。"));return}if(t.length>B){this.resetSelectedFiles(),this.setError(this.t("maxFilesError",`最多上传 ${B} 张图片。`));return}if(t.find(r=>r.size>lt)){this.resetSelectedFiles(),this.setError(this.t("maxSingleFileSizeError","单张图片不能超过 10MB。"));return}if(t.reduce((r,a)=>r+a.size,0)>ct){this.resetSelectedFiles(),this.setError(this.t("maxTotalFileSizeError","上传图片总大小不能超过 50MB。"));return}this.selectedFiles=t,this.setStatus(this.t("imageReady",`已选择 ${this.selectedFiles.length} 张图片,开始识别...`));try{await this.options.onRecognize({images:[...this.selectedFiles]}),this.resetSelectedFiles()}catch{}}startDrag(e){var s;if(this.options.mode!=="floating"||!this.host||e.button!==0||(s=e.target)!=null&&s.closest('[data-role="close"]'))return;const t=this.host.getBoundingClientRect();this.host.style.left=`${t.left}px`,this.host.style.top=`${t.top}px`,this.host.style.right="auto",this.host.style.bottom="auto",this.dragState={pointerId:e.pointerId,startX:e.clientX,startY:e.clientY,startLeft:t.left,startTop:t.top,moved:!1},e.preventDefault(),document.addEventListener("pointermove",this.handleDragMove),document.addEventListener("pointerup",this.handleDragEnd),document.addEventListener("pointercancel",this.handleDragEnd)}keepHostInViewport(){this.options.mode!=="floating"||!this.host||!this.host.style.left||!this.host.style.top||window.requestAnimationFrame(()=>{if(!this.host)return;const e=this.host.getBoundingClientRect(),t=O(e.left,8,Math.max(8,window.innerWidth-e.width-8)),s=O(e.top,8,Math.max(8,window.innerHeight-e.height-8));this.host.style.left=`${t}px`,this.host.style.top=`${s}px`})}query(e){var t;return((t=this.root)==null?void 0:t.querySelector(e))||null}t(e,t){var s;return((s=this.options.messages)==null?void 0:s[e])||t}}function ut(n){return n.replace(/[&<>"']/g,e=>({"&":"&","<":"<",">":">",'"':""","'":"'"})[e]||e)}function O(n,e,t){return Math.min(Math.max(n,e),t)}function ft(n){var t;return Array.from(((t=n.clipboardData)==null?void 0:t.items)||[]).filter(s=>s.kind==="file"&&s.type.startsWith("image/")).map(s=>s.getAsFile()).filter(s=>!!s)}const pt=`
|
|
47
47
|
:host, .sf-panel { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }
|
|
48
48
|
:host(.sf-sdk-host-floating), .sf-sdk-host-floating { position: fixed; top: 24px; right: 24px; z-index: 2147483647; display: flex; flex-direction: column; align-items: flex-end; gap: 12px; width: min(420px, calc(100vw - 32px)); pointer-events: none; }
|
|
49
49
|
.sf-float { align-self: flex-end; border: 0; border-radius: 999px; padding: 12px 18px; color: #fff; background: #2563eb; box-shadow: 0 10px 24px rgba(37,99,235,.3); cursor: move; user-select: none; touch-action: none; pointer-events: auto; }
|
|
@@ -89,4 +89,4 @@
|
|
|
89
89
|
[data-status="error"] { color: #dc2626; }
|
|
90
90
|
[data-status="warning"] { color: #d97706; }
|
|
91
91
|
[data-status="success"] { color: #16a34a; }
|
|
92
|
-
`,Be=.75;class J{constructor(e,t){c(this,"events",new _);c(this,"scanner");c(this,"ruleEngine",new U);c(this,"panel",null);c(this,"panels",[]);c(this,"panelStorageKeys",new Map);c(this,"registeredFields",[]);c(this,"adapters",[]);c(this,"scanResult",null);c(this,"autoApplyState",null);c(this,"formConfigVersion");c(this,"localPriorityEnabled",!1);c(this,"destroyed",!1);this.config=e,this.context=t,this.scanner=new Y(e.root||document),this.localPriorityEnabled=Je(),this.context.manager.add(this)}on(e,t){return this.events.on(e,t)}useAdapter(e){return this.assertAlive(),this.adapters.push(e),this}registerFields(e){this.assertAlive();const t=new Set;for(const s of e){const i=s.rowKey==null?s.fieldId:`${s.fieldId}:${s.rowKey}`;if(t.has(i))throw E("UNSUPPORTED_PAGE",`字段 ${i} 重复注册。`,"scan");t.add(i)}return this.registeredFields=e,this}unregisterFields(e){this.assertAlive(),this.registeredFields=e!=null&&e.length?this.registeredFields.filter(t=>!e.includes(t.fieldId)):[]}mount(e){this.assertAlive();const t=typeof e=="string"?document.querySelector(e):e;if(!t)throw E("UNSUPPORTED_PAGE","未找到智能录入挂载点。","ui");const s=te("inline",We(e,t)),i=ne(s,!0);return this.createPanel("inline",s,i).mount(t),this}mountFloatingButton(){this.assertAlive();const e=te("floating"),t=ne(e,!1);return this.createPanel("floating",e,t).mount(document.body),this}createPanel(e,t,s){let i;return i=new He({mode:e,initialOpen:s,messages:this.config.messages,localPriorityEnabled:this.localPriorityEnabled,onOpen:()=>this.open(i),onClose:()=>this.close(i),onRecognize:r=>(this.panel=i,this.recognize(r)),onLocalPriorityChange:r=>this.handleLocalPriorityChange(r,i)}),this.panels.push(i),this.panelStorageKeys.set(i,t),this.panel=i,i}handleLocalPriorityChange(e,t){this.localPriorityEnabled=e,Qe(e);for(const s of this.panels)s!==t&&s.setLocalPriorityEnabled(e)}async open(e=this.panel){this.assertAlive(),e&&(this.context.manager.activate(this),this.panel=e,e.setOpen(!0),se(this.panelStorageKeys.get(e),!0),await this.rescan())}close(e=this.panel){e&&(this.panel=e,e.setOpen(!1),se(this.panelStorageKeys.get(e),!1))}async rescan(){var e;this.assertAlive();try{this.formConfigVersion=void 0;const t=this.registeredFields.length?this.registeredFields:void 0;if(this.scanResult=this.scanner.scan({registered:t,maxFields:this.config.maxFields}),!this.scanResult.fields.length)throw E("NO_FIELDS_FOUND","当前页面未找到可回填字段。","scan");return this.events.emit("scanCompleted",{scanToken:this.scanResult.scanToken,fieldCount:this.scanResult.fields.length}),(e=this.panel)==null||e.setStatus(`已扫描 ${this.scanResult.fields.length} 个字段。`),this.scanResult}catch(t){throw this.emitError(t,"scan"),t}}async recognize(e){var r,a,o;this.assertAlive();const t=this.scanResult||await this.rescan(),s=C("trace"),i=performance.now();this.events.emit("recognizing",{scanToken:t.scanToken,traceId:s}),(r=this.panel)==null||r.setBusy(!0,"识别中...");try{const d=t.fields.filter(b=>Ge(b.localRuleMode,this.localPriorityEnabled)),l=t.fields.filter(b=>b.localRuleMode!=="only"),{text:u,usedOcr:m}=await this.context.client.resolveInputText({text:e.text,images:e.images,onStatusChange:b=>{var M,I,N;if(b==="image_uploading"){(M=this.panel)==null||M.setStatus("图片上传中...");return}if(b==="image_recognizing"){(I=this.panel)==null||I.setStatus("图片识别中...");return}(N=this.panel)==null||N.setStatus("识别中...")}}),S=u?this.ruleEngine.recognize(u,d,t.scanToken):[];let x;if(!l.length)x=ee(t.scanToken,s,S,i,void 0,m);else try{const b=await this.context.client.recognize({scanToken:t.scanToken,formCode:this.config.formCode,configVersion:this.formConfigVersion,text:u,usedOcr:m,fields:l,onStatusChange:()=>{var I;(I=this.panel)==null||I.setStatus("识别中...")}});b.trace.durationMs=b.trace.durationMs||Math.round(performance.now()-i);const M=Ze(S,b.suggestions.filter(I=>l.some(N=>N.fieldId===I.fieldId)));x={...b,suggestions:M}}catch(b){if(!S.length)throw b;x=ee(t.scanToken,s,S,i,[m?"后端识别失败,已使用 OCR 文本触发本地识别继续回填。":"后端识别失败,已启用本地识别继续回填。"],m)}return this.autoApplyState=je(x.scanToken,x.trace.traceId,x.suggestions,t,this.registeredFields),this.events.emit("recognized",x),(a=this.panel)==null||a.setAutoApplyState(this.autoApplyState),await this.applyAutoItems(this.autoApplyState),x}catch(d){throw this.emitError(d,"recognize"),d}finally{(o=this.panel)==null||o.setBusy(!1)}}async apply(e){var i;if(this.assertAlive(),!this.scanResult)throw E("SCAN_TOKEN_EXPIRED","请先扫描字段后再回填。","apply");this.events.emit("applying",{scanToken:e.scanToken,count:e.values.length});const s=await new D(this.scanResult.fields,this.registeredFields,this.adapters).apply(e);return this.events.emit("applied",s),(i=this.panel)==null||i.setApplyResult(s),s}async applyAutoItems(e){var o,d;if(!this.scanResult)throw E("SCAN_TOKEN_EXPIRED","请先扫描字段后再回填。","apply");const t=Ye(e),s=e.items.filter(l=>Q(l)).map(l=>({fieldId:l.fieldId,value:l.value,source:l.source}));this.events.emit("applying",{scanToken:e.scanToken,count:s.length}),(o=this.panel)==null||o.setStatus("识别完成,正在自动回填...");const r=await new D(this.scanResult.fields,this.registeredFields,this.adapters).apply({scanToken:e.scanToken,values:s}),a={...r,skipped:[...t,...r.skipped],warnings:[...r.warnings||[],...t.length?["部分字段因置信度或风险策略被跳过。"]:[]]};return this.events.emit("applied",a),(d=this.panel)==null||d.setApplyResult(a),a}destroy(){if(!this.destroyed){this.destroyed=!0;for(const e of this.panels)e.destroy();this.panels.length=0,this.panelStorageKeys.clear(),this.panel=null,this.events.clear(),this.context.manager.remove(this)}}assertAlive(){if(this.destroyed)throw E("INSTANCE_DESTROYED","实例已销毁。","ui")}emitError(e,t){var i;const s=oe(e,t);this.events.emit("error",s),(i=this.panel)==null||i.setError(s.message)}}function je(n,e,t,s,i){return{scanToken:n,traceId:e,items:t.map(r=>{const a=s.fields.find(l=>l.fieldId===r.fieldId),o=i.find(l=>l.fieldId===r.fieldId),d=o!=null&&o.getValue?o.getValue():Xe(a==null?void 0:a.element);return{applyItemId:`${r.fieldId}_${Math.random().toString(36).slice(2,8)}`,fieldId:r.fieldId,scanToken:n,groupId:r.groupId,label:r.label,value:r.value,currentValue:d,displayValue:r.displayValue||String(r.value??""),confidence:r.confidence,source:r.source,warnings:r.warnings,previousValue:d}})}}function Q(n){var e;return n.confidence>=Be&&!((e=n.warnings)!=null&&e.length)}function Ye(n){return n.items.filter(e=>!Q(e)).map(e=>{var t,s;return{fieldId:e.fieldId,label:e.label,attemptedValue:e.value,reason:((t=e.warnings)==null?void 0:t.join(";"))||`置信度 ${Math.round(e.confidence*100)}% 低于自动回填阈值`,reasonCode:(s=e.warnings)!=null&&s.length?"AUTO_APPLY_WARNING":"LOW_CONFIDENCE"}})}function Ze(n,e){const t=new Map;for(const s of e)t.set(s.fieldId,s);for(const s of n)t.has(s.fieldId)||t.set(s.fieldId,s);return[...t.values()]}function Ge(n,e){return n==="only"?!0:n==="off"?!1:e}function ee(n,e,t,s,i,r=!1){return{scanToken:n,suggestions:t,warnings:i,trace:{traceId:e,usedOcr:r,usedAi:!1,durationMs:Math.round(performance.now()-s)}}}function Xe(n){if(n)return n instanceof HTMLInputElement&&n.type==="checkbox"?n.checked:n instanceof HTMLInputElement||n instanceof HTMLTextAreaElement||n instanceof HTMLSelectElement?n.value:n.textContent}function te(n,e="default"){const t=typeof location<"u"?location.pathname:"unknown-page",s=e.replace(/[^\w-]/g,"_")||"default";return`smart-fill:${n}:${t}:${s}:open`}function We(n,e){if(typeof n=="string")return n;if(e.id)return`#${e.id}`;const t=typeof e.className=="string"?e.className.trim().split(/\s+/).filter(Boolean)[0]:"";return t?`.${t}`:e.tagName.toLowerCase()}function ne(n,e){try{const t=window.localStorage.getItem(n);return t==null?e:t==="1"}catch{return e}}function se(n,e){if(n)try{window.localStorage.setItem(n,e?"1":"0")}catch{}}function ie(){return`smart-fill:${typeof location<"u"?location.pathname:"unknown-page"}:local-priority`}function Je(){try{return window.localStorage.getItem(ie())==="1"}catch{return!1}}function Qe(n){try{window.localStorage.setItem(ie(),n?"1":"0")}catch{}}const f={status:"idle"},H=new _,re=new he;class ae{static async setup(e){if(typeof window>"u")return f.status="ready",et();if(f.status==="ready"&&f.apiKey===e.apiKey&&f.session)return f.session;if(f.status==="loading"&&f.apiKey===e.apiKey&&f.promise)return f.promise;f.apiKey&&f.apiKey!==e.apiKey&&re.destroyAll();const t=new le(e);return f.status="loading",f.apiKey=e.apiKey,f.client=t,f.promise=t.createSession().then(s=>(console.log("SmartFill session created:",s),t.setAccessToken(s.apiKey),f.status="ready",f.session=s,H.emit("ready",{apiKey:e.apiKey}),s)).catch(s=>{throw f.status="error",s}),f.promise}static create(e={}){if(typeof window>"u")return k();if(f.status!=="ready"||!f.client)throw E("SDK_NOT_READY","请先 await SmartFill.setup({ apiKey })。","setup");return new J(e,{client:f.client,manager:re})}}c(ae,"on",H.on.bind(H));function et(){return{apiKey:"server-mock",accessToken:"server-mock",expiresIn:0,refreshBefore:0,features:{text:!1,image:!1,ai:!1,localRuleOnly:!0},rulesVersion:"server"}}function k(n){const e=()=>{};return{on:e,useAdapter:()=>k(),registerFields:()=>k(),unregisterFields:e,mount:()=>k(),mountFloatingButton:()=>k(),open:async()=>{},close:e,rescan:async()=>({scanToken:"server",fields:[]}),recognize:async()=>({scanToken:"server",suggestions:[],trace:{traceId:"server",usedOcr:!1,usedAi:!1,durationMs:0}}),apply:async()=>({applied:[],skipped:[]}),destroy:e}}const tt={name:"native",match:n=>n instanceof HTMLInputElement||n instanceof HTMLTextAreaElement||n instanceof HTMLSelectElement,getValue:n=>{const e=n.element;if(e instanceof HTMLInputElement&&e.type==="checkbox")return e.checked;if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement)return e.value},setValue:(n,e)=>{const t=n.element;t instanceof HTMLInputElement&&t.type==="checkbox"?t.checked=!!e:(t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement)&&(t.value=String(e??"")),t==null||t.dispatchEvent(new Event("input",{bubbles:!0})),t==null||t.dispatchEvent(new Event("change",{bubbles:!0}))}};p.DEFAULT_BASE_URL=y,p.DomFiller=D,p.DomScanner=Y,p.EventBus=_,p.LocalRuleEngine=U,p.NativeAdapter=tt,p.SmartFill=ae,p.SmartFillInstance=J,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
92
|
+
`,ht=.75;class de{constructor(e,t){c(this,"events",new _);c(this,"scanner");c(this,"ruleEngine",new J);c(this,"panel",null);c(this,"panels",[]);c(this,"panelStorageKeys",new Map);c(this,"registeredFields",[]);c(this,"adapters",[Y]);c(this,"scanResult",null);c(this,"autoApplyState",null);c(this,"formConfigVersion");c(this,"localPriorityEnabled",!1);c(this,"destroyed",!1);this.config=e,this.context=t,this.scanner=new ne(e.root||document),this.localPriorityEnabled=vt(),this.context.manager.add(this)}on(e,t){return this.events.on(e,t)}useAdapter(e){return this.assertAlive(),this.adapters.push(e),this}registerFields(e){this.assertAlive();const t=new Set;for(const s of e){const i=s.rowKey==null?s.fieldId:`${s.fieldId}:${s.rowKey}`;if(t.has(i))throw E("UNSUPPORTED_PAGE",`字段 ${i} 重复注册。`,"scan");t.add(i)}return this.registeredFields=e,this}unregisterFields(e){this.assertAlive(),this.registeredFields=e!=null&&e.length?this.registeredFields.filter(t=>!e.includes(t.fieldId)):[]}mount(e){this.assertAlive();const t=typeof e=="string"?document.querySelector(e):e;if(!t)throw E("UNSUPPORTED_PAGE","未找到智能录入挂载点。","ui");const s=pe("inline",xt(e,t)),i=he(s,!0);return this.createPanel("inline",s,i).mount(t),this}mountFloatingButton(){this.assertAlive();const e=pe("floating"),t=he(e,!1);return this.createPanel("floating",e,t).mount(document.body),this}createPanel(e,t,s){let i;return i=new dt({mode:e,initialOpen:s,messages:this.config.messages,localPriorityEnabled:this.localPriorityEnabled,onOpen:()=>this.open(i),onClose:()=>this.close(i),onRecognize:r=>(this.panel=i,this.recognize(r)),onLocalPriorityChange:r=>this.handleLocalPriorityChange(r,i)}),this.panels.push(i),this.panelStorageKeys.set(i,t),this.panel=i,i}handleLocalPriorityChange(e,t){this.localPriorityEnabled=e,wt(e);for(const s of this.panels)s!==t&&s.setLocalPriorityEnabled(e)}async open(e=this.panel){this.assertAlive(),e&&(this.context.manager.activate(this),this.panel=e,e.setOpen(!0),ge(this.panelStorageKeys.get(e),!0),await this.rescan())}close(e=this.panel){e&&(this.panel=e,e.setOpen(!1),ge(this.panelStorageKeys.get(e),!1))}async rescan(){var e;this.assertAlive();try{this.formConfigVersion=void 0;const t=this.registeredFields.length?this.registeredFields:void 0;if(this.scanResult=this.scanner.scan({registered:t,maxFields:this.config.maxFields}),!this.scanResult.fields.length)throw E("NO_FIELDS_FOUND","当前页面未找到可回填字段。","scan");return this.events.emit("scanCompleted",{scanToken:this.scanResult.scanToken,fieldCount:this.scanResult.fields.length}),(e=this.panel)==null||e.setStatus(`已扫描 ${this.scanResult.fields.length} 个字段。`),this.scanResult}catch(t){throw this.emitError(t,"scan"),t}}async recognize(e){var r,a,o;this.assertAlive();const t=this.scanResult||await this.rescan(),s=L("trace"),i=performance.now();this.events.emit("recognizing",{scanToken:t.scanToken,traceId:s}),(r=this.panel)==null||r.setBusy(!0,"识别中...");try{const d=t.fields.filter(y=>yt(y.localRuleMode,this.localPriorityEnabled)),l=t.fields.filter(y=>y.localRuleMode!=="only"),{text:u,usedOcr:m}=await this.context.client.resolveInputText({text:e.text,images:e.images,onStatusChange:y=>{var N,A,$;if(y==="image_uploading"){(N=this.panel)==null||N.setStatus("图片上传中...");return}if(y==="image_recognizing"){(A=this.panel)==null||A.setStatus("图片识别中...");return}($=this.panel)==null||$.setStatus("识别中...")}}),k=u?this.ruleEngine.recognize(u,d,t.scanToken):[];let w;if(!l.length)w=fe(t.scanToken,s,k,i,void 0,m);else try{const y=await this.context.client.recognize({scanToken:t.scanToken,formCode:this.config.formCode,configVersion:this.formConfigVersion,text:u,usedOcr:m,fields:l,onStatusChange:()=>{var A;(A=this.panel)==null||A.setStatus("识别中...")}});y.trace.durationMs=y.trace.durationMs||Math.round(performance.now()-i);const N=bt(k,y.suggestions.filter(A=>l.some($=>$.fieldId===A.fieldId)));w={...y,suggestions:N}}catch(y){if(!k.length)throw y;w=fe(t.scanToken,s,k,i,[m?"后端识别失败,已使用 OCR 文本触发本地识别继续回填。":"后端识别失败,已启用本地识别继续回填。"],m)}return this.autoApplyState=gt(w.scanToken,w.trace.traceId,w.suggestions,t,this.registeredFields),this.events.emit("recognized",w),(a=this.panel)==null||a.setAutoApplyState(this.autoApplyState),await this.applyAutoItems(this.autoApplyState),w}catch(d){throw this.emitError(d,"recognize"),d}finally{(o=this.panel)==null||o.setBusy(!1)}}async apply(e){var i;if(this.assertAlive(),!this.scanResult)throw E("SCAN_TOKEN_EXPIRED","请先扫描字段后再回填。","apply");this.events.emit("applying",{scanToken:e.scanToken,count:e.values.length});const s=await new z(this.scanResult.fields,this.registeredFields,this.adapters).apply(e);return this.events.emit("applied",s),(i=this.panel)==null||i.setApplyResult(s),s}async applyAutoItems(e){var o,d;if(!this.scanResult)throw E("SCAN_TOKEN_EXPIRED","请先扫描字段后再回填。","apply");const t=mt(e),s=e.items.filter(l=>ue(l)).map(l=>({fieldId:l.fieldId,value:l.value,source:l.source}));this.events.emit("applying",{scanToken:e.scanToken,count:s.length}),(o=this.panel)==null||o.setStatus("识别完成,正在自动回填...");const r=await new z(this.scanResult.fields,this.registeredFields,this.adapters).apply({scanToken:e.scanToken,values:s}),a={...r,skipped:[...t,...r.skipped],warnings:[...r.warnings||[],...t.length?["部分字段因置信度或风险策略被跳过。"]:[]]};return this.events.emit("applied",a),(d=this.panel)==null||d.setApplyResult(a),a}destroy(){if(!this.destroyed){this.destroyed=!0;for(const e of this.panels)e.destroy();this.panels.length=0,this.panelStorageKeys.clear(),this.panel=null,this.events.clear(),this.context.manager.remove(this)}}assertAlive(){if(this.destroyed)throw E("INSTANCE_DESTROYED","实例已销毁。","ui")}emitError(e,t){var i;const s=Ee(e,t);this.events.emit("error",s),(i=this.panel)==null||i.setError(s.message)}}function gt(n,e,t,s,i){return{scanToken:n,traceId:e,items:t.map(r=>{const a=s.fields.find(l=>l.fieldId===r.fieldId),o=i.find(l=>l.fieldId===r.fieldId),d=o!=null&&o.getValue?o.getValue():Et(a==null?void 0:a.element);return{applyItemId:`${r.fieldId}_${Math.random().toString(36).slice(2,8)}`,fieldId:r.fieldId,scanToken:n,groupId:r.groupId,label:r.label,value:r.value,currentValue:d,displayValue:r.displayValue||String(r.value??""),confidence:r.confidence,source:r.source,warnings:r.warnings,previousValue:d}})}}function ue(n){var e;return n.confidence>=ht&&!((e=n.warnings)!=null&&e.length)}function mt(n){return n.items.filter(e=>!ue(e)).map(e=>{var t,s;return{fieldId:e.fieldId,label:e.label,attemptedValue:e.value,reason:((t=e.warnings)==null?void 0:t.join(";"))||`置信度 ${Math.round(e.confidence*100)}% 低于自动回填阈值`,reasonCode:(s=e.warnings)!=null&&s.length?"AUTO_APPLY_WARNING":"LOW_CONFIDENCE"}})}function bt(n,e){const t=new Map;for(const s of e)t.set(s.fieldId,s);for(const s of n)t.has(s.fieldId)||t.set(s.fieldId,s);return[...t.values()]}function yt(n,e){return n==="only"?!0:n==="off"?!1:e}function fe(n,e,t,s,i,r=!1){return{scanToken:n,suggestions:t,warnings:i,trace:{traceId:e,usedOcr:r,usedAi:!1,durationMs:Math.round(performance.now()-s)}}}function Et(n){if(n)return n instanceof HTMLInputElement&&n.type==="checkbox"?n.checked:n instanceof HTMLInputElement||n instanceof HTMLTextAreaElement||n instanceof HTMLSelectElement?n.value:n.textContent}function pe(n,e="default"){const t=typeof location<"u"?location.pathname:"unknown-page",s=e.replace(/[^\w-]/g,"_")||"default";return`smart-fill:${n}:${t}:${s}:open`}function xt(n,e){if(typeof n=="string")return n;if(e.id)return`#${e.id}`;const t=typeof e.className=="string"?e.className.trim().split(/\s+/).filter(Boolean)[0]:"";return t?`.${t}`:e.tagName.toLowerCase()}function he(n,e){try{const t=window.localStorage.getItem(n);return t==null?e:t==="1"}catch{return e}}function ge(n,e){if(n)try{window.localStorage.setItem(n,e?"1":"0")}catch{}}function me(){return`smart-fill:${typeof location<"u"?location.pathname:"unknown-page"}:local-priority`}function vt(){try{return window.localStorage.getItem(me())==="1"}catch{return!1}}function wt(n){try{window.localStorage.setItem(me(),n?"1":"0")}catch{}}const f={status:"idle"},j=new _,be=new Ie;class ye{static async setup(e){if(typeof window>"u")return f.status="ready",St();if(f.status==="ready"&&f.apiKey===e.apiKey&&f.session)return f.session;if(f.status==="loading"&&f.apiKey===e.apiKey&&f.promise)return f.promise;f.apiKey&&f.apiKey!==e.apiKey&&be.destroyAll();const t=new xe(e);return f.status="loading",f.apiKey=e.apiKey,f.client=t,f.promise=t.createSession().then(s=>(console.log("SmartFill session created:",s),t.setAccessToken(s.apiKey),f.status="ready",f.session=s,j.emit("ready",{apiKey:e.apiKey}),s)).catch(s=>{throw f.status="error",s}),f.promise}static create(e={}){if(typeof window>"u")return T();if(f.status!=="ready"||!f.client)throw E("SDK_NOT_READY","请先 await SmartFill.setup({ apiKey })。","setup");return new de(e,{client:f.client,manager:be})}}c(ye,"on",j.on.bind(j));function St(){return{apiKey:"server-mock",accessToken:"server-mock",expiresIn:0,refreshBefore:0,features:{text:!1,image:!1,ai:!1,localRuleOnly:!0},rulesVersion:"server"}}function T(n){const e=()=>{};return{on:e,useAdapter:()=>T(),registerFields:()=>T(),unregisterFields:e,mount:()=>T(),mountFloatingButton:()=>T(),open:async()=>{},close:e,rescan:async()=>({scanToken:"server",fields:[]}),recognize:async()=>({scanToken:"server",suggestions:[],trace:{traceId:"server",usedOcr:!1,usedAi:!1,durationMs:0}}),apply:async()=>({applied:[],skipped:[]}),destroy:e}}const kt={name:"native",match:n=>n instanceof HTMLInputElement||n instanceof HTMLTextAreaElement||n instanceof HTMLSelectElement,getValue:n=>{const e=n.element;if(e instanceof HTMLInputElement&&e.type==="checkbox")return e.checked;if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement||e instanceof HTMLSelectElement)return e.value},setValue:(n,e)=>{const t=n.element;t instanceof HTMLInputElement&&t.type==="checkbox"?t.checked=!!e:(t instanceof HTMLInputElement||t instanceof HTMLTextAreaElement||t instanceof HTMLSelectElement)&&(t.value=String(e??"")),t==null||t.dispatchEvent(new Event("input",{bubbles:!0})),t==null||t.dispatchEvent(new Event("change",{bubbles:!0}))}};p.DEFAULT_BASE_URL=b,p.DomFiller=z,p.DomScanner=ne,p.ElementAdapter=Y,p.EventBus=_,p.LocalRuleEngine=J,p.NativeAdapter=kt,p.SmartFill=ye,p.SmartFillInstance=de,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"element.d.ts","sourceRoot":"","sources":["../../../src/adapters/element.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAA;AAahD,eAAO,MAAM,cAAc,EAAE,gBAoC5B,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"smart-fill-instance.d.ts","sourceRoot":"","sources":["../../../src/core/smart-fill-instance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"smart-fill-instance.d.ts","sourceRoot":"","sources":["../../../src/core/smart-fill-instance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAKzD,OAAO,EAAc,KAAK,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,KAAK,EACV,UAAU,EACV,WAAW,EAEX,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,iBAAiB,EAClB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE1D,+BAA+B;AAC/B,KAAK,eAAe,GAAG;IACrB,MAAM,EAAE,aAAa,CAAA;IACrB,OAAO,EAAE,eAAe,CAAC,iBAAiB,CAAC,CAAA;CAC5C,CAAA;AAKD,qBAAa,iBAAiB;IA6B1B,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IA7B1B,kCAAkC;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;IACxC,iDAAiD;IACjD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAY;IACpC,8BAA8B;IAC9B,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAwB;IACnD,sEAAsE;IACtE,OAAO,CAAC,KAAK,CAA8B;IAC3C,sDAAsD;IACtD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;IAC9C,oBAAoB;IACpB,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAoC;IACrE,kDAAkD;IAClD,OAAO,CAAC,gBAAgB,CAAoB;IAC5C,uCAAuC;IACvC,OAAO,CAAC,QAAQ,CAAuC;IACvD,4CAA4C;IAC5C,OAAO,CAAC,UAAU,CAA0B;IAC5C,sDAAsD;IACtD,OAAO,CAAC,cAAc,CAA8B;IACpD,4BAA4B;IAC5B,OAAO,CAAC,iBAAiB,CAAC,CAAQ;IAClC,mCAAmC;IACnC,OAAO,CAAC,oBAAoB,CAAQ;IACpC,kDAAkD;IAClD,OAAO,CAAC,SAAS,CAAQ;gBAGN,MAAM,EAAE,qBAAqB,EAC7B,OAAO,EAAE,eAAe;IAO3C,sBAAsB;IACtB,EAAE,CAAC,CAAC,SAAS,MAAM,iBAAiB,EAAE,KAAK,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,OAAO,EAAE,iBAAiB,CAAC,CAAC,CAAC,KAAK,IAAI;IAIhG,wBAAwB;IACxB,UAAU,CAAC,OAAO,EAAE,gBAAgB;IAMpC;;;;OAIG;IACH,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;IAcpC,iCAAiC;IACjC,gBAAgB,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE;IAOpC,2BAA2B;IAC3B,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW;IAYlC,kCAAkC;IAClC,mBAAmB;IAQnB;;;OAGG;IACH,OAAO,CAAC,WAAW;IAqBnB,iDAAiD;IACjD,OAAO,CAAC,yBAAyB;IAQjC,wCAAwC;IAClC,IAAI,CAAC,WAAW,wBAAa;IAUnC,kBAAkB;IAClB,KAAK,CAAC,WAAW,wBAAa;IAO9B;;;;OAIG;IACG,MAAM;IAwBZ;;;;;;;;;OASG;IACG,SAAS,CAAC,KAAK,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,IAAI,EAAE,CAAA;KAAE;IAoFzD;;;;OAIG;IACG,KAAK,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;IAapD;;;OAGG;YACW,cAAc;IA6B5B,0CAA0C;IAC1C,OAAO;IAWP,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,SAAS;CAKlB"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export { EventBus } from './events/event-bus';
|
|
|
4
4
|
export { DomScanner } from './scanner/dom-scanner';
|
|
5
5
|
export { LocalRuleEngine } from './rules/local-rules';
|
|
6
6
|
export { DomFiller } from './filler/dom-filler';
|
|
7
|
+
export { ElementAdapter } from './adapters/element';
|
|
7
8
|
export { NativeAdapter } from './adapters/native';
|
|
8
9
|
export { DEFAULT_BASE_URL } from './config/defaults';
|
|
9
10
|
export type { AutoApplyItem, AutoApplyState, ApplyInput, ApplyResult, FieldDescriptor, FieldOption, FieldSchema, FieldSuggestion, FieldType, RecognizePayload, RecognizeResult, SessionResponse, SmartFillAdapter, SmartFillCreateConfig, SmartFillError, SmartFillEventMap, SmartFillSetupConfig } from './types';
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,aAAa,CAAA;AAEpB,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,YAAY,EACV,aAAa,EACb,cAAc,EACd,UAAU,EACV,WAAW,EACX,eAAe,EACf,WAAW,EACX,WAAW,EACX,eAAe,EACf,SAAS,EACT,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACrB,MAAM,SAAS,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,aAAa,CAAA;AAEpB,OAAO,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAA;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAA;AAClD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,YAAY,EACV,aAAa,EACb,cAAc,EACd,UAAU,EACV,WAAW,EACX,eAAe,EACf,WAAW,EACX,WAAW,EACX,eAAe,EACf,SAAS,EACT,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,qBAAqB,EACrB,cAAc,EACd,iBAAiB,EACjB,oBAAoB,EACrB,MAAM,SAAS,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dom-scanner.d.ts","sourceRoot":"","sources":["../../../src/scanner/dom-scanner.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"dom-scanner.d.ts","sourceRoot":"","sources":["../../../src/scanner/dom-scanner.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AA4C5D,+BAA+B;AAC/B,MAAM,MAAM,UAAU,GAAG;IACvB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,eAAe,EAAE,CAAA;CAC1B,CAAA;AAED,qBAAa,UAAU;IACT,OAAO,CAAC,QAAQ,CAAC,IAAI;gBAAJ,IAAI,GAAE,WAAW,GAAG,UAAU,GAAG,QAAmB;IAEjF;;;;OAIG;IACH,IAAI,CAAC,OAAO,GAAE;QAAE,UAAU,CAAC,EAAE,WAAW,EAAE,CAAC;QAAC,aAAa,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAA;KAAO,GAAG,UAAU;IAsB1G,+DAA+D;IAC/D,OAAO,CAAC,UAAU;IAsBlB,sEAAsE;IACtE,OAAO,CAAC,WAAW;IA4BnB,sCAAsC;IACtC,OAAO,CAAC,WAAW;IAUnB,2BAA2B;IAC3B,OAAO,CAAC,oBAAoB;IAwB5B;;;OAGG;IACH,OAAO,CAAC,uBAAuB;CAiBhC"}
|