"use strict";(self.webpackChunkdami=self.webpackChunkdami||[]).push([[7014],{27014:(e,t,a)=>{a.r(t),a.d(t,{default:()=>v,javascriptCode:()=>f});var r=a(59022),o=a(93150),n=a.n(o),s=a(83465),i=a.n(s);function c(e,{type:t,resource:a}){return new Promise(((r,o)=>{if(!t||!["text","json","base64"].includes(t))return void o(new Error('The "type" must be "text" or "json"'));const n=`__autom-fetch-response-${e}__`,s=({detail:t})=>{t.id===e&&(window.removeEventListener(n,s),t.isError?o(new Error(t.result)):r(t.result))};window.addEventListener(n,s),window.dispatchEvent(new CustomEvent("__automa-fetch__",{detail:{id:e,type:t,resource:a}}))}))}function u(e,t,a){return new Promise(((r,o)=>{try{let n=document;if(e.frameSelector){const u=document.querySelector(e.frameSelector)?.contentDocument;if(!u)return void o(new Error("iframe-not-found"));n=u}const s=`block--${e.id}`;if(n.querySelector(`.automa-custom-js[${s}]`))return void r("");const i=document.createElement("script");i.setAttribute(s,""),i.classList.add("automa-custom-js"),i.textContent=`(() => {\n        ${a}\n\n        try {\n          ${e.data.code}\n          ${e.data.everyNewTab||e.data.code.includes("automaNextBlock")?"":"automaNextBlock()"}\n        } catch (error) {\n          console.error(error);\n          ${e.data.everyNewTab?"":"automaNextBlock({ $error: true, message: error.message })"}\n        }\n      })()`;const c=t.map((e=>{const t=document.createElement("script");return t.id=e.id,t.textContent=e.script,n.head.appendChild(t),{element:t,removeAfterExec:e.removeAfterExec}}));if(e.data.everyNewTab)r();else{let d,m,l;function f(){i.remove(),c.forEach((e=>{e.removeAfterExec&&e.script.remove()})),clearTimeout(d),n.body.removeEventListener("__automa-reset-timeout__",l),n.body.removeEventListener("__automa-next-block__",m)}m=({detail:e})=>{f(),r({columns:{data:e?.data,insert:e?.insert},variables:e?.refData?.variables})},l=()=>{clearTimeout(d),d=setTimeout(f,e.data.timeout)},n.body.addEventListener("__automa-next-block__",m),n.body.addEventListener("__automa-reset-timeout__",l),d=setTimeout(f,e.data.timeout)}n.head.appendChild(i)}catch(v){console.error(v)}}))}var d=a(31791);const m=(0,r.k)("1234567890abcdef",5);function l(e,t,a){let r=`\nconst ${e} = ${JSON.stringify(t)};\n${(0,d.XB)(e)}\nfunction automaSetVariable(name, value) {\n  ${e}.variables[name] = value;\n}\nfunction automaNextBlock(data, insert = true) {\n  document.body.dispatchEvent(new CustomEvent('__automa-next-block__', { detail: { data, insert, refData: ${e} } }));\n}\nfunction automaResetTimeout() {\n document.body.dispatchEvent(new CustomEvent('__automa-reset-timeout__'));\n}\nfunction automaFetch(type, resource) {\n  return (${c.toString()})('${e}', { type, resource });\n}\n  `;return a&&(r=(0,d.XB)(e)),r}async function f({outputs:e,data:t,...a},{refData:r}){const o=this.getBlockConnections(a.id);if(t.everyNewTab){if(this.preloadScripts.some((({id:e})=>e===a.id))||this.preloadScripts.push({id:a.id,data:i()(t)}),!this.activeTab.id)return{data:"",nextBlockId:o}}else if(!this.activeTab.id&&"background"!==t.context)throw new Error("no-tab");const s={...a,data:t,refData:{variables:{}},frameSelector:this.frameSelector};t.code.includes("automaRefData")&&(s.refData={...r,secrets:{}});const c=(await Promise.allSettled(t.preloadScripts.map((async e=>{const{protocol:t}=new URL(e.src);if(!/https?/.test(t))return null;const a=await fetch(e.src);if(!a.ok)throw new Error(a.statusText);return{script:await a.text(),id:`automa-script-${m()}`,removeAfterExec:e.removeAfterExec}})))).reduce(((e,t)=>("fulfilled"===t.status&&e.push(t.value),e)),[]),f=`automa${m()}`,v=t.everyNewTab||"background"===t.context?"":l(f,s.refData,t.everyNewTab);"background"!==t.context&&await(0,d.Hh)({tabId:this.activeTab.id,ms:this.settings?.tabLoadTimeout??3e4});const b="background"===t.context,p=await(b?(0,d.k$)("javascriptBlock",{instanceId:f,preloadScripts:c,refData:s.refData,blockData:i()(s.data)}):async function(e,t,a){if(a.engine.isMV2)return e[0]=i()(e[0]),await a._sendMessageToTab({label:"javascript-code",data:e});const[{result:r}]=await n().scripting.executeScript({args:e,target:t,world:"MAIN",func:u});return r}([s,c,v,f],{tabId:this.activeTab.id,frameIds:[this.activeTab.frameId||0]},this));if(p){if(p.columns.data?.$error)throw new Error(p.columns.data.message);if(p.variables&&Object.keys(p.variables).forEach((e=>{this.setVariable(e,p.variables[e])})),p.columns.insert&&p.columns.data){const e=Array.isArray(p.columns.data)?p.columns.data:[p.columns.data];this.addDataToColumn(e)}}return{nextBlockId:o,data:p?.columns.data||{}}}const v=f}}]);