auto-webmcp 0.3.10 → 0.3.11
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/dist/auto-webmcp.cjs.js +16 -1
- package/dist/auto-webmcp.cjs.js.map +2 -2
- package/dist/auto-webmcp.esm.js +16 -1
- package/dist/auto-webmcp.esm.js.map +2 -2
- package/dist/auto-webmcp.iife.js +1 -1
- package/dist/auto-webmcp.iife.js.map +3 -3
- package/dist/interceptor.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/auto-webmcp.iife.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var AutoWebMCP=(()=>{var B=Object.defineProperty;var Pe=Object.getOwnPropertyDescriptor;var _e=Object.getOwnPropertyNames;var Ne=Object.prototype.hasOwnProperty;var qe=(e,t)=>()=>(e&&(t=e(e=0)),t);var ne=(e,t)=>{for(var n in t)B(e,n,{get:t[n],enumerable:!0})},Be=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of _e(t))!Ne.call(e,i)&&i!==n&&B(e,i,{get:()=>t[i],enumerable:!(r=Pe(t,i))||r.enumerable});return e};var De=e=>Be(B({},"__esModule",{value:!0}),e);var He={};ne(He,{getAllRegisteredTools:()=>V,getRegisteredToolName:()=>lt,isWebMCPSupported:()=>L,registerFormTool:()=>z,unregisterAll:()=>G,unregisterFormTool:()=>C});function L(){return typeof navigator<"u"&&typeof navigator.modelContext<"u"}async function z(e,t,n){if(!L())return;H.get(e)&&await C(e);let i={name:t.name,description:t.description,inputSchema:t.inputSchema,execute:n};t.annotations&&Object.keys(t.annotations).length>0&&(i.annotations=t.annotations);try{await navigator.modelContext.registerTool(i)}catch{try{await navigator.modelContext.unregisterTool(t.name),await navigator.modelContext.registerTool(i)}catch{}}H.set(e,t.name)}async function C(e){if(!L())return;let t=H.get(e);if(t){try{await navigator.modelContext.unregisterTool(t)}catch{}H.delete(e)}}function lt(e){return H.get(e)}function V(){return Array.from(H.entries()).map(([e,t])=>({form:e,name:t}))}async function G(){let e=Array.from(H.entries());await Promise.all(e.map(([t])=>C(t)))}var H,F=qe(()=>{"use strict";H=new Map});var xt={};ne(xt,{autoWebMCP:()=>Oe});function re(e){return{exclude:e?.exclude??[],autoSubmit:e?.autoSubmit??!1,overrides:e?.overrides??{},debug:e?.debug??!1}}var v=["textbox","combobox","checkbox","radio","switch","spinbutton","searchbox","slider"];function W(e){return e instanceof HTMLInputElement?We(e):e instanceof HTMLTextAreaElement?{type:"string"}:e instanceof HTMLSelectElement?je(e):null}function We(e){switch(e.type.toLowerCase()){case"text":case"search":case"tel":return D(e);case"email":return{...D(e),format:"email"};case"url":return{...D(e),format:"uri"};case"number":case"range":{let n={type:"number"};return e.min!==""&&(n.minimum=parseFloat(e.min)),e.max!==""&&(n.maximum=parseFloat(e.max)),n}case"date":return{type:"string",format:"date"};case"datetime-local":return{type:"string",format:"date-time"};case"time":return{type:"string",format:"time"};case"month":return{type:"string",pattern:"^\\d{4}-\\d{2}$"};case"week":return{type:"string",pattern:"^\\d{4}-W\\d{2}$"};case"color":return{type:"string",pattern:"^#[0-9a-fA-F]{6}$"};case"checkbox":return{type:"boolean"};case"radio":return{type:"string"};case"file":case"hidden":case"submit":case"reset":case"button":case"image":return null;case"password":return null;default:return{type:"string"}}}function D(e){let t={type:"string"};e.minLength>0&&(t.minLength=e.minLength),e.maxLength>0&&e.maxLength!==524288&&(t.maxLength=e.maxLength),e.pattern&&(t.pattern=e.pattern);let n=e.getAttribute("list");if(n){let r=e.ownerDocument.getElementById(n);if(r instanceof HTMLDataListElement){let i=Array.from(r.options).filter(s=>!s.disabled&&s.value.trim()!=="");i.length>0&&(t.enum=i.map(s=>s.value.trim()),t.oneOf=i.map(s=>({const:s.value.trim(),title:s.textContent?.trim()||s.value.trim()})))}}return t}var Je=/^(select|choose|pick)\b|^--+|---/i;function oe(e){return e.disabled?!0:e.value!==""?!1:Je.test(e.text.trim())}function je(e){let t=[],n=[];for(let r of Array.from(e.children))if(r instanceof HTMLOptGroupElement){if(r.disabled)continue;let i=r.label?.trim()??"";for(let s of Array.from(r.children)){if(!(s instanceof HTMLOptionElement)||oe(s))continue;t.push(s.value);let o={const:s.value,title:s.text.trim()||s.value};i&&(o.group=i),n.push(o)}}else if(r instanceof HTMLOptionElement){if(oe(r))continue;t.push(r.value),n.push({const:r.value,title:r.text.trim()||r.value})}return t.length===0?{type:"string"}:e.multiple?{type:"array",items:{type:"string",enum:t}}:{type:"string",enum:t,oneOf:n}}function ie(e,t){return Array.from(e.querySelectorAll(`input[type="checkbox"][name="${CSS.escape(t)}"]`)).map(n=>n.value).filter(n=>n!==""&&n!=="on")}function se(e,t){return Array.from(e.querySelectorAll(`input[type="radio"][name="${CSS.escape(t)}"]`)).map(r=>r.value).filter(r=>r!=="")}function ae(e,t){return Array.from(e.querySelectorAll(`input[type="radio"][name="${CSS.escape(t)}"]`)).filter(r=>r.value!=="").map(r=>{let i=Ue(r);return{const:r.value,title:i||r.value}})}function le(e,t){switch(t){case"checkbox":case"switch":return{type:"boolean"};case"spinbutton":case"slider":{let n={type:"number"},r=e.getAttribute("aria-valuemin"),i=e.getAttribute("aria-valuemax");return r!==null&&(n.minimum=parseFloat(r)),i!==null&&(n.maximum=parseFloat(i)),n}case"combobox":{let n=e.getAttribute("aria-owns")??e.getAttribute("aria-controls");if(n){let r=document.getElementById(n);if(r){let i=Array.from(r.querySelectorAll('[role="option"]')).filter(s=>s.getAttribute("aria-disabled")!=="true");if(i.length>0){let s=i.map(l=>(l.getAttribute("data-value")??l.textContent??"").trim()).filter(Boolean),o=i.map(l=>({const:(l.getAttribute("data-value")??l.textContent??"").trim(),title:(l.textContent??"").trim()}));return{type:"string",enum:s,oneOf:o}}}}return{type:"string"}}case"textbox":case"searchbox":case"radio":default:return{type:"string"}}}function Ue(e){let t=e.closest("label");if(t){let n=t.cloneNode(!0);n.querySelectorAll("input, select, textarea, button").forEach(i=>i.remove());let r=n.textContent?.trim()??"";if(r)return r}if(e.id){let n=document.querySelector(`label[for="${CSS.escape(e.id)}"]`);if(n){let r=n.textContent?.trim()??"";if(r)return r}}return""}var me=0;function de(e,t){let n=t?.name??ze(e),r=t?.description??Ge(e),{schema:i,fieldElements:s}=Qe(e),o=Ye(e);return{name:n,description:r,inputSchema:i,annotations:o,fieldElements:s}}function ze(e){let t=e.getAttribute("toolname");if(t)return b(t);let n=e.dataset.webmcpName;if(n)return b(n);let r=J(e);if(r)return b(r);let i=pe(e);if(i)return b(i);if(e.id)return b(e.id);if(e.name)return b(e.name);if(e.action){let s=Ve(e.action);if(s)return b(s)}return`form_${++me}`}function b(e){return e.toLowerCase().trim().replace(/[^a-z0-9]+/g,"_").replace(/^_+|_+$/g,"").slice(0,64)||"form"}function J(e){let t=[...Array.from(e.querySelectorAll('button[type="submit"], button:not([type])')),...Array.from(e.querySelectorAll('input[type="submit"]'))];for(let n of t){let r=n instanceof HTMLInputElement?n.value.trim():n.textContent?.trim()??"";if(r&&r.length>0&&r.length<80)return r}return""}function pe(e){let t=e;for(;t;){let n=t.previousElementSibling;for(;n;){if(/^H[1-3]$/i.test(n.tagName)){let r=n.textContent?.trim()??"";if(r)return r}n=n.previousElementSibling}if(t=t.parentElement,!t||t===document.body)break}return""}function Ve(e){try{let n=new URL(e,window.location.href).pathname.split("/").filter(Boolean);return n[n.length-1]??""}catch{return""}}function Ge(e){let t=e.getAttribute("tooldescription");if(t)return t.trim();let n=e.dataset.webmcpDescription;if(n)return n.trim();let r=e.querySelector("legend");if(r?.textContent?.trim())return r.textContent.trim();let i=e.getAttribute("aria-label");if(i?.trim())return i.trim();let s=e.getAttribute("aria-describedby");if(s){let a=document.getElementById(s);if(a?.textContent?.trim())return a.textContent.trim()}let o=pe(e),l=document.title?.trim();return o&&l?`${o}: ${l}`:o||l||"Submit form"}var fe=/^(search|find|look|filter|browse|view|show|check|preview|get|fetch|retrieve|load)\b/i,ge=/^(delete|remove|cancel|terminate|destroy|purge|revoke|unsubscribe|deactivate)\b/i,Ke=/\/(delete|remove|cancel|destroy)\b/i;function Ye(e){let t={};if(e.dataset.webmcpReadonly!==void 0&&(t.readOnlyHint=e.dataset.webmcpReadonly!=="false"),e.dataset.webmcpDestructive!==void 0&&(t.destructiveHint=e.dataset.webmcpDestructive!=="false"),e.dataset.webmcpIdempotent!==void 0&&(t.idempotentHint=e.dataset.webmcpIdempotent!=="false"),e.dataset.webmcpOpenworld!==void 0&&(t.openWorldHint=e.dataset.webmcpOpenworld!=="false"),t.readOnlyHint===void 0){let r=e.method.toLowerCase()==="get",i=J(e),s=i?fe.test(i.trim()):!1;(r||s)&&(t.readOnlyHint=!0)}if(t.destructiveHint===void 0){let r=J(e),i=r?ge.test(r.trim()):!1,s=e.action?Ke.test(e.action):!1;(i||s)&&(t.destructiveHint=!0)}return t.idempotentHint===void 0&&(t.readOnlyHint===!0||e.method.toLowerCase()==="get")&&(t.idempotentHint=!0),t.openWorldHint===void 0&&(t.openWorldHint=t.readOnlyHint!==!0),t.readOnlyHint===!0||t.destructiveHint===!0||t.idempotentHint===!0||t.openWorldHint===!1?t:{}}function Xe(e){if(e instanceof HTMLInputElement){let t=e.type.toLowerCase();return t==="checkbox"?e.checked?!0:void 0:t==="radio"?void 0:t==="number"||t==="range"?e.value!==""?parseFloat(e.value):void 0:e.value!==""?e.value:void 0}if(e instanceof HTMLTextAreaElement)return e.value!==""?e.value:void 0;if(e instanceof HTMLSelectElement){if(e.multiple){let t=Array.from(e.options).filter(n=>n.selected).map(n=>n.value);return t.length>0?t:void 0}return e.value!==""?e.value:void 0}}function be(e,t=new Set){if(t.has(e))return[];t.add(e);let n=[];for(let r of Array.from(e.querySelectorAll("*")))r.shadowRoot&&n.push(...Array.from(r.shadowRoot.querySelectorAll("input, textarea, select")),...be(r.shadowRoot,t));return n}function Qe(e){let t={},n=[],r=new Map,i=new Set,s=new Set,o=[...Array.from(e.querySelectorAll("input, textarea, select")),...be(e)];for(let c of o){let m=c.name,u=m||Te(c);if(!u)continue;if(c instanceof HTMLInputElement&&c.type==="radio"){if(i.has(u))continue;i.add(u)}if(c instanceof HTMLInputElement&&c.type==="checkbox"){if(s.has(u))continue;s.add(u)}let p=W(c);if(!p||!U(c))continue;p.title=he(c);let f=ye(c);f&&(p.description=f);let g=Xe(c);if(g!==void 0&&(p.default=g),c instanceof HTMLInputElement&&c.type==="radio"){p.enum=se(e,u);let y=ae(e,u);y.length>0&&(p.oneOf=y);let d=e.querySelector(`input[type="radio"][name="${CSS.escape(u)}"]:checked`);d?.value&&(p.default=d.value)}if(c instanceof HTMLInputElement&&c.type==="checkbox"){let y=ie(e,u);if(y.length>1){let d={type:"array",items:{type:"string",enum:y},title:p.title};p.description&&(d.description=p.description);let E=Array.from(e.querySelectorAll(`input[type="checkbox"][name="${CSS.escape(u)}"]:checked`)).map(T=>T.value);E.length>0&&(d.default=E),t[u]=d,c.required&&n.push(u);continue}}t[u]=p,m||r.set(u,c),c.required&&n.push(u)}let l=et(e),a=new Set;for(let{el:c,role:m,key:u,enumValues:p,enumOneOf:f}of l){if(t[u])continue;if(m==="radio"){if(a.has(u))continue;a.add(u)}let g=le(c,m);p&&p.length>0&&(g.enum=p,f&&f.length>0&&(g.oneOf=f)),g.title=tt(c);let y=nt(c);y&&(g.description=y),t[u]=g,r.set(u,c),c.getAttribute("aria-required")==="true"&&n.push(u)}return{schema:{$schema:"https://json-schema.org/draft/2020-12/schema",type:"object",properties:t,required:n},fieldElements:r}}var Ee=/^_r_[0-9a-z]+_$|^:[a-z0-9]+:$/i;function Te(e){let t=e;if(t.dataset.webmcpName)return b(t.dataset.webmcpName);if(e.id&&!Ee.test(e.id))return b(e.id);let n=e.getAttribute("aria-label");return n?b(n):(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement)&&e.placeholder?.trim()?b(e.placeholder.trim()):e instanceof HTMLInputElement&&e.type!=="text"?e.type:null}function Ze(e){if(e.dataset.webmcpName)return b(e.dataset.webmcpName);if(e.id&&!Ee.test(e.id))return b(e.id);let t=e.getAttribute("aria-label");if(t)return b(t);let n=e.getAttribute("placeholder");return n?b(n):null}function et(e){let t=v.map(a=>`[role="${a}"]`).join(", "),n=[];for(let a of Array.from(e.querySelectorAll(t))){if(a instanceof HTMLInputElement||a instanceof HTMLTextAreaElement||a instanceof HTMLSelectElement||a.getAttribute("aria-hidden")==="true"||a.hidden)continue;let c=a.getAttribute("role"),m=ce(a);m&&n.push({el:a,role:c,key:m})}let r=n.filter(a=>a.role==="radio"),i=n.filter(a=>a.role!=="radio"),s=new Map,o=[];for(let a of r){let c=a.el.closest('[role="radiogroup"]');c?(s.has(c)||s.set(c,[]),s.get(c).push(a.el)):o.push(a)}let l=[];for(let[a,c]of s){let m=ce(a);if(!m)continue;let u=c.map(f=>(f.getAttribute("data-value")??f.getAttribute("aria-label")??f.textContent??"").trim()).filter(Boolean),p=c.map(f=>{let g=(f.getAttribute("data-value")??f.getAttribute("aria-label")??f.textContent??"").trim(),y=(f.getAttribute("aria-label")??f.textContent??"").trim();return{const:g,title:y||g}}).filter(f=>f.const!=="");u.length>0&&l.push({el:a,role:"radio",key:m,enumValues:u,enumOneOf:p})}return[...i,...l,...o]}function ce(e){let t=e;if(t.dataset?.webmcpName)return b(t.dataset.webmcpName);if(e.id)return b(e.id);let n=e.getAttribute("aria-label");if(n)return b(n);let r=e.getAttribute("aria-labelledby");if(r){let i=document.getElementById(r)?.textContent?.trim();if(i)return b(i)}return null}function tt(e){let t=e;if(t.dataset?.webmcpTitle)return t.dataset.webmcpTitle;let n=e.getAttribute("aria-label");if(n)return n.trim();let r=e.getAttribute("aria-labelledby");if(r){let i=document.getElementById(r)?.textContent?.trim();if(i)return i}return e.id?j(e.id):""}function nt(e){let t=e.getAttribute("toolparamdescription");if(t)return t.trim();let n=e;if(n.dataset?.webmcpDescription)return n.dataset.webmcpDescription;let r=e.getAttribute("aria-description");if(r)return r;let i=e.getAttribute("aria-describedby");if(i){let o=document.getElementById(i)?.textContent?.trim();if(o)return o}let s=e.getAttribute("placeholder")??e.dataset?.placeholder;return s?s.trim():""}function he(e){if("dataset"in e&&e.dataset.webmcpTitle)return e.dataset.webmcpTitle;let t=rt(e);return t||(e.name?j(e.name):e.id?j(e.id):(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement)&&e.placeholder?.trim()?e.placeholder.trim():"")}function ye(e){let t=e.getAttribute("toolparamdescription");if(t)return t.trim();let n=e;if(n.dataset.webmcpDescription)return n.dataset.webmcpDescription;let r=e.getAttribute("aria-description");if(r)return r;let i=e.getAttribute("aria-describedby");if(i){let s=document.getElementById(i);if(s?.textContent?.trim())return s.textContent.trim()}if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement){let s=e.placeholder?.trim();if(s&&s.length>0)return s}return""}function rt(e){if(e.id){let n=document.querySelector(`label[for="${CSS.escape(e.id)}"]`);if(n){let r=ue(n);if(r)return r}}let t=e.closest("label");if(t){let n=ue(t);if(n)return n}return""}function ue(e){let t=e.cloneNode(!0);return t.querySelectorAll("input, select, textarea, button").forEach(n=>n.remove()),t.textContent?.trim()??""}function j(e){return e.replace(/[-_]/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").trim().replace(/\b\w/g,t=>t.toUpperCase())}function U(e){let t=window.getComputedStyle(e);if(t.display==="none"||t.visibility==="hidden"||e.offsetParent===null&&t.position!=="fixed")return!1;let n=e;for(;n&&n!==document.body;){if(n.getAttribute("aria-hidden")==="true")return!1;n=n.parentElement}return!e.closest("fieldset")?.disabled}function Le(e,t,n){let r=it(e,n),i=st(e),{schema:s,fieldElements:o}=at(t),l=ot(n);return{name:r,description:i,inputSchema:s,annotations:l,fieldElements:o}}function ot(e){let t={},n=e instanceof HTMLInputElement?e.value.trim():e?.textContent?.trim()??"";return fe.test(n)&&(t.readOnlyHint=!0,t.idempotentHint=!0),ge.test(n)&&(t.destructiveHint=!0),t.readOnlyHint!==!0&&(t.openWorldHint=!0),t.readOnlyHint===!0||t.destructiveHint===!0||t.idempotentHint===!0||t.openWorldHint===!1?t:{}}function it(e,t){if(t){let i=t instanceof HTMLInputElement?t.value.trim():t.textContent?.trim()??"";if(i&&i.length>0&&i.length<80)return b(i)}let n=Me(e);if(n)return b(n);let r=document.title?.trim();return r?b(r):`form_${++me}`}function st(e){let t=Me(e),n=document.title?.trim();return t&&n&&t!==n?`${t} on ${n}`:t||n||"Submit form"}function Me(e){let t=e.querySelector("h1, h2, h3");if(t?.textContent?.trim())return t.textContent.trim();let n=e;for(;n;){let r=n.previousElementSibling;for(;r;){if(/^H[1-3]$/i.test(r.tagName)){let i=r.textContent?.trim()??"";if(i)return i}r=r.previousElementSibling}if(n=n.parentElement,!n||n===document.body)break}return""}function at(e){let t={},n=[],r=new Map,i=new Set,s=new Set;for(let o of e){if(!(o instanceof HTMLInputElement)&&!(o instanceof HTMLTextAreaElement)&&!(o instanceof HTMLSelectElement)){let u=Ze(o);if(!u||!U(o))continue;let p={type:"string"};p.title=o.getAttribute("aria-label")??u;let f=(o.getAttribute("aria-description")??o.getAttribute("aria-describedby"),null);f&&(p.description=f),t[u]=p,r.set(u,o),n.push(u);continue}let l=o.name,a=(l?b(l):null)||Te(o);if(!a)continue;if(o instanceof HTMLInputElement&&o.type==="radio"){if(i.has(a))continue;i.add(a)}if(o instanceof HTMLInputElement&&o.type==="checkbox"){if(s.has(a))continue;s.add(a)}let c=W(o);if(!c||!U(o))continue;c.title=he(o);let m=ye(o);if(m&&(c.description=m),o instanceof HTMLInputElement&&o.type==="checkbox"){let u=e.filter(p=>p instanceof HTMLInputElement&&p.type==="checkbox"&&p.name===a).map(p=>p.value).filter(p=>p!==""&&p!=="on");if(u.length>1){let p={type:"array",items:{type:"string",enum:u},title:c.title};c.description&&(p.description=c.description),t[a]=p,o.required&&n.push(a);continue}}t[a]=c,l||r.set(a,o),o.required&&n.push(a)}return{schema:{$schema:"https://json-schema.org/draft/2020-12/schema",type:"object",properties:t,required:n},fieldElements:r}}F();var $=new WeakMap,Ae=new WeakMap,O=new WeakMap,M=new WeakMap,x=new WeakMap,P=new WeakMap,ct=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,"value")?.set,ut=Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype,"value")?.set,A=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,"checked")?.set;function ve(e,t,n,r){return r?.fieldElements&&O.set(e,r.fieldElements),xe(e,n),async i=>{x.set(e,[]),M.delete(e),K(e,i);let s=pt(r,i);return s.length>0&&M.set(e,s),window.dispatchEvent(new CustomEvent("toolactivated",{detail:{toolName:n}})),new Promise((o,l)=>{$.set(e,{resolve:o,reject:l}),(t.autoSubmit||e.hasAttribute("toolautosubmit")||e.dataset.webmcpAutosubmit!==void 0)&&Se(e).then(async()=>{try{K(e,i);for(let c=0;c<2&&dt(e,i,O.get(e)).length!==0;c++)K(e,i),await Se(e,400,100);let a=e;if(!e.isConnected){let m=document.querySelector('button[type="submit"]:not([disabled]), input[type="submit"]:not([disabled])')?.closest("form");m&&(a=m,$.set(a,{resolve:o,reject:l}),xe(a,n))}a!==e&&M.has(e)&&(M.set(a,M.get(e)),M.delete(e)),a.requestSubmit()}catch(a){l(a instanceof Error?a:new Error(String(a)))}})})}}function xe(e,t){e.__awmcp_intercepted||(e.__awmcp_intercepted=!0,e.addEventListener("submit",n=>{let r=$.get(e);if(!r)return;let{resolve:i}=r;$.delete(e);let s=mt(e,Ae.get(e),O.get(e));P.delete(e);let o=M.get(e)??[];M.delete(e);let l=x.get(e)??[];x.delete(e);let a=l.filter(g=>g.type==="not_filled").map(g=>g.field),c={status:o.length>0||a.length>0?"partial":"success",filled_fields:s,skipped_fields:a,missing_required:o,warnings:[...o.map(g=>({field:g,type:"missing_required",message:`required field "${g}" was not provided`})),...l]},m=[...o.length?[`required fields were not filled: ${o.join(", ")}`]:[],...l.map(g=>g.message)],u=m.length?` Note: ${m.join("; ")}.`:"",f={content:[{type:"text",text:`Form submitted. Fields: ${JSON.stringify(s)}${u}`},{type:"text",text:JSON.stringify(c)}]};n.agentInvoked&&typeof n.respondWith=="function"&&(n.preventDefault(),n.respondWith(Promise.resolve(f))),i(f)}),e.addEventListener("reset",()=>{P.delete(e),window.dispatchEvent(new CustomEvent("toolcancel",{detail:{toolName:t}}))}))}function k(e,t){if(e.focus(),e.select?.(),document.execCommand("insertText",!1,t)&&e.value===t)return;let n=e instanceof HTMLTextAreaElement?ut:ct;n?n.call(e,t):e.value=t,e.dispatchEvent(new InputEvent("input",{bubbles:!0,cancelable:!0,inputType:"insertText",data:t})),e.dispatchEvent(new Event("change",{bubbles:!0}))}function Y(e,t){A?A.call(e,t):e.checked=t,e.dispatchEvent(new Event("change",{bubbles:!0}))}function _(e,t){for(let n of Array.from(e.querySelectorAll("*"))){let r=n.shadowRoot;if(!r)continue;let i=r.querySelector(t);if(i)return i;let s=_(r,t);if(s)return s}return null}function Q(e,t){let n=CSS.escape(t),r=e.querySelector(`[name="${n}"]`)??e.querySelector(`input#${n}, textarea#${n}, select#${n}`);return r||(_(document,`[name="${n}"]`)??_(document,`input#${n}, textarea#${n}, select#${n}`))}function K(e,t){Ae.set(e,t);let n=O.get(e),r={};for(let[i,s]of Object.entries(t)){let o=Q(e,i);if(o){if(o instanceof HTMLInputElement)if(we(o,e,i,s),o.type==="checkbox")if(Array.isArray(s)){let a=CSS.escape(i);r[i]=Array.from(e.querySelectorAll(`input[type="checkbox"][name="${a}"]`)).filter(c=>c.checked).map(c=>c.value)}else r[i]=o.checked;else r[i]=o.value;else o instanceof HTMLTextAreaElement?(k(o,String(s??"")),r[i]=o.value):o instanceof HTMLSelectElement&&(X(o,s,e,i),r[i]=o.multiple?Array.from(o.options).filter(a=>a.selected).map(a=>a.value):o.value);continue}let l=n?.get(i);if(l){let a=l;if(!l.isConnected){let c=l.id;if(c){let m=document.getElementById(c)??_(document,`#${CSS.escape(c)}`);m&&(a=m)}}a instanceof HTMLInputElement?(we(a,e,i,s),r[i]=a.type==="checkbox"?a.checked:a.value):a instanceof HTMLTextAreaElement?(k(a,String(s??"")),r[i]=a.value):a instanceof HTMLSelectElement?(X(a,s,e,i),r[i]=a.multiple?Array.from(a.options).filter(c=>c.selected).map(c=>c.value):a.value):(Ce(a,s),r[i]=s)}}P.set(e,r),window.__lastFillWarnings=x.get(e)??[]}function we(e,t,n,r){let i=e.type.toLowerCase();if(i==="checkbox"){if(Array.isArray(r)){let s=CSS.escape(n),o=t.querySelectorAll(`input[type="checkbox"][name="${s}"]`);for(let l of o)Y(l,r.map(String).includes(l.value));return}Y(e,!!r);return}if(i==="number"||i==="range"){let s=String(r??""),o=Number(s);if(s===""||isNaN(o)){x.get(t)?.push({field:n,type:"type_mismatch",message:`"${n}" expects a number, got: ${JSON.stringify(r)}`,original:r});return}let l=e.min!==""?parseFloat(e.min):-1/0,a=e.max!==""?parseFloat(e.max):1/0;if(o<l||o>a){let c=Math.min(Math.max(o,l),a);x.get(t)?.push({field:n,type:"clamped",message:`"${n}" value ${o} is outside allowed range [${e.min||"?"}, ${e.max||"?"}], clamped to ${c}`,original:o,actual:c}),e.value=String(c)}else e.value=String(o);e.dispatchEvent(new InputEvent("input",{bubbles:!0,cancelable:!0,inputType:"insertText",data:String(o)})),e.dispatchEvent(new Event("change",{bubbles:!0}));return}if(i==="radio"){let s=CSS.escape(n),o=t.querySelectorAll(`input[type="radio"][name="${s}"]`);for(let l of o)if(l.value===String(r)){A?A.call(l,!0):l.checked=!0,l.dispatchEvent(new Event("change",{bubbles:!0}));break}return}k(e,String(r??""))}function X(e,t,n,r){if(e.multiple){let s=Array.isArray(t)?t.map(String):[String(t??"")];for(let o of Array.from(e.options))o.selected=s.includes(o.value);e.dispatchEvent(new Event("change",{bubbles:!0}));return}let i=String(t??"");if(e.value=i,e.value!==i){let s=i.toLowerCase(),o=Array.from(e.options).find(l=>l.text.trim().toLowerCase()===s||l.label.trim().toLowerCase()===s);o?e.value=o.value:n&&r&&x.get(n)?.push({field:r,type:"not_filled",message:`"${r}" value "${i}" did not match any option in the select`,original:i})}e.dispatchEvent(new Event("change",{bubbles:!0}))}function Ce(e,t){let n=e.getAttribute("role");if(n==="checkbox"||n==="switch"){e.setAttribute("aria-checked",String(!!t)),e.dispatchEvent(new MouseEvent("click",{bubbles:!0}));return}if(n==="radio"){e.setAttribute("aria-checked","true"),e.dispatchEvent(new MouseEvent("click",{bubbles:!0}));return}if(n==="radiogroup"){let i=Array.from(e.querySelectorAll('[role="radio"]'));for(let s of i)if((s.getAttribute("data-value")??s.getAttribute("aria-label")??s.textContent??"").trim()===String(t)){s.setAttribute("aria-checked","true"),s.dispatchEvent(new MouseEvent("click",{bubbles:!0}));for(let l of i)l!==s&&l.setAttribute("aria-checked","false");break}return}let r=e;if(r.isContentEditable){r.focus();let i=document.createRange();i.selectNodeContents(r);let s=window.getSelection();s?.removeAllRanges(),s?.addRange(i),document.execCommand("insertText",!1,String(t??""))}else e.dispatchEvent(new Event("input",{bubbles:!0})),e.dispatchEvent(new Event("change",{bubbles:!0}))}function mt(e,t,n){let r={},i=new FormData(e),s=P.get(e);for(let[o,l]of i.entries())if(r[o]!==void 0){let a=r[o];Array.isArray(a)?a.push(l):r[o]=[a,l]}else r[o]=l;if(t)for(let o of Object.keys(t)){if(o in r)continue;if(s&&o in s){r[o]=s[o];continue}let l=Q(e,o)??n?.get(o)??null;if(l)if(l instanceof HTMLInputElement&&l.type==="checkbox")r[o]=l.checked;else if(l instanceof HTMLInputElement||l instanceof HTMLTextAreaElement||l instanceof HTMLSelectElement)r[o]=l.value;else{let a=l.getAttribute("role");a==="checkbox"||a==="switch"?r[o]=l.getAttribute("aria-checked")==="true":r[o]=l.textContent?.trim()??""}}return r}function ke(e,t){if(e instanceof HTMLInputElement){let n=e.type.toLowerCase();n==="checkbox"?Y(e,!!t):n==="radio"?e.value===String(t)&&(A?A.call(e,!0):e.checked=!0,e.dispatchEvent(new Event("change",{bubbles:!0}))):k(e,String(t??""))}else e instanceof HTMLTextAreaElement?k(e,String(t??"")):e instanceof HTMLSelectElement?X(e,t):Ce(e,t)}function Se(e,t=800,n=150){return new Promise(r=>{let i=!1,s=null,o=()=>{i||(i=!0,l.disconnect(),s!==null&&clearTimeout(s),r())},l=new MutationObserver(()=>{s!==null&&clearTimeout(s),s=setTimeout(o,n)});l.observe(e,{childList:!0,subtree:!0,attributes:!0,characterData:!0}),setTimeout(o,t),s=setTimeout(o,n)})}function dt(e,t,n){let r=[];for(let[i,s]of Object.entries(t)){let o=Q(e,i)??n?.get(i)??null;o&&(o instanceof HTMLInputElement&&o.type==="checkbox"?o.checked!==!!s&&r.push(i):(o instanceof HTMLInputElement||o instanceof HTMLTextAreaElement||o instanceof HTMLSelectElement)&&o.value!==String(s??"")&&r.push(i))}return r}function pt(e,t){return e?.inputSchema?.required?.length?e.inputSchema.required.filter(n=>!(n in t)):[]}function Re(e,t,n){window.dispatchEvent(new CustomEvent(e,{detail:{form:t,toolName:n}}))}function ft(e,t){if(e.dataset.noWebmcp!==void 0)return!0;for(let n of t.exclude)try{if(e.matches(n))return!0}catch{}return!1}async function N(e,t){if(ft(e,t))return;let n;for(let[l,a]of Object.entries(t.overrides))try{if(e.matches(l)){n=a;break}}catch{}let r=de(e,n);t.debug&&Ht(r.name,r.description);let i=ve(e,t,r.name,r);await z(e,r,i),ee.add(e),Ie++;let s=e.querySelector('[type="submit"], button[data-variant="primary"], button:not([type])')??null,o=window.__pendingSubmitBtns??={};o[r.name]=s,t.debug&&console.log(`[auto-webmcp] Registered: ${r.name}`,r),Re("form:registered",e,r.name)}async function gt(e,t){let{getRegisteredToolName:n}=await Promise.resolve().then(()=>(F(),He)),r=n(e);r&&(await C(e),ee.delete(e),t.debug&&console.log(`[auto-webmcp] Unregistered: ${r}`),Re("form:unregistered",e,r))}var I=null,ee=new WeakSet,Ie=0,Z=new Map,bt=300;function Et(e){let t=e.tagName.toLowerCase();if(t==="input"||t==="textarea"||t==="select")return!0;let n=e.getAttribute("role");if(n&&v.includes(n)||e.querySelector("input, textarea, select"))return!0;for(let r of v)if(e.querySelector(`[role="${r}"]`))return!0;return!1}function Tt(e,t){let n=Z.get(e);n&&clearTimeout(n),Z.set(e,setTimeout(()=>{Z.delete(e),N(e,t)},bt))}function ht(e){I||(I=new MutationObserver(t=>{for(let n of t){for(let r of n.addedNodes){if(!(r instanceof Element))continue;if(r instanceof HTMLFormElement){N(r,e);continue}let i=r.closest("form");i instanceof HTMLFormElement&&ee.has(i)&&Et(r)&&Tt(i,e);for(let s of Array.from(r.querySelectorAll("form")))N(s,e)}for(let r of n.removedNodes){if(!(r instanceof Element))continue;let i=r instanceof HTMLFormElement?[r]:Array.from(r.querySelectorAll("form"));for(let s of i)gt(s,e)}}}),I.observe(document.body,{childList:!0,subtree:!0}))}function yt(e){window.addEventListener("hashchange",()=>R(e));let t={pushState:history.pushState.bind(history),replaceState:history.replaceState.bind(history)};history.pushState=function(...n){t.pushState(...n),R(e)},history.replaceState=function(...n){t.replaceState(...n),R(e)},window.addEventListener("popstate",()=>R(e))}async function R(e){let t=Array.from(document.querySelectorAll("form"));await Promise.allSettled(t.map(n=>N(n,e)))}var Lt=new Set(["password","hidden","file","submit","reset","button","image"]);async function Mt(e){if(!L())return;let t='[type="submit"]:not([disabled]), button[data-variant="primary"]:not([disabled])',n='[type="submit"], button[data-variant="primary"]',r=/subscribe|submit|sign[\s-]?up|send|join|go|search|post|tweet|publish/i,i=Array.from(document.querySelectorAll('input:not(form input), textarea:not(form textarea), select:not(form select), [role="textbox"]:not(form [role="textbox"]):not(input):not(textarea), [role="searchbox"]:not(form [role="searchbox"]):not(input):not(textarea), [contenteditable="true"]:not(form [contenteditable="true"]):not(input):not(textarea)')).filter(o=>{if(o instanceof HTMLInputElement&&Lt.has(o.type.toLowerCase()))return console.log(`[auto-webmcp] orphan: skipping excluded type "${o.type}" (name="${o.name}" id="${o.id}")`),!1;let l=o.getBoundingClientRect();return l.width===0||l.height===0?(console.log(`[auto-webmcp] orphan: skipping invisible input (name="${o.name}" id="${o.id}")`),!1):!0});if(console.log(`[auto-webmcp] orphan: found ${i.length} visible orphan input(s)`),i.length===0)return;let s=new Map;for(let o of i){let l=o.parentElement,a=o.parentElement??document.body;for(;l&&l!==document.body;){if(l.querySelector(n)!==null||Array.from(l.querySelectorAll("button")).some(m=>r.test(m.textContent??""))){a=l;break}l=l.parentElement}console.log(`[auto-webmcp] orphan: input (name="${o.name}" id="${o.id}") grouped into container`,a),s.has(a)||s.set(a,[]),s.get(a).push(o)}console.log(`[auto-webmcp] orphan: ${s.size} group(s) found`);for(let[o,l]of s){let a=Array.from(o.querySelectorAll(t)).filter(d=>{let E=d.getBoundingClientRect();return E.width>0&&E.height>0}),c=a[a.length-1]??null;if(!c){let d=Array.from(o.querySelectorAll(n)).filter(E=>{let T=E.getBoundingClientRect();return T.width>0&&T.height>0&&E.disabled});c=d[d.length-1]??null,c&&console.log(`[auto-webmcp] orphan: using disabled submit button as reference: "${c.textContent?.trim()}"`)}if(!c){let d=Array.from(o.querySelectorAll("button")).filter(E=>{let T=E.getBoundingClientRect();return T.width>0&&T.height>0&&!E.disabled&&r.test(E.textContent??"")});c=d[d.length-1]??null,c&&console.log(`[auto-webmcp] orphan: using text-matched button in container: "${c.textContent?.trim()}"`)}if(!c){let d=Array.from(document.querySelectorAll("button")).filter(E=>{let T=E.getBoundingClientRect();return T.width>0&&T.height>0&&r.test(E.textContent??"")});c=d[d.length-1]??null,c&&console.log(`[auto-webmcp] orphan: using page-wide fallback submit button: "${c.textContent?.trim()}"`)}console.log("[auto-webmcp] orphan: submit button for group:",c?`"${c.textContent?.trim()}" disabled=${c.disabled}`:"none");let m=Le(o,l,c);console.log(`[auto-webmcp] orphan: tool="${m.name}" schema keys:`,Object.keys(m.inputSchema.properties));let u=[],p=m.inputSchema.properties,f=/^_r_[0-9a-z]+_$/i;for(let d of l){let E=d.id&&!f.test(d.id)?d.id:null,T=d.name||d.getAttribute("name")||d.dataset.webmcpName||E||d.getAttribute("aria-label")||d.getAttribute("placeholder")||null,w=T?T.toLowerCase().replace(/[^a-z0-9]+/g,"_").replace(/^_+|_+$/g,"").slice(0,64):null,h=!!(w&&p[w]);console.log(`[auto-webmcp] orphan: field (name="${d.name??""}" id="${d.id}") rawKey="${T}" safeKey="${w}" matched=${h}`),h&&u.push({key:w,el:d})}if(console.log(`[auto-webmcp] orphan: ${u.length}/${l.length} input(s) mapped to schema keys`),u.length===0){console.log(`[auto-webmcp] orphan: skipping group "${m.name}" \u2014 no inputs mapped to schema keys`);continue}let g=m.name,y=async d=>{console.log(`[auto-webmcp] orphan execute: tool="${g}" params=`,d),console.log("[auto-webmcp] orphan execute: inputPairs=",u.map(h=>h.key));for(let{key:h,el:S}of u)d[h]!==void 0?(console.log(`[auto-webmcp] orphan execute: filling key="${h}" value=`,d[h],"element=",S),ke(S,d[h]),console.log("[auto-webmcp] orphan execute: after fill, element value=",S.value)):console.log(`[auto-webmcp] orphan execute: key="${h}" not in params, skipping`);if(window.dispatchEvent(new CustomEvent("toolactivated",{detail:{toolName:g}})),!(e.autoSubmit||!!c?.hasAttribute("toolautosubmit")||c instanceof HTMLElement&&c.dataset.webmcpAutosubmit!==void 0||o.hasAttribute("toolautosubmit")||o instanceof HTMLElement&&o.dataset.webmcpAutosubmit!==void 0))return console.log("[auto-webmcp] orphan execute: autoSubmit=false, returning without clicking submit"),{content:[{type:"text",text:"Fields filled. Ready to submit."}]};console.log("[auto-webmcp] orphan execute: polling for enabled submit button (up to 2s)...");let T=null,w=Date.now()+2e3;for(;Date.now()<w;){let h=Array.from(o.querySelectorAll(t)).filter(q=>{let te=q.getBoundingClientRect();return te.width>0&&te.height>0}),S=h[h.length-1]??null;if(S){T=S;break}await new Promise(q=>setTimeout(q,100))}return T?(console.log(`[auto-webmcp] orphan execute: clicking submit button "${T.textContent?.trim()}"`),T.click(),{content:[{type:"text",text:"Fields filled and form submitted."}]}):(console.warn("[auto-webmcp] orphan execute: submit button still disabled after 2s"),{content:[{type:"text",text:"Fields filled but the submit button is still disabled. The page may require additional input before submitting."}]})};try{let d={name:m.name,description:m.description,inputSchema:m.inputSchema,execute:y};m.annotations&&Object.keys(m.annotations).length>0&&(d.annotations=m.annotations),await navigator.modelContext.registerTool(d);let E=window.__pendingSubmitBtns??={};E[m.name]=c,e.debug&&console.log(`[auto-webmcp] Orphan tool registered: ${m.name}`,m)}catch{}}}function Ht(e,t){/^form_\d+$|^submit$|^form$/.test(e)&&console.warn(`[auto-webmcp] Tool "${e}" has a generic name. Consider adding a toolname or data-webmcp-name attribute.`),(!t||t==="Submit form")&&console.warn(`[auto-webmcp] Tool "${e}" has no meaningful description.`),/don'?t|do not|never|avoid|not for/i.test(t)&&console.warn(`[auto-webmcp] Tool "${e}" description contains negative instructions. Per spec best practices, prefer positive descriptions.`)}async function Fe(e){document.readyState==="loading"&&await new Promise(t=>document.addEventListener("DOMContentLoaded",()=>t(),{once:!0})),Ie=0,ht(e),yt(e),await R(e),await Mt(e)}function $e(){I?.disconnect(),I=null}F();async function Oe(e){let t=re(e);return t.debug&&console.debug("[auto-webmcp] Initializing",{webmcpSupported:L(),config:t}),await Fe(t),{destroy:async()=>{$e(),await G()},getTools:V,isSupported:L()}}typeof window<"u"&&!window.__AUTO_WEBMCP_NO_AUTOINIT&&Oe();return De(xt);})();
|
|
1
|
+
"use strict";var AutoWebMCP=(()=>{var B=Object.defineProperty;var Pe=Object.getOwnPropertyDescriptor;var _e=Object.getOwnPropertyNames;var Ne=Object.prototype.hasOwnProperty;var qe=(e,t)=>()=>(e&&(t=e(e=0)),t);var ne=(e,t)=>{for(var n in t)B(e,n,{get:t[n],enumerable:!0})},Be=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of _e(t))!Ne.call(e,i)&&i!==n&&B(e,i,{get:()=>t[i],enumerable:!(r=Pe(t,i))||r.enumerable});return e};var De=e=>Be(B({},"__esModule",{value:!0}),e);var He={};ne(He,{getAllRegisteredTools:()=>V,getRegisteredToolName:()=>lt,isWebMCPSupported:()=>L,registerFormTool:()=>z,unregisterAll:()=>G,unregisterFormTool:()=>C});function L(){return typeof navigator<"u"&&typeof navigator.modelContext<"u"}async function z(e,t,n){if(!L())return;H.get(e)&&await C(e);let i={name:t.name,description:t.description,inputSchema:t.inputSchema,execute:n};t.annotations&&Object.keys(t.annotations).length>0&&(i.annotations=t.annotations);try{await navigator.modelContext.registerTool(i)}catch{try{await navigator.modelContext.unregisterTool(t.name),await navigator.modelContext.registerTool(i)}catch{}}H.set(e,t.name)}async function C(e){if(!L())return;let t=H.get(e);if(t){try{await navigator.modelContext.unregisterTool(t)}catch{}H.delete(e)}}function lt(e){return H.get(e)}function V(){return Array.from(H.entries()).map(([e,t])=>({form:e,name:t}))}async function G(){let e=Array.from(H.entries());await Promise.all(e.map(([t])=>C(t)))}var H,F=qe(()=>{"use strict";H=new Map});var xt={};ne(xt,{autoWebMCP:()=>Oe});function re(e){return{exclude:e?.exclude??[],autoSubmit:e?.autoSubmit??!1,overrides:e?.overrides??{},debug:e?.debug??!1}}var v=["textbox","combobox","checkbox","radio","switch","spinbutton","searchbox","slider"];function W(e){return e instanceof HTMLInputElement?We(e):e instanceof HTMLTextAreaElement?{type:"string"}:e instanceof HTMLSelectElement?je(e):null}function We(e){switch(e.type.toLowerCase()){case"text":case"search":case"tel":return D(e);case"email":return{...D(e),format:"email"};case"url":return{...D(e),format:"uri"};case"number":case"range":{let n={type:"number"};return e.min!==""&&(n.minimum=parseFloat(e.min)),e.max!==""&&(n.maximum=parseFloat(e.max)),n}case"date":return{type:"string",format:"date"};case"datetime-local":return{type:"string",format:"date-time"};case"time":return{type:"string",format:"time"};case"month":return{type:"string",pattern:"^\\d{4}-\\d{2}$"};case"week":return{type:"string",pattern:"^\\d{4}-W\\d{2}$"};case"color":return{type:"string",pattern:"^#[0-9a-fA-F]{6}$"};case"checkbox":return{type:"boolean"};case"radio":return{type:"string"};case"file":case"hidden":case"submit":case"reset":case"button":case"image":return null;case"password":return null;default:return{type:"string"}}}function D(e){let t={type:"string"};e.minLength>0&&(t.minLength=e.minLength),e.maxLength>0&&e.maxLength!==524288&&(t.maxLength=e.maxLength),e.pattern&&(t.pattern=e.pattern);let n=e.getAttribute("list");if(n){let r=e.ownerDocument.getElementById(n);if(r instanceof HTMLDataListElement){let i=Array.from(r.options).filter(s=>!s.disabled&&s.value.trim()!=="");i.length>0&&(t.enum=i.map(s=>s.value.trim()),t.oneOf=i.map(s=>({const:s.value.trim(),title:s.textContent?.trim()||s.value.trim()})))}}return t}var Je=/^(select|choose|pick)\b|^--+|---/i;function oe(e){return e.disabled?!0:e.value!==""?!1:Je.test(e.text.trim())}function je(e){let t=[],n=[];for(let r of Array.from(e.children))if(r instanceof HTMLOptGroupElement){if(r.disabled)continue;let i=r.label?.trim()??"";for(let s of Array.from(r.children)){if(!(s instanceof HTMLOptionElement)||oe(s))continue;t.push(s.value);let o={const:s.value,title:s.text.trim()||s.value};i&&(o.group=i),n.push(o)}}else if(r instanceof HTMLOptionElement){if(oe(r))continue;t.push(r.value),n.push({const:r.value,title:r.text.trim()||r.value})}return t.length===0?{type:"string"}:e.multiple?{type:"array",items:{type:"string",enum:t}}:{type:"string",enum:t,oneOf:n}}function ie(e,t){return Array.from(e.querySelectorAll(`input[type="checkbox"][name="${CSS.escape(t)}"]`)).map(n=>n.value).filter(n=>n!==""&&n!=="on")}function se(e,t){return Array.from(e.querySelectorAll(`input[type="radio"][name="${CSS.escape(t)}"]`)).map(r=>r.value).filter(r=>r!=="")}function ae(e,t){return Array.from(e.querySelectorAll(`input[type="radio"][name="${CSS.escape(t)}"]`)).filter(r=>r.value!=="").map(r=>{let i=Ue(r);return{const:r.value,title:i||r.value}})}function le(e,t){switch(t){case"checkbox":case"switch":return{type:"boolean"};case"spinbutton":case"slider":{let n={type:"number"},r=e.getAttribute("aria-valuemin"),i=e.getAttribute("aria-valuemax");return r!==null&&(n.minimum=parseFloat(r)),i!==null&&(n.maximum=parseFloat(i)),n}case"combobox":{let n=e.getAttribute("aria-owns")??e.getAttribute("aria-controls");if(n){let r=document.getElementById(n);if(r){let i=Array.from(r.querySelectorAll('[role="option"]')).filter(s=>s.getAttribute("aria-disabled")!=="true");if(i.length>0){let s=i.map(l=>(l.getAttribute("data-value")??l.textContent??"").trim()).filter(Boolean),o=i.map(l=>({const:(l.getAttribute("data-value")??l.textContent??"").trim(),title:(l.textContent??"").trim()}));return{type:"string",enum:s,oneOf:o}}}}return{type:"string"}}case"textbox":case"searchbox":case"radio":default:return{type:"string"}}}function Ue(e){let t=e.closest("label");if(t){let n=t.cloneNode(!0);n.querySelectorAll("input, select, textarea, button").forEach(i=>i.remove());let r=n.textContent?.trim()??"";if(r)return r}if(e.id){let n=document.querySelector(`label[for="${CSS.escape(e.id)}"]`);if(n){let r=n.textContent?.trim()??"";if(r)return r}}return""}var me=0;function de(e,t){let n=t?.name??ze(e),r=t?.description??Ge(e),{schema:i,fieldElements:s}=Qe(e),o=Ye(e);return{name:n,description:r,inputSchema:i,annotations:o,fieldElements:s}}function ze(e){let t=e.getAttribute("toolname");if(t)return b(t);let n=e.dataset.webmcpName;if(n)return b(n);let r=J(e);if(r)return b(r);let i=pe(e);if(i)return b(i);if(e.id)return b(e.id);if(e.name)return b(e.name);if(e.action){let s=Ve(e.action);if(s)return b(s)}return`form_${++me}`}function b(e){return e.toLowerCase().trim().replace(/[^a-z0-9]+/g,"_").replace(/^_+|_+$/g,"").slice(0,64)||"form"}function J(e){let t=[...Array.from(e.querySelectorAll('button[type="submit"], button:not([type])')),...Array.from(e.querySelectorAll('input[type="submit"]'))];for(let n of t){let r=n instanceof HTMLInputElement?n.value.trim():n.textContent?.trim()??"";if(r&&r.length>0&&r.length<80)return r}return""}function pe(e){let t=e;for(;t;){let n=t.previousElementSibling;for(;n;){if(/^H[1-3]$/i.test(n.tagName)){let r=n.textContent?.trim()??"";if(r)return r}n=n.previousElementSibling}if(t=t.parentElement,!t||t===document.body)break}return""}function Ve(e){try{let n=new URL(e,window.location.href).pathname.split("/").filter(Boolean);return n[n.length-1]??""}catch{return""}}function Ge(e){let t=e.getAttribute("tooldescription");if(t)return t.trim();let n=e.dataset.webmcpDescription;if(n)return n.trim();let r=e.querySelector("legend");if(r?.textContent?.trim())return r.textContent.trim();let i=e.getAttribute("aria-label");if(i?.trim())return i.trim();let s=e.getAttribute("aria-describedby");if(s){let a=document.getElementById(s);if(a?.textContent?.trim())return a.textContent.trim()}let o=pe(e),l=document.title?.trim();return o&&l?`${o}: ${l}`:o||l||"Submit form"}var fe=/^(search|find|look|filter|browse|view|show|check|preview|get|fetch|retrieve|load)\b/i,ge=/^(delete|remove|cancel|terminate|destroy|purge|revoke|unsubscribe|deactivate)\b/i,Ke=/\/(delete|remove|cancel|destroy)\b/i;function Ye(e){let t={};if(e.dataset.webmcpReadonly!==void 0&&(t.readOnlyHint=e.dataset.webmcpReadonly!=="false"),e.dataset.webmcpDestructive!==void 0&&(t.destructiveHint=e.dataset.webmcpDestructive!=="false"),e.dataset.webmcpIdempotent!==void 0&&(t.idempotentHint=e.dataset.webmcpIdempotent!=="false"),e.dataset.webmcpOpenworld!==void 0&&(t.openWorldHint=e.dataset.webmcpOpenworld!=="false"),t.readOnlyHint===void 0){let r=e.method.toLowerCase()==="get",i=J(e),s=i?fe.test(i.trim()):!1;(r||s)&&(t.readOnlyHint=!0)}if(t.destructiveHint===void 0){let r=J(e),i=r?ge.test(r.trim()):!1,s=e.action?Ke.test(e.action):!1;(i||s)&&(t.destructiveHint=!0)}return t.idempotentHint===void 0&&(t.readOnlyHint===!0||e.method.toLowerCase()==="get")&&(t.idempotentHint=!0),t.openWorldHint===void 0&&(t.openWorldHint=t.readOnlyHint!==!0),t.readOnlyHint===!0||t.destructiveHint===!0||t.idempotentHint===!0||t.openWorldHint===!1?t:{}}function Xe(e){if(e instanceof HTMLInputElement){let t=e.type.toLowerCase();return t==="checkbox"?e.checked?!0:void 0:t==="radio"?void 0:t==="number"||t==="range"?e.value!==""?parseFloat(e.value):void 0:e.value!==""?e.value:void 0}if(e instanceof HTMLTextAreaElement)return e.value!==""?e.value:void 0;if(e instanceof HTMLSelectElement){if(e.multiple){let t=Array.from(e.options).filter(n=>n.selected).map(n=>n.value);return t.length>0?t:void 0}return e.value!==""?e.value:void 0}}function be(e,t=new Set){if(t.has(e))return[];t.add(e);let n=[];for(let r of Array.from(e.querySelectorAll("*")))r.shadowRoot&&n.push(...Array.from(r.shadowRoot.querySelectorAll("input, textarea, select")),...be(r.shadowRoot,t));return n}function Qe(e){let t={},n=[],r=new Map,i=new Set,s=new Set,o=[...Array.from(e.querySelectorAll("input, textarea, select")),...be(e)];for(let c of o){let m=c.name,u=m||he(c);if(!u)continue;if(c instanceof HTMLInputElement&&c.type==="radio"){if(i.has(u))continue;i.add(u)}if(c instanceof HTMLInputElement&&c.type==="checkbox"){if(s.has(u))continue;s.add(u)}let p=W(c);if(!p||!U(c))continue;p.title=Te(c);let f=ye(c);f&&(p.description=f);let g=Xe(c);if(g!==void 0&&(p.default=g),c instanceof HTMLInputElement&&c.type==="radio"){p.enum=se(e,u);let y=ae(e,u);y.length>0&&(p.oneOf=y);let d=e.querySelector(`input[type="radio"][name="${CSS.escape(u)}"]:checked`);d?.value&&(p.default=d.value)}if(c instanceof HTMLInputElement&&c.type==="checkbox"){let y=ie(e,u);if(y.length>1){let d={type:"array",items:{type:"string",enum:y},title:p.title};p.description&&(d.description=p.description);let E=Array.from(e.querySelectorAll(`input[type="checkbox"][name="${CSS.escape(u)}"]:checked`)).map(h=>h.value);E.length>0&&(d.default=E),t[u]=d,c.required&&n.push(u);continue}}t[u]=p,m||r.set(u,c),c.required&&n.push(u)}let l=et(e),a=new Set;for(let{el:c,role:m,key:u,enumValues:p,enumOneOf:f}of l){if(t[u])continue;if(m==="radio"){if(a.has(u))continue;a.add(u)}let g=le(c,m);p&&p.length>0&&(g.enum=p,f&&f.length>0&&(g.oneOf=f)),g.title=tt(c);let y=nt(c);y&&(g.description=y),t[u]=g,r.set(u,c),c.getAttribute("aria-required")==="true"&&n.push(u)}return{schema:{$schema:"https://json-schema.org/draft/2020-12/schema",type:"object",properties:t,required:n},fieldElements:r}}var Ee=/^_r_[0-9a-z]+_$|^:[a-z0-9]+:$/i;function he(e){let t=e;if(t.dataset.webmcpName)return b(t.dataset.webmcpName);if(e.id&&!Ee.test(e.id))return b(e.id);let n=e.getAttribute("aria-label");return n?b(n):(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement)&&e.placeholder?.trim()?b(e.placeholder.trim()):e instanceof HTMLInputElement&&e.type!=="text"?e.type:null}function Ze(e){if(e.dataset.webmcpName)return b(e.dataset.webmcpName);if(e.id&&!Ee.test(e.id))return b(e.id);let t=e.getAttribute("aria-label");if(t)return b(t);let n=e.getAttribute("placeholder");return n?b(n):null}function et(e){let t=v.map(a=>`[role="${a}"]`).join(", "),n=[];for(let a of Array.from(e.querySelectorAll(t))){if(a instanceof HTMLInputElement||a instanceof HTMLTextAreaElement||a instanceof HTMLSelectElement||a.getAttribute("aria-hidden")==="true"||a.hidden)continue;let c=a.getAttribute("role"),m=ce(a);m&&n.push({el:a,role:c,key:m})}let r=n.filter(a=>a.role==="radio"),i=n.filter(a=>a.role!=="radio"),s=new Map,o=[];for(let a of r){let c=a.el.closest('[role="radiogroup"]');c?(s.has(c)||s.set(c,[]),s.get(c).push(a.el)):o.push(a)}let l=[];for(let[a,c]of s){let m=ce(a);if(!m)continue;let u=c.map(f=>(f.getAttribute("data-value")??f.getAttribute("aria-label")??f.textContent??"").trim()).filter(Boolean),p=c.map(f=>{let g=(f.getAttribute("data-value")??f.getAttribute("aria-label")??f.textContent??"").trim(),y=(f.getAttribute("aria-label")??f.textContent??"").trim();return{const:g,title:y||g}}).filter(f=>f.const!=="");u.length>0&&l.push({el:a,role:"radio",key:m,enumValues:u,enumOneOf:p})}return[...i,...l,...o]}function ce(e){let t=e;if(t.dataset?.webmcpName)return b(t.dataset.webmcpName);if(e.id)return b(e.id);let n=e.getAttribute("aria-label");if(n)return b(n);let r=e.getAttribute("aria-labelledby");if(r){let i=document.getElementById(r)?.textContent?.trim();if(i)return b(i)}return null}function tt(e){let t=e;if(t.dataset?.webmcpTitle)return t.dataset.webmcpTitle;let n=e.getAttribute("aria-label");if(n)return n.trim();let r=e.getAttribute("aria-labelledby");if(r){let i=document.getElementById(r)?.textContent?.trim();if(i)return i}return e.id?j(e.id):""}function nt(e){let t=e.getAttribute("toolparamdescription");if(t)return t.trim();let n=e;if(n.dataset?.webmcpDescription)return n.dataset.webmcpDescription;let r=e.getAttribute("aria-description");if(r)return r;let i=e.getAttribute("aria-describedby");if(i){let o=document.getElementById(i)?.textContent?.trim();if(o)return o}let s=e.getAttribute("placeholder")??e.dataset?.placeholder;return s?s.trim():""}function Te(e){if("dataset"in e&&e.dataset.webmcpTitle)return e.dataset.webmcpTitle;let t=rt(e);return t||(e.name?j(e.name):e.id?j(e.id):(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement)&&e.placeholder?.trim()?e.placeholder.trim():"")}function ye(e){let t=e.getAttribute("toolparamdescription");if(t)return t.trim();let n=e;if(n.dataset.webmcpDescription)return n.dataset.webmcpDescription;let r=e.getAttribute("aria-description");if(r)return r;let i=e.getAttribute("aria-describedby");if(i){let s=document.getElementById(i);if(s?.textContent?.trim())return s.textContent.trim()}if(e instanceof HTMLInputElement||e instanceof HTMLTextAreaElement){let s=e.placeholder?.trim();if(s&&s.length>0)return s}return""}function rt(e){if(e.id){let n=document.querySelector(`label[for="${CSS.escape(e.id)}"]`);if(n){let r=ue(n);if(r)return r}}let t=e.closest("label");if(t){let n=ue(t);if(n)return n}return""}function ue(e){let t=e.cloneNode(!0);return t.querySelectorAll("input, select, textarea, button").forEach(n=>n.remove()),t.textContent?.trim()??""}function j(e){return e.replace(/[-_]/g," ").replace(/([a-z])([A-Z])/g,"$1 $2").trim().replace(/\b\w/g,t=>t.toUpperCase())}function U(e){let t=window.getComputedStyle(e);if(t.display==="none"||t.visibility==="hidden"||e.offsetParent===null&&t.position!=="fixed")return!1;let n=e;for(;n&&n!==document.body;){if(n.getAttribute("aria-hidden")==="true")return!1;n=n.parentElement}return!e.closest("fieldset")?.disabled}function Le(e,t,n){let r=it(e,n),i=st(e),{schema:s,fieldElements:o}=at(t),l=ot(n);return{name:r,description:i,inputSchema:s,annotations:l,fieldElements:o}}function ot(e){let t={},n=e instanceof HTMLInputElement?e.value.trim():e?.textContent?.trim()??"";return fe.test(n)&&(t.readOnlyHint=!0,t.idempotentHint=!0),ge.test(n)&&(t.destructiveHint=!0),t.readOnlyHint!==!0&&(t.openWorldHint=!0),t.readOnlyHint===!0||t.destructiveHint===!0||t.idempotentHint===!0||t.openWorldHint===!1?t:{}}function it(e,t){if(t){let i=t instanceof HTMLInputElement?t.value.trim():t.textContent?.trim()??"";if(i&&i.length>0&&i.length<80)return b(i)}let n=Me(e);if(n)return b(n);let r=document.title?.trim();return r?b(r):`form_${++me}`}function st(e){let t=Me(e),n=document.title?.trim();return t&&n&&t!==n?`${t} on ${n}`:t||n||"Submit form"}function Me(e){let t=e.querySelector("h1, h2, h3");if(t?.textContent?.trim())return t.textContent.trim();let n=e;for(;n;){let r=n.previousElementSibling;for(;r;){if(/^H[1-3]$/i.test(r.tagName)){let i=r.textContent?.trim()??"";if(i)return i}r=r.previousElementSibling}if(n=n.parentElement,!n||n===document.body)break}return""}function at(e){let t={},n=[],r=new Map,i=new Set,s=new Set;for(let o of e){if(!(o instanceof HTMLInputElement)&&!(o instanceof HTMLTextAreaElement)&&!(o instanceof HTMLSelectElement)){let u=Ze(o);if(!u||!U(o))continue;let p={type:"string"};p.title=o.getAttribute("aria-label")??u;let f=(o.getAttribute("aria-description")??o.getAttribute("aria-describedby"),null);f&&(p.description=f),t[u]=p,r.set(u,o),n.push(u);continue}let l=o.name,a=(l?b(l):null)||he(o);if(!a)continue;if(o instanceof HTMLInputElement&&o.type==="radio"){if(i.has(a))continue;i.add(a)}if(o instanceof HTMLInputElement&&o.type==="checkbox"){if(s.has(a))continue;s.add(a)}let c=W(o);if(!c||!U(o))continue;c.title=Te(o);let m=ye(o);if(m&&(c.description=m),o instanceof HTMLInputElement&&o.type==="checkbox"){let u=e.filter(p=>p instanceof HTMLInputElement&&p.type==="checkbox"&&p.name===a).map(p=>p.value).filter(p=>p!==""&&p!=="on");if(u.length>1){let p={type:"array",items:{type:"string",enum:u},title:c.title};c.description&&(p.description=c.description),t[a]=p,o.required&&n.push(a);continue}}t[a]=c,l||r.set(a,o),o.required&&n.push(a)}return{schema:{$schema:"https://json-schema.org/draft/2020-12/schema",type:"object",properties:t,required:n},fieldElements:r}}F();var $=new WeakMap,Ae=new WeakMap,O=new WeakMap,M=new WeakMap,x=new WeakMap,P=new WeakMap,ct=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,"value")?.set,ut=Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype,"value")?.set,A=Object.getOwnPropertyDescriptor(HTMLInputElement.prototype,"checked")?.set;function ve(e,t,n,r){return r?.fieldElements&&O.set(e,r.fieldElements),xe(e,n),async i=>{x.set(e,[]),M.delete(e),K(e,i);let s=pt(r,i);return s.length>0&&M.set(e,s),window.dispatchEvent(new CustomEvent("toolactivated",{detail:{toolName:n}})),new Promise((o,l)=>{$.set(e,{resolve:o,reject:l}),(t.autoSubmit||e.hasAttribute("toolautosubmit")||e.dataset.webmcpAutosubmit!==void 0)&&Se(e).then(async()=>{try{K(e,i);for(let c=0;c<2&&dt(e,i,O.get(e)).length!==0;c++)K(e,i),await Se(e,400,100);let a=e;if(!e.isConnected){let m=document.querySelector('button[type="submit"]:not([disabled]), input[type="submit"]:not([disabled])')?.closest("form");m&&(a=m,$.set(a,{resolve:o,reject:l}),xe(a,n))}a!==e&&M.has(e)&&(M.set(a,M.get(e)),M.delete(e)),a.requestSubmit()}catch(a){l(a instanceof Error?a:new Error(String(a)))}})})}}function xe(e,t){e.__awmcp_intercepted||(e.__awmcp_intercepted=!0,e.addEventListener("submit",n=>{let r=$.get(e);if(!r)return;let{resolve:i}=r;$.delete(e);let s=mt(e,Ae.get(e),O.get(e));P.delete(e);let o=M.get(e)??[];M.delete(e);let l=x.get(e)??[];x.delete(e);let a=l.filter(g=>g.type==="not_filled").map(g=>g.field),c={status:o.length>0||a.length>0?"partial":"success",filled_fields:s,skipped_fields:a,missing_required:o,warnings:[...o.map(g=>({field:g,type:"missing_required",message:`required field "${g}" was not provided`})),...l]},m=[...o.length?[`required fields were not filled: ${o.join(", ")}`]:[],...l.map(g=>g.message)],u=m.length?` Note: ${m.join("; ")}.`:"",f={content:[{type:"text",text:`Form submitted. Fields: ${JSON.stringify(s)}${u}`},{type:"text",text:JSON.stringify(c)}]};n.agentInvoked&&typeof n.respondWith=="function"&&(n.preventDefault(),n.respondWith(Promise.resolve(f))),i(f)}),e.addEventListener("reset",()=>{P.delete(e),window.dispatchEvent(new CustomEvent("toolcancel",{detail:{toolName:t}}))}))}function k(e,t){if(e.focus(),e.select?.(),document.execCommand("insertText",!1,t)&&e.value===t)return;let n=e instanceof HTMLTextAreaElement?ut:ct;n?n.call(e,t):e.value=t,e.dispatchEvent(new InputEvent("input",{bubbles:!0,cancelable:!0,inputType:"insertText",data:t})),e.dispatchEvent(new Event("change",{bubbles:!0}))}function Y(e,t){A?A.call(e,t):e.checked=t,e.dispatchEvent(new Event("change",{bubbles:!0}))}function _(e,t){for(let n of Array.from(e.querySelectorAll("*"))){let r=n.shadowRoot;if(!r)continue;let i=r.querySelector(t);if(i)return i;let s=_(r,t);if(s)return s}return null}function Q(e,t){let n=CSS.escape(t),r=e.querySelector(`[name="${n}"]`)??e.querySelector(`input#${n}, textarea#${n}, select#${n}`);return r||(_(document,`[name="${n}"]`)??_(document,`input#${n}, textarea#${n}, select#${n}`))}function K(e,t){Ae.set(e,t);let n=O.get(e),r={};for(let[i,s]of Object.entries(t)){let o=Q(e,i);if(o){if(o instanceof HTMLInputElement)if(we(o,e,i,s),o.type==="checkbox")if(Array.isArray(s)){let a=CSS.escape(i);r[i]=Array.from(e.querySelectorAll(`input[type="checkbox"][name="${a}"]`)).filter(c=>c.checked).map(c=>c.value)}else r[i]=o.checked;else r[i]=o.value;else o instanceof HTMLTextAreaElement?(k(o,String(s??"")),r[i]=o.value):o instanceof HTMLSelectElement&&(X(o,s,e,i),r[i]=o.multiple?Array.from(o.options).filter(a=>a.selected).map(a=>a.value):o.value);continue}let l=n?.get(i);if(l){let a=l;if(!l.isConnected){let c=l.id;if(c){let m=document.getElementById(c)??_(document,`#${CSS.escape(c)}`);m&&(a=m)}}a instanceof HTMLInputElement?(we(a,e,i,s),r[i]=a.type==="checkbox"?a.checked:a.value):a instanceof HTMLTextAreaElement?(k(a,String(s??"")),r[i]=a.value):a instanceof HTMLSelectElement?(X(a,s,e,i),r[i]=a.multiple?Array.from(a.options).filter(c=>c.selected).map(c=>c.value):a.value):(Ce(a,s),r[i]=s)}}P.set(e,r),window.__lastFillWarnings=x.get(e)??[]}function we(e,t,n,r){let i=e.type.toLowerCase();if(i==="checkbox"){if(Array.isArray(r)){let s=CSS.escape(n),o=t.querySelectorAll(`input[type="checkbox"][name="${s}"]`);for(let l of o)Y(l,r.map(String).includes(l.value));return}Y(e,!!r);return}if(i==="number"||i==="range"){let s=String(r??""),o=Number(s);if(s===""||isNaN(o)){x.get(t)?.push({field:n,type:"type_mismatch",message:`"${n}" expects a number, got: ${JSON.stringify(r)}`,original:r});return}let l=e.min!==""?parseFloat(e.min):-1/0,a=e.max!==""?parseFloat(e.max):1/0;if(o<l||o>a){let c=Math.min(Math.max(o,l),a);x.get(t)?.push({field:n,type:"clamped",message:`"${n}" value ${o} is outside allowed range [${e.min||"?"}, ${e.max||"?"}], clamped to ${c}`,original:o,actual:c}),e.value=String(c)}else e.value=String(o);e.dispatchEvent(new InputEvent("input",{bubbles:!0,cancelable:!0,inputType:"insertText",data:String(o)})),e.dispatchEvent(new Event("change",{bubbles:!0}));return}if(i==="radio"){let s=CSS.escape(n),o=t.querySelectorAll(`input[type="radio"][name="${s}"]`);for(let l of o)if(l.value===String(r)){A?A.call(l,!0):l.checked=!0,l.dispatchEvent(new Event("change",{bubbles:!0}));break}return}k(e,String(r??""))}function X(e,t,n,r){if(e.multiple){let s=Array.isArray(t)?t.map(String):[String(t??"")];for(let o of Array.from(e.options))o.selected=s.includes(o.value);e.dispatchEvent(new Event("change",{bubbles:!0}));return}let i=String(t??"");if(e.value=i,e.value!==i){let s=i.toLowerCase(),o=Array.from(e.options).find(l=>l.text.trim().toLowerCase()===s||l.label.trim().toLowerCase()===s);o?e.value=o.value:n&&r&&x.get(n)?.push({field:r,type:"not_filled",message:`"${r}" value "${i}" did not match any option in the select`,original:i})}e.dispatchEvent(new Event("change",{bubbles:!0}))}function Ce(e,t){let n=e.getAttribute("role");if(n==="checkbox"||n==="switch"){e.setAttribute("aria-checked",String(!!t)),e.dispatchEvent(new MouseEvent("click",{bubbles:!0}));return}if(n==="radio"){e.setAttribute("aria-checked","true"),e.dispatchEvent(new MouseEvent("click",{bubbles:!0}));return}if(n==="radiogroup"){let i=Array.from(e.querySelectorAll('[role="radio"]'));for(let s of i)if((s.getAttribute("data-value")??s.getAttribute("aria-label")??s.textContent??"").trim()===String(t)){s.setAttribute("aria-checked","true"),s.dispatchEvent(new MouseEvent("click",{bubbles:!0}));for(let l of i)l!==s&&l.setAttribute("aria-checked","false");break}return}let r=e;if(r.isContentEditable){r.focus();let i=document.createRange();i.selectNodeContents(r);let s=window.getSelection();s?.removeAllRanges(),s?.addRange(i);let o=!1;try{let l=new DataTransfer;l.setData("text/plain",String(t??""));let a=new ClipboardEvent("paste",{bubbles:!0,cancelable:!0,composed:!0,clipboardData:l});o=!r.dispatchEvent(a)}catch{}o||document.execCommand("insertText",!1,String(t??""))}else e.dispatchEvent(new Event("input",{bubbles:!0})),e.dispatchEvent(new Event("change",{bubbles:!0}))}function mt(e,t,n){let r={},i=new FormData(e),s=P.get(e);for(let[o,l]of i.entries())if(r[o]!==void 0){let a=r[o];Array.isArray(a)?a.push(l):r[o]=[a,l]}else r[o]=l;if(t)for(let o of Object.keys(t)){if(o in r)continue;if(s&&o in s){r[o]=s[o];continue}let l=Q(e,o)??n?.get(o)??null;if(l)if(l instanceof HTMLInputElement&&l.type==="checkbox")r[o]=l.checked;else if(l instanceof HTMLInputElement||l instanceof HTMLTextAreaElement||l instanceof HTMLSelectElement)r[o]=l.value;else{let a=l.getAttribute("role");a==="checkbox"||a==="switch"?r[o]=l.getAttribute("aria-checked")==="true":r[o]=l.textContent?.trim()??""}}return r}function ke(e,t){if(e instanceof HTMLInputElement){let n=e.type.toLowerCase();n==="checkbox"?Y(e,!!t):n==="radio"?e.value===String(t)&&(A?A.call(e,!0):e.checked=!0,e.dispatchEvent(new Event("change",{bubbles:!0}))):k(e,String(t??""))}else e instanceof HTMLTextAreaElement?k(e,String(t??"")):e instanceof HTMLSelectElement?X(e,t):Ce(e,t)}function Se(e,t=800,n=150){return new Promise(r=>{let i=!1,s=null,o=()=>{i||(i=!0,l.disconnect(),s!==null&&clearTimeout(s),r())},l=new MutationObserver(()=>{s!==null&&clearTimeout(s),s=setTimeout(o,n)});l.observe(e,{childList:!0,subtree:!0,attributes:!0,characterData:!0}),setTimeout(o,t),s=setTimeout(o,n)})}function dt(e,t,n){let r=[];for(let[i,s]of Object.entries(t)){let o=Q(e,i)??n?.get(i)??null;o&&(o instanceof HTMLInputElement&&o.type==="checkbox"?o.checked!==!!s&&r.push(i):(o instanceof HTMLInputElement||o instanceof HTMLTextAreaElement||o instanceof HTMLSelectElement)&&o.value!==String(s??"")&&r.push(i))}return r}function pt(e,t){return e?.inputSchema?.required?.length?e.inputSchema.required.filter(n=>!(n in t)):[]}function Re(e,t,n){window.dispatchEvent(new CustomEvent(e,{detail:{form:t,toolName:n}}))}function ft(e,t){if(e.dataset.noWebmcp!==void 0)return!0;for(let n of t.exclude)try{if(e.matches(n))return!0}catch{}return!1}async function N(e,t){if(ft(e,t))return;let n;for(let[l,a]of Object.entries(t.overrides))try{if(e.matches(l)){n=a;break}}catch{}let r=de(e,n);t.debug&&Ht(r.name,r.description);let i=ve(e,t,r.name,r);await z(e,r,i),ee.add(e),Ie++;let s=e.querySelector('[type="submit"], button[data-variant="primary"], button:not([type])')??null,o=window.__pendingSubmitBtns??={};o[r.name]=s,t.debug&&console.log(`[auto-webmcp] Registered: ${r.name}`,r),Re("form:registered",e,r.name)}async function gt(e,t){let{getRegisteredToolName:n}=await Promise.resolve().then(()=>(F(),He)),r=n(e);r&&(await C(e),ee.delete(e),t.debug&&console.log(`[auto-webmcp] Unregistered: ${r}`),Re("form:unregistered",e,r))}var I=null,ee=new WeakSet,Ie=0,Z=new Map,bt=300;function Et(e){let t=e.tagName.toLowerCase();if(t==="input"||t==="textarea"||t==="select")return!0;let n=e.getAttribute("role");if(n&&v.includes(n)||e.querySelector("input, textarea, select"))return!0;for(let r of v)if(e.querySelector(`[role="${r}"]`))return!0;return!1}function ht(e,t){let n=Z.get(e);n&&clearTimeout(n),Z.set(e,setTimeout(()=>{Z.delete(e),N(e,t)},bt))}function Tt(e){I||(I=new MutationObserver(t=>{for(let n of t){for(let r of n.addedNodes){if(!(r instanceof Element))continue;if(r instanceof HTMLFormElement){N(r,e);continue}let i=r.closest("form");i instanceof HTMLFormElement&&ee.has(i)&&Et(r)&&ht(i,e);for(let s of Array.from(r.querySelectorAll("form")))N(s,e)}for(let r of n.removedNodes){if(!(r instanceof Element))continue;let i=r instanceof HTMLFormElement?[r]:Array.from(r.querySelectorAll("form"));for(let s of i)gt(s,e)}}}),I.observe(document.body,{childList:!0,subtree:!0}))}function yt(e){window.addEventListener("hashchange",()=>R(e));let t={pushState:history.pushState.bind(history),replaceState:history.replaceState.bind(history)};history.pushState=function(...n){t.pushState(...n),R(e)},history.replaceState=function(...n){t.replaceState(...n),R(e)},window.addEventListener("popstate",()=>R(e))}async function R(e){let t=Array.from(document.querySelectorAll("form"));await Promise.allSettled(t.map(n=>N(n,e)))}var Lt=new Set(["password","hidden","file","submit","reset","button","image"]);async function Mt(e){if(!L())return;let t='[type="submit"]:not([disabled]), button[data-variant="primary"]:not([disabled])',n='[type="submit"], button[data-variant="primary"]',r=/subscribe|submit|sign[\s-]?up|send|join|go|search|post|tweet|publish/i,i=Array.from(document.querySelectorAll('input:not(form input), textarea:not(form textarea), select:not(form select), [role="textbox"]:not(form [role="textbox"]):not(input):not(textarea), [role="searchbox"]:not(form [role="searchbox"]):not(input):not(textarea), [contenteditable="true"]:not(form [contenteditable="true"]):not(input):not(textarea)')).filter(o=>{if(o instanceof HTMLInputElement&&Lt.has(o.type.toLowerCase()))return console.log(`[auto-webmcp] orphan: skipping excluded type "${o.type}" (name="${o.name}" id="${o.id}")`),!1;let l=o.getBoundingClientRect();return l.width===0||l.height===0?(console.log(`[auto-webmcp] orphan: skipping invisible input (name="${o.name}" id="${o.id}")`),!1):!0});if(console.log(`[auto-webmcp] orphan: found ${i.length} visible orphan input(s)`),i.length===0)return;let s=new Map;for(let o of i){let l=o.parentElement,a=o.parentElement??document.body;for(;l&&l!==document.body;){if(l.querySelector(n)!==null||Array.from(l.querySelectorAll("button")).some(m=>r.test(m.textContent??""))){a=l;break}l=l.parentElement}console.log(`[auto-webmcp] orphan: input (name="${o.name}" id="${o.id}") grouped into container`,a),s.has(a)||s.set(a,[]),s.get(a).push(o)}console.log(`[auto-webmcp] orphan: ${s.size} group(s) found`);for(let[o,l]of s){let a=Array.from(o.querySelectorAll(t)).filter(d=>{let E=d.getBoundingClientRect();return E.width>0&&E.height>0}),c=a[a.length-1]??null;if(!c){let d=Array.from(o.querySelectorAll(n)).filter(E=>{let h=E.getBoundingClientRect();return h.width>0&&h.height>0&&E.disabled});c=d[d.length-1]??null,c&&console.log(`[auto-webmcp] orphan: using disabled submit button as reference: "${c.textContent?.trim()}"`)}if(!c){let d=Array.from(o.querySelectorAll("button")).filter(E=>{let h=E.getBoundingClientRect();return h.width>0&&h.height>0&&!E.disabled&&r.test(E.textContent??"")});c=d[d.length-1]??null,c&&console.log(`[auto-webmcp] orphan: using text-matched button in container: "${c.textContent?.trim()}"`)}if(!c){let d=Array.from(document.querySelectorAll("button")).filter(E=>{let h=E.getBoundingClientRect();return h.width>0&&h.height>0&&r.test(E.textContent??"")});c=d[d.length-1]??null,c&&console.log(`[auto-webmcp] orphan: using page-wide fallback submit button: "${c.textContent?.trim()}"`)}console.log("[auto-webmcp] orphan: submit button for group:",c?`"${c.textContent?.trim()}" disabled=${c.disabled}`:"none");let m=Le(o,l,c);console.log(`[auto-webmcp] orphan: tool="${m.name}" schema keys:`,Object.keys(m.inputSchema.properties));let u=[],p=m.inputSchema.properties,f=/^_r_[0-9a-z]+_$/i;for(let d of l){let E=d.id&&!f.test(d.id)?d.id:null,h=d.name||d.getAttribute("name")||d.dataset.webmcpName||E||d.getAttribute("aria-label")||d.getAttribute("placeholder")||null,w=h?h.toLowerCase().replace(/[^a-z0-9]+/g,"_").replace(/^_+|_+$/g,"").slice(0,64):null,T=!!(w&&p[w]);console.log(`[auto-webmcp] orphan: field (name="${d.name??""}" id="${d.id}") rawKey="${h}" safeKey="${w}" matched=${T}`),T&&u.push({key:w,el:d})}if(console.log(`[auto-webmcp] orphan: ${u.length}/${l.length} input(s) mapped to schema keys`),u.length===0){console.log(`[auto-webmcp] orphan: skipping group "${m.name}" \u2014 no inputs mapped to schema keys`);continue}let g=m.name,y=async d=>{console.log(`[auto-webmcp] orphan execute: tool="${g}" params=`,d),console.log("[auto-webmcp] orphan execute: inputPairs=",u.map(T=>T.key));for(let{key:T,el:S}of u)d[T]!==void 0?(console.log(`[auto-webmcp] orphan execute: filling key="${T}" value=`,d[T],"element=",S),ke(S,d[T]),console.log("[auto-webmcp] orphan execute: after fill, element value=",S.value)):console.log(`[auto-webmcp] orphan execute: key="${T}" not in params, skipping`);if(window.dispatchEvent(new CustomEvent("toolactivated",{detail:{toolName:g}})),!(e.autoSubmit||!!c?.hasAttribute("toolautosubmit")||c instanceof HTMLElement&&c.dataset.webmcpAutosubmit!==void 0||o.hasAttribute("toolautosubmit")||o instanceof HTMLElement&&o.dataset.webmcpAutosubmit!==void 0))return console.log("[auto-webmcp] orphan execute: autoSubmit=false, returning without clicking submit"),{content:[{type:"text",text:"Fields filled. Ready to submit."}]};console.log("[auto-webmcp] orphan execute: polling for enabled submit button (up to 2s)...");let h=null,w=Date.now()+2e3;for(;Date.now()<w;){let T=Array.from(o.querySelectorAll(t)).filter(q=>{let te=q.getBoundingClientRect();return te.width>0&&te.height>0}),S=T[T.length-1]??null;if(S){h=S;break}await new Promise(q=>setTimeout(q,100))}return h?(console.log(`[auto-webmcp] orphan execute: clicking submit button "${h.textContent?.trim()}"`),h.click(),{content:[{type:"text",text:"Fields filled and form submitted."}]}):(console.warn("[auto-webmcp] orphan execute: submit button still disabled after 2s"),{content:[{type:"text",text:"Fields filled but the submit button is still disabled. The page may require additional input before submitting."}]})};try{let d={name:m.name,description:m.description,inputSchema:m.inputSchema,execute:y};m.annotations&&Object.keys(m.annotations).length>0&&(d.annotations=m.annotations),await navigator.modelContext.registerTool(d);let E=window.__pendingSubmitBtns??={};E[m.name]=c,e.debug&&console.log(`[auto-webmcp] Orphan tool registered: ${m.name}`,m)}catch{}}}function Ht(e,t){/^form_\d+$|^submit$|^form$/.test(e)&&console.warn(`[auto-webmcp] Tool "${e}" has a generic name. Consider adding a toolname or data-webmcp-name attribute.`),(!t||t==="Submit form")&&console.warn(`[auto-webmcp] Tool "${e}" has no meaningful description.`),/don'?t|do not|never|avoid|not for/i.test(t)&&console.warn(`[auto-webmcp] Tool "${e}" description contains negative instructions. Per spec best practices, prefer positive descriptions.`)}async function Fe(e){document.readyState==="loading"&&await new Promise(t=>document.addEventListener("DOMContentLoaded",()=>t(),{once:!0})),Ie=0,Tt(e),yt(e),await R(e),await Mt(e)}function $e(){I?.disconnect(),I=null}F();async function Oe(e){let t=re(e);return t.debug&&console.debug("[auto-webmcp] Initializing",{webmcpSupported:L(),config:t}),await Fe(t),{destroy:async()=>{$e(),await G()},getTools:V,isSupported:L()}}typeof window<"u"&&!window.__AUTO_WEBMCP_NO_AUTOINIT&&Oe();return De(xt);})();
|
|
2
2
|
//# sourceMappingURL=auto-webmcp.iife.js.map
|