copilot-chat-widget 0.1.20 → 0.1.22
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/chat-widget.min.js +2 -2
- package/dist/index.cjs +3 -3
- package/dist/index.mjs +39 -39
- package/package.json +1 -1
- package/src/index.js +25 -29
- package/src/widget-runtime.js +41 -39
package/dist/chat-widget.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
(function(){"use strict";(()=>{if(typeof window>"u"||typeof document>"u"||window.__copilotWidgetLoaded)return;window.__copilotWidgetLoaded=!0;const S="http://localhost:3000",b=64,f=720,x=document.currentScript,k=e=>{document.readyState==="complete"||document.readyState==="interactive"?setTimeout(e,0):document.addEventListener("DOMContentLoaded",e)},y=()=>x||Array.from(document.querySelectorAll("script")).reverse().find(o=>o.
|
|
1
|
+
(function(){"use strict";(()=>{if(typeof window>"u"||typeof document>"u"||window.__copilotWidgetLoaded)return;window.__copilotWidgetLoaded=!0;const S="http://localhost:3000",b=64,f=720,x=document.currentScript,k=e=>{document.readyState==="complete"||document.readyState==="interactive"?setTimeout(e,0):document.addEventListener("DOMContentLoaded",e)},y=()=>x||Array.from(document.querySelectorAll("script")).reverse().find(o=>o.src&&o.src.includes("chat-widget"))||null,T=({iframeUrl:e,launcherIcon:p})=>{const o=document.querySelector("[data-copilot-widget-root]");o&&o.remove();const c=d=>{const i=document.querySelector("[data-copilot-checkout-toast]");i&&i.remove();const a=document.createElement("div");a.setAttribute("data-copilot-checkout-toast","true"),a.innerText=d,Object.assign(a.style,{position:"fixed",top:"20px",left:"50%",transform:"translateX(-50%)",padding:"12px 16px",background:"#0f172a",color:"white",borderRadius:"12px",boxShadow:"0 8px 24px rgba(0,0,0,0.25)",zIndex:2147483647,fontSize:"14px",fontWeight:"600",maxWidth:"420px",lineHeight:"1.4",textAlign:"center"}),document.body.appendChild(a),setTimeout(()=>{a.remove()},4500)},m=document.createElement("div");m.setAttribute("data-copilot-widget-root","true");const h=m.attachShadow({mode:"open"});document.body.appendChild(m);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
|
|
2
2
|
<img
|
|
3
3
|
src="${p}"
|
|
4
4
|
alt="Copilot chat launcher"
|
|
5
5
|
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
6
6
|
/>
|
|
7
|
-
`,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${b}px`,height:`${b}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const n=document.createElement("div");n.setAttribute("data-copilot-widget-root","true"),Object.assign(n.style,{display:"none",position:"fixed",bottom:`${b+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const l=document.createElement("div"),E=document.createElement("div"),v=document.createElement("div");Object.assign(l.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(E.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(v.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),l.appendChild(E),l.appendChild(v);const u=document.createElement("div");Object.assign(u.style,{width:`${f}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(f,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),h.appendChild(u);const r=document.createElement("iframe");r.src=e,r.title="Copilot chat widget",r.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",r.setAttribute("scrolling","no"),Object.assign(r.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),h.appendChild(u),u.appendChild(r),n.appendChild(l),n.appendChild(u),document.body.appendChild(t),document.body.appendChild(n);let s=!1;const w=()=>{s&&(s=!1,n.style.opacity="0",n.style.transform="scale(0.8) translateY(20px)",l.style.display="none",setTimeout(()=>{n.style.display="none"},250),t.style.transform="scale(1)")},O=d=>{const{data:i,source:a}=d||{};if(i?.type){if(i.type==="CART_CHECKOUT"&&a===r.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",i),c("Checkout message received from chat widget. Check console for payload.");return}if(i.type==="WIDGET_READY"&&a===r.contentWindow){r.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}i.type==="CHAT_CLOSED"&&w()}};t.onclick=d=>{d.stopPropagation(),s=!s,s?(n.style.display="block",l.style.display="block",requestAnimationFrame(()=>{n.style.opacity="1",n.style.transform="scale(1) translateY(0)"})):w()},window.addEventListener("message",O),document.addEventListener("click",d=>{const i=d.target;i&&s&&!n.contains(i)&&i!==t&&w()});const g={close:w,open:()=>{s||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g},
|
|
7
|
+
`,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${b}px`,height:`${b}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const n=document.createElement("div");n.setAttribute("data-copilot-widget-root","true"),Object.assign(n.style,{display:"none",position:"fixed",bottom:`${b+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const l=document.createElement("div"),E=document.createElement("div"),v=document.createElement("div");Object.assign(l.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(E.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(v.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),l.appendChild(E),l.appendChild(v);const u=document.createElement("div");Object.assign(u.style,{width:`${f}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(f,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),h.appendChild(u);const r=document.createElement("iframe");r.src=e,r.title="Copilot chat widget",r.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",r.setAttribute("scrolling","no"),Object.assign(r.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),h.appendChild(u),u.appendChild(r),n.appendChild(l),n.appendChild(u),document.body.appendChild(t),document.body.appendChild(n);let s=!1;const w=()=>{s&&(s=!1,n.style.opacity="0",n.style.transform="scale(0.8) translateY(20px)",l.style.display="none",setTimeout(()=>{n.style.display="none"},250),t.style.transform="scale(1)")},O=d=>{const{data:i,source:a}=d||{};if(i?.type){if(i.type==="CART_CHECKOUT"&&a===r.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",i),c("Checkout message received from chat widget. Check console for payload.");return}if(i.type==="WIDGET_READY"&&a===r.contentWindow){r.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}i.type==="CHAT_CLOSED"&&w()}};t.onclick=d=>{d.stopPropagation(),s=!s,s?(n.style.display="block",l.style.display="block",requestAnimationFrame(()=>{n.style.opacity="1",n.style.transform="scale(1) translateY(0)"}),r.contentWindow&&r.contentWindow.postMessage({type:"CHAT_OPENED"},"*")):w()},window.addEventListener("message",O),document.addEventListener("click",d=>{const i=d.target;i&&s&&!n.contains(i)&&i!==t&&w()});const g={close:w,open:()=>{s||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=g.close,window.CopilotChat.open=g.open,window.CopilotChat.controls=g,g},I=e=>{console.error(`[CopilotChat] ${e}`)},A=()=>{const e=y();return e?(e.dataset||{}).autoload!=="false":!1},C=async(e={})=>{e.scriptEl||y();const p=S;try{const o=await fetch(`${p}/api/chat-widget/config`,{credentials:"omit",mode:"cors"});if(!o.ok)throw new Error(`Server responded with ${o.status}`);const c=await o.json();if(!c?.iframeUrl||!c?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.init=(h={})=>{const t={iframeUrl:h.iframeUrl||c.iframeUrl,launcherIcon:h.launcherIcon||c.launcherIcon};return T(t)},window.CopilotChat.init(e)}catch(o){return I(o instanceof Error?o.message:"Unknown error during widget bootstrap."),null}};A()&&k(()=>{C()}),window.CopilotChat=window.CopilotChat||{},window.CopilotChat.load=C})()})();
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const v="http://localhost:3000",g=64,y=720,T=({iframeUrl:m,launcherIcon:b})=>{const p=document.querySelector("[data-copilot-widget-root]");p&&p.remove();const i=d=>{const o=document.querySelector("[data-copilot-checkout-toast]");o&&o.remove();const a=document.createElement("div");a.setAttribute("data-copilot-checkout-toast","true"),a.innerText=d,Object.assign(a.style,{position:"fixed",top:"20px",left:"50%",transform:"translateX(-50%)",padding:"12px 16px",background:"#0f172a",color:"white",borderRadius:"12px",boxShadow:"0 8px 24px rgba(0,0,0,0.25)",zIndex:2147483647,fontSize:"14px",fontWeight:"600",maxWidth:"420px",lineHeight:"1.4",textAlign:"center"}),document.body.appendChild(a),setTimeout(()=>{a.remove()},4500)},r=document.createElement("div");r.setAttribute("data-copilot-widget-root","true");const x=r.attachShadow({mode:"open"});document.body.appendChild(r);const t=document.createElement("button");t.type="button",t.setAttribute("aria-label","Open Copilot chat"),t.innerHTML=`
|
|
2
2
|
<img
|
|
3
|
-
src="${
|
|
3
|
+
src="${b}"
|
|
4
4
|
alt="Copilot chat launcher"
|
|
5
5
|
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
6
6
|
/>
|
|
7
|
-
`,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${
|
|
7
|
+
`,Object.assign(t.style,{position:"fixed",bottom:"24px",right:"24px",width:`${g}px`,height:`${g}px`,borderRadius:"50%",border:"none",background:"linear-gradient(135deg, #0078ff, #00c6ff)",color:"white",cursor:"pointer",zIndex:999998,display:"flex",alignItems:"center",justifyContent:"center",boxShadow:"0 6px 14px rgba(0,0,0,0.25)",transition:"all 0.25s ease"}),t.onmouseover=()=>{t.style.transform="scale(1.12)",t.style.boxShadow="0 10px 25px rgba(0,0,0,0.3)"},t.onmouseout=()=>{t.style.transform="scale(1)",t.style.boxShadow="0 6px 14px rgba(0,0,0,0.25)"};const e=document.createElement("div");e.setAttribute("data-copilot-widget-root","true"),Object.assign(e.style,{display:"none",position:"fixed",bottom:`${g+36}px`,right:"24px",zIndex:"999999",transformOrigin:"bottom right",transform:"scale(0.8) translateY(20px)",opacity:"0",transition:"all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"});const l=document.createElement("div"),f=document.createElement("div"),w=document.createElement("div");Object.assign(l.style,{position:"absolute",bottom:"-14px",right:"28px",width:"30px",height:"20px",pointerEvents:"none",display:"none",zIndex:"1"}),Object.assign(f.style,{position:"absolute",bottom:"0",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"15px solid transparent",borderRight:"15px solid transparent",borderTop:"15px solid rgba(15,23,42,0.1)"}),Object.assign(w.style,{position:"absolute",bottom:"2px",left:"0",right:"0",margin:"0 auto",width:"0",height:"0",borderLeft:"13px solid transparent",borderRight:"13px solid transparent",borderTop:"13px solid white",boxShadow:"0 6px 16px rgba(15,23,42,0.12)",borderRadius:"2px"}),l.appendChild(f),l.appendChild(w);const c=document.createElement("div");Object.assign(c.style,{width:`${y}px`,maxWidth:"calc(100vw - 48px)",height:`${Math.min(y,Math.max(320,window.innerHeight-140))}px`,maxHeight:"calc(100vh - 150px)",borderRadius:"20px",background:"white",border:"1px solid rgba(15,23,42,0.12)",boxShadow:"0 18px 45px rgba(15,23,42,0.16)",overflow:"hidden"}),x.appendChild(c);const n=document.createElement("iframe");n.src=m,n.title="Copilot chat widget",n.allow="clipboard-read; clipboard-write; microphone; camera; display-capture",n.setAttribute("scrolling","no"),Object.assign(n.style,{width:"100%",height:"100%",border:"none",display:"block",background:"transparent",overflow:"hidden"}),x.appendChild(c),c.appendChild(n),e.appendChild(l),e.appendChild(c),document.body.appendChild(t),document.body.appendChild(e);let s=!1;const h=()=>{s&&(s=!1,e.style.opacity="0",e.style.transform="scale(0.8) translateY(20px)",l.style.display="none",setTimeout(()=>{e.style.display="none"},250),t.style.transform="scale(1)")},E=d=>{const{data:o,source:a}=d||{};if(o?.type){if(o.type==="CART_CHECKOUT"&&a===n.contentWindow){console.log("[CopilotChat] Received checkout payload from widget:",o),i("Checkout message received from chat widget. Check console for payload.");return}if(o.type==="WIDGET_READY"&&a===n.contentWindow){n.contentWindow.postMessage({type:"INIT_WIDGET"},"*");return}o.type==="CHAT_CLOSED"&&h()}};window.addEventListener("message",E),t.onclick=d=>{d.stopPropagation(),s=!s,s?(e.style.display="block",l.style.display="block",requestAnimationFrame(()=>{e.style.opacity="1",e.style.transform="scale(1) translateY(0)"}),n.contentWindow&&n.contentWindow.postMessage({type:"CHAT_OPENED"},"*")):h()},document.addEventListener("click",d=>{const o=d.target;o&&s&&!e.contains(o)&&o!==t&&h()});const u={close:h,open:()=>{s||t.click()}};return window.CopilotChat=window.CopilotChat||{},window.CopilotChat.close=u.close,window.CopilotChat.open=u.open,window.CopilotChat.controls=u,u};async function C(m={}){if(typeof window>"u"||typeof document>"u")return null;const p=`${v.replace(/\/$/,"")}/api/chat-widget/config`;try{const i=await fetch(p,{credentials:"omit",mode:"cors"});if(!i.ok)throw new Error(`Server responded with ${i.status}`);const r=await i.json();if(!r?.iframeUrl||!r?.launcherIcon)throw new Error("Received incomplete widget configuration from server.");return T({iframeUrl:r.iframeUrl,launcherIcon:r.launcherIcon})}catch(i){return console.error("[CopilotChat] Error during widget bootstrap:",i instanceof Error?i.message:i),null}}exports.default=C;exports.loadCopilotChatWidget=C;
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const y = "http://localhost:3000";
|
|
2
|
-
const C = ({ iframeUrl: g, launcherIcon:
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
const
|
|
2
|
+
const C = ({ iframeUrl: g, launcherIcon: m }) => {
|
|
3
|
+
const p = document.querySelector("[data-copilot-widget-root]");
|
|
4
|
+
p && p.remove();
|
|
5
|
+
const i = (d) => {
|
|
6
6
|
const o = document.querySelector("[data-copilot-checkout-toast]");
|
|
7
7
|
o && o.remove();
|
|
8
8
|
const a = document.createElement("div");
|
|
@@ -32,7 +32,7 @@ const C = ({ iframeUrl: g, launcherIcon: p }) => {
|
|
|
32
32
|
const t = document.createElement("button");
|
|
33
33
|
t.type = "button", t.setAttribute("aria-label", "Open Copilot chat"), t.innerHTML = `
|
|
34
34
|
<img
|
|
35
|
-
src="${
|
|
35
|
+
src="${m}"
|
|
36
36
|
alt="Copilot chat launcher"
|
|
37
37
|
style="width: 38px; height: 38px; object-fit: contain; border-radius: 50%; pointer-events: none;"
|
|
38
38
|
/>
|
|
@@ -70,8 +70,8 @@ const C = ({ iframeUrl: g, launcherIcon: p }) => {
|
|
|
70
70
|
opacity: "0",
|
|
71
71
|
transition: "all 0.3s cubic-bezier(0.68, -0.55, 0.265, 1.55)"
|
|
72
72
|
});
|
|
73
|
-
const
|
|
74
|
-
Object.assign(
|
|
73
|
+
const c = document.createElement("div"), x = document.createElement("div"), f = document.createElement("div");
|
|
74
|
+
Object.assign(c.style, {
|
|
75
75
|
position: "absolute",
|
|
76
76
|
bottom: "-14px",
|
|
77
77
|
right: "28px",
|
|
@@ -80,7 +80,7 @@ const C = ({ iframeUrl: g, launcherIcon: p }) => {
|
|
|
80
80
|
pointerEvents: "none",
|
|
81
81
|
display: "none",
|
|
82
82
|
zIndex: "1"
|
|
83
|
-
}), Object.assign(
|
|
83
|
+
}), Object.assign(x.style, {
|
|
84
84
|
position: "absolute",
|
|
85
85
|
bottom: "0",
|
|
86
86
|
left: "0",
|
|
@@ -91,7 +91,7 @@ const C = ({ iframeUrl: g, launcherIcon: p }) => {
|
|
|
91
91
|
borderLeft: "15px solid transparent",
|
|
92
92
|
borderRight: "15px solid transparent",
|
|
93
93
|
borderTop: "15px solid rgba(15,23,42,0.1)"
|
|
94
|
-
}), Object.assign(
|
|
94
|
+
}), Object.assign(f.style, {
|
|
95
95
|
position: "absolute",
|
|
96
96
|
bottom: "2px",
|
|
97
97
|
left: "0",
|
|
@@ -104,9 +104,9 @@ const C = ({ iframeUrl: g, launcherIcon: p }) => {
|
|
|
104
104
|
borderTop: "13px solid white",
|
|
105
105
|
boxShadow: "0 6px 16px rgba(15,23,42,0.12)",
|
|
106
106
|
borderRadius: "2px"
|
|
107
|
-
}),
|
|
108
|
-
const
|
|
109
|
-
Object.assign(
|
|
107
|
+
}), c.appendChild(x), c.appendChild(f);
|
|
108
|
+
const l = document.createElement("div");
|
|
109
|
+
Object.assign(l.style, {
|
|
110
110
|
width: "720px",
|
|
111
111
|
maxWidth: "calc(100vw - 48px)",
|
|
112
112
|
height: `${Math.min(720, Math.max(320, window.innerHeight - 140))}px`,
|
|
@@ -116,73 +116,73 @@ const C = ({ iframeUrl: g, launcherIcon: p }) => {
|
|
|
116
116
|
border: "1px solid rgba(15,23,42,0.12)",
|
|
117
117
|
boxShadow: "0 18px 45px rgba(15,23,42,0.16)",
|
|
118
118
|
overflow: "hidden"
|
|
119
|
-
}), b.appendChild(
|
|
120
|
-
const
|
|
121
|
-
|
|
119
|
+
}), b.appendChild(l);
|
|
120
|
+
const n = document.createElement("iframe");
|
|
121
|
+
n.src = g, n.title = "Copilot chat widget", n.allow = "clipboard-read; clipboard-write; microphone; camera; display-capture", n.setAttribute("scrolling", "no"), Object.assign(n.style, {
|
|
122
122
|
width: "100%",
|
|
123
123
|
height: "100%",
|
|
124
124
|
border: "none",
|
|
125
125
|
display: "block",
|
|
126
126
|
background: "transparent",
|
|
127
127
|
overflow: "hidden"
|
|
128
|
-
}), b.appendChild(
|
|
128
|
+
}), b.appendChild(l), l.appendChild(n), e.appendChild(c), e.appendChild(l), document.body.appendChild(t), document.body.appendChild(e);
|
|
129
129
|
let s = !1;
|
|
130
|
-
const
|
|
131
|
-
s && (s = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)",
|
|
130
|
+
const h = () => {
|
|
131
|
+
s && (s = !1, e.style.opacity = "0", e.style.transform = "scale(0.8) translateY(20px)", c.style.display = "none", setTimeout(() => {
|
|
132
132
|
e.style.display = "none";
|
|
133
133
|
}, 250), t.style.transform = "scale(1)");
|
|
134
|
-
},
|
|
134
|
+
}, w = (d) => {
|
|
135
135
|
const { data: o, source: a } = d || {};
|
|
136
136
|
if (o?.type) {
|
|
137
|
-
if (o.type === "CART_CHECKOUT" && a ===
|
|
138
|
-
console.log("[CopilotChat] Received checkout payload from widget:", o),
|
|
137
|
+
if (o.type === "CART_CHECKOUT" && a === n.contentWindow) {
|
|
138
|
+
console.log("[CopilotChat] Received checkout payload from widget:", o), i("Checkout message received from chat widget. Check console for payload.");
|
|
139
139
|
return;
|
|
140
140
|
}
|
|
141
|
-
if (o.type === "WIDGET_READY" && a ===
|
|
142
|
-
|
|
141
|
+
if (o.type === "WIDGET_READY" && a === n.contentWindow) {
|
|
142
|
+
n.contentWindow.postMessage({ type: "INIT_WIDGET" }, "*");
|
|
143
143
|
return;
|
|
144
144
|
}
|
|
145
|
-
o.type === "CHAT_CLOSED" &&
|
|
145
|
+
o.type === "CHAT_CLOSED" && h();
|
|
146
146
|
}
|
|
147
147
|
};
|
|
148
|
-
window.addEventListener("message",
|
|
149
|
-
d.stopPropagation(), s = !s, s ? (e.style.display = "block",
|
|
148
|
+
window.addEventListener("message", w), t.onclick = (d) => {
|
|
149
|
+
d.stopPropagation(), s = !s, s ? (e.style.display = "block", c.style.display = "block", requestAnimationFrame(() => {
|
|
150
150
|
e.style.opacity = "1", e.style.transform = "scale(1) translateY(0)";
|
|
151
|
-
})) :
|
|
151
|
+
}), n.contentWindow && n.contentWindow.postMessage({ type: "CHAT_OPENED" }, "*")) : h();
|
|
152
152
|
}, document.addEventListener("click", (d) => {
|
|
153
153
|
const o = d.target;
|
|
154
|
-
o && s && !e.contains(o) && o !== t &&
|
|
154
|
+
o && s && !e.contains(o) && o !== t && h();
|
|
155
155
|
});
|
|
156
|
-
const
|
|
157
|
-
close:
|
|
156
|
+
const u = {
|
|
157
|
+
close: h,
|
|
158
158
|
open: () => {
|
|
159
159
|
s || t.click();
|
|
160
160
|
}
|
|
161
161
|
};
|
|
162
|
-
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close =
|
|
162
|
+
return window.CopilotChat = window.CopilotChat || {}, window.CopilotChat.close = u.close, window.CopilotChat.open = u.open, window.CopilotChat.controls = u, u;
|
|
163
163
|
};
|
|
164
164
|
async function E(g = {}) {
|
|
165
165
|
if (typeof window > "u" || typeof document > "u")
|
|
166
166
|
return null;
|
|
167
|
-
const p =
|
|
167
|
+
const p = `${y.replace(/\/$/, "")}/api/chat-widget/config`;
|
|
168
168
|
try {
|
|
169
|
-
const
|
|
169
|
+
const i = await fetch(p, {
|
|
170
170
|
credentials: "omit",
|
|
171
171
|
mode: "cors"
|
|
172
172
|
});
|
|
173
|
-
if (!
|
|
174
|
-
throw new Error(`Server responded with ${
|
|
175
|
-
const r = await
|
|
173
|
+
if (!i.ok)
|
|
174
|
+
throw new Error(`Server responded with ${i.status}`);
|
|
175
|
+
const r = await i.json();
|
|
176
176
|
if (!r?.iframeUrl || !r?.launcherIcon)
|
|
177
177
|
throw new Error("Received incomplete widget configuration from server.");
|
|
178
|
-
return
|
|
178
|
+
return C({
|
|
179
179
|
iframeUrl: r.iframeUrl,
|
|
180
180
|
launcherIcon: r.launcherIcon
|
|
181
181
|
});
|
|
182
|
-
} catch (
|
|
182
|
+
} catch (i) {
|
|
183
183
|
return console.error(
|
|
184
184
|
"[CopilotChat] Error during widget bootstrap:",
|
|
185
|
-
|
|
185
|
+
i instanceof Error ? i.message : i
|
|
186
186
|
), null;
|
|
187
187
|
}
|
|
188
188
|
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -225,20 +225,23 @@ const buildWidget = ({ iframeUrl, launcherIcon }) => {
|
|
|
225
225
|
|
|
226
226
|
window.addEventListener("message", handleWidgetMessage);
|
|
227
227
|
|
|
228
|
-
btn.onclick = (event) => {
|
|
229
|
-
event.stopPropagation();
|
|
230
|
-
isOpen = !isOpen;
|
|
231
|
-
if (isOpen) {
|
|
232
|
-
container.style.display = "block";
|
|
233
|
-
arrow.style.display = "block";
|
|
234
|
-
requestAnimationFrame(() => {
|
|
235
|
-
container.style.opacity = "1";
|
|
236
|
-
container.style.transform = "scale(1) translateY(0)";
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
228
|
+
btn.onclick = (event) => {
|
|
229
|
+
event.stopPropagation();
|
|
230
|
+
isOpen = !isOpen;
|
|
231
|
+
if (isOpen) {
|
|
232
|
+
container.style.display = "block";
|
|
233
|
+
arrow.style.display = "block";
|
|
234
|
+
requestAnimationFrame(() => {
|
|
235
|
+
container.style.opacity = "1";
|
|
236
|
+
container.style.transform = "scale(1) translateY(0)";
|
|
237
|
+
});
|
|
238
|
+
if (iframe.contentWindow) {
|
|
239
|
+
iframe.contentWindow.postMessage({ type: "CHAT_OPENED" }, "*");
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
closeChat();
|
|
243
|
+
}
|
|
244
|
+
};
|
|
242
245
|
|
|
243
246
|
document.addEventListener("click", (event) => {
|
|
244
247
|
const target = event.target;
|
|
@@ -270,19 +273,15 @@ export async function loadCopilotChatWidget(options = {}) {
|
|
|
270
273
|
return null;
|
|
271
274
|
}
|
|
272
275
|
|
|
273
|
-
const globalConfig = window.CopilotChatConfig || {};
|
|
274
|
-
|
|
275
276
|
const baseUrl =
|
|
276
277
|
BUILD_BASE_URL ||
|
|
277
278
|
(typeof window !== "undefined" && window.location?.origin) ||
|
|
278
279
|
null;
|
|
279
|
-
|
|
280
|
-
if (!baseUrl) {
|
|
281
|
-
console.error(
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
return null;
|
|
285
|
-
}
|
|
280
|
+
|
|
281
|
+
if (!baseUrl) {
|
|
282
|
+
console.error("[CopilotChat] Unable to resolve widget base URL.");
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
286
285
|
|
|
287
286
|
const url = `${baseUrl.replace(/\/$/, "")}/api/chat-widget/config`;
|
|
288
287
|
|
|
@@ -301,12 +300,9 @@ export async function loadCopilotChatWidget(options = {}) {
|
|
|
301
300
|
throw new Error("Received incomplete widget configuration from server.");
|
|
302
301
|
}
|
|
303
302
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return buildWidget({
|
|
308
|
-
iframeUrl: remoteConfig.iframeUrl,
|
|
309
|
-
launcherIcon: remoteConfig.launcherIcon,
|
|
303
|
+
return buildWidget({
|
|
304
|
+
iframeUrl: remoteConfig.iframeUrl,
|
|
305
|
+
launcherIcon: remoteConfig.launcherIcon,
|
|
310
306
|
});
|
|
311
307
|
} catch (error) {
|
|
312
308
|
console.error(
|
package/src/widget-runtime.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
const IFRAME_SIZE = 720;
|
|
12
12
|
const embeddingScript = document.currentScript;
|
|
13
13
|
|
|
14
|
-
const ready = (fn) => {
|
|
14
|
+
const ready = (fn) => {
|
|
15
15
|
if (document.readyState === "complete" || document.readyState === "interactive") {
|
|
16
16
|
setTimeout(fn, 0);
|
|
17
17
|
} else {
|
|
@@ -30,15 +30,15 @@
|
|
|
30
30
|
}
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
const resolveEmbeddingScript = () => {
|
|
34
|
-
if (embeddingScript) {
|
|
35
|
-
return embeddingScript;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const scripts = Array.from(document.querySelectorAll("script"));
|
|
39
|
-
const candidate = scripts
|
|
40
|
-
.reverse()
|
|
41
|
-
.find((script) => script.
|
|
33
|
+
const resolveEmbeddingScript = () => {
|
|
34
|
+
if (embeddingScript) {
|
|
35
|
+
return embeddingScript;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const scripts = Array.from(document.querySelectorAll("script"));
|
|
39
|
+
const candidate = scripts
|
|
40
|
+
.reverse()
|
|
41
|
+
.find((script) => script.src && script.src.includes("chat-widget"));
|
|
42
42
|
|
|
43
43
|
return candidate || null;
|
|
44
44
|
};
|
|
@@ -261,20 +261,23 @@
|
|
|
261
261
|
}
|
|
262
262
|
};
|
|
263
263
|
|
|
264
|
-
btn.onclick = (event) => {
|
|
265
|
-
event.stopPropagation();
|
|
266
|
-
isOpen = !isOpen;
|
|
267
|
-
if (isOpen) {
|
|
268
|
-
container.style.display = "block";
|
|
269
|
-
arrow.style.display = "block";
|
|
270
|
-
requestAnimationFrame(() => {
|
|
271
|
-
container.style.opacity = "1";
|
|
272
|
-
container.style.transform = "scale(1) translateY(0)";
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
264
|
+
btn.onclick = (event) => {
|
|
265
|
+
event.stopPropagation();
|
|
266
|
+
isOpen = !isOpen;
|
|
267
|
+
if (isOpen) {
|
|
268
|
+
container.style.display = "block";
|
|
269
|
+
arrow.style.display = "block";
|
|
270
|
+
requestAnimationFrame(() => {
|
|
271
|
+
container.style.opacity = "1";
|
|
272
|
+
container.style.transform = "scale(1) translateY(0)";
|
|
273
|
+
});
|
|
274
|
+
if (iframe.contentWindow) {
|
|
275
|
+
iframe.contentWindow.postMessage({ type: "CHAT_OPENED" }, "*");
|
|
276
|
+
}
|
|
277
|
+
} else {
|
|
278
|
+
closeChat();
|
|
279
|
+
}
|
|
280
|
+
};
|
|
278
281
|
|
|
279
282
|
window.addEventListener("message", handleWidgetMessage);
|
|
280
283
|
|
|
@@ -307,33 +310,32 @@
|
|
|
307
310
|
console.error(`[CopilotChat] ${message}`);
|
|
308
311
|
};
|
|
309
312
|
|
|
310
|
-
const shouldAutoBootstrapFromScript = () => {
|
|
311
|
-
const scriptEl = resolveEmbeddingScript();
|
|
312
|
-
if (!scriptEl) return false;
|
|
313
|
-
const dataset = scriptEl.dataset || {};
|
|
314
|
-
return dataset.autoload !== "false";
|
|
315
|
-
};
|
|
316
|
-
|
|
313
|
+
const shouldAutoBootstrapFromScript = () => {
|
|
314
|
+
const scriptEl = resolveEmbeddingScript();
|
|
315
|
+
if (!scriptEl) return false;
|
|
316
|
+
const dataset = scriptEl.dataset || {};
|
|
317
|
+
return dataset.autoload !== "false";
|
|
318
|
+
};
|
|
319
|
+
|
|
317
320
|
const initCopilotChatWidget = async (config = {}) => {
|
|
318
321
|
const scriptEl = config.scriptEl || resolveEmbeddingScript();
|
|
319
|
-
const globalConfig = window.CopilotChatConfig || {};
|
|
320
322
|
|
|
321
323
|
const baseUrl =
|
|
322
324
|
BUILD_BASE_URL ||
|
|
323
325
|
inferBaseUrlFromScript(scriptEl) ||
|
|
324
|
-
(typeof window !== "undefined" && window.location?.origin) ||
|
|
325
|
-
null;
|
|
326
|
+
(typeof window !== "undefined" && window.location?.origin) ||
|
|
327
|
+
null;
|
|
326
328
|
|
|
327
329
|
if (!baseUrl) {
|
|
328
330
|
handleError("Unable to resolve base URL from embedding script or window.location.");
|
|
329
331
|
return null;
|
|
330
332
|
}
|
|
331
333
|
|
|
332
|
-
try {
|
|
333
|
-
const response = await fetch(`${baseUrl}/api/chat-widget/config`, {
|
|
334
|
-
credentials: "omit",
|
|
335
|
-
mode: "cors",
|
|
336
|
-
});
|
|
334
|
+
try {
|
|
335
|
+
const response = await fetch(`${baseUrl}/api/chat-widget/config`, {
|
|
336
|
+
credentials: "omit",
|
|
337
|
+
mode: "cors",
|
|
338
|
+
});
|
|
337
339
|
|
|
338
340
|
if (!response.ok) {
|
|
339
341
|
throw new Error(`Server responded with ${response.status}`);
|