JavaScript Tag
JavaScript Tag

Install

Insert the following tag in the <head> of your HTML file before your main script:

1
2
3
<script>
   (()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{factory:()=>f,initChallenges:()=>w,runChallenge:()=>v,setUserId:()=>g});const r=e=>new Promise((t=>setTimeout(t,e))),n=()=>{};var o=(e,t,r)=>new Promise(((n,o)=>{var i=e=>{try{s(r.next(e))}catch(e){o(e)}},l=e=>{try{s(r.throw(e))}catch(e){o(e)}},s=e=>e.done?n(e.value):Promise.resolve(e.value).then(i,l);s((r=r.apply(e,t)).next())}));const i="__prt__",l=i+"preload__runners_controller",s=()=>String(Math.random()).replace("0.","");function a(e,t,r){return o(this,null,(function*(){let n=null;const o=new Promise((e=>{n=setTimeout((()=>{setTimeout((()=>{e(null)}),100)}),t)})),i=yield Promise.race([e,o]);if(n&&clearTimeout(n),!i)throw new Error(r);return i}))}const c=function(e){return new Promise((function(t,r){let n=0;e.forEach((o=>{o.then(t).catch((function(t){++n===e.length&&r(t)}))}))}))};function u(){let e=n,t=n;return{promise:new Promise(((r,n)=>{e=r,t=n})),resolve:e,reject:t}}const d=()=>{let e=u(),t=e.resolve,r=e.reject;const n=()=>{t=null,r=null},o={current:e.promise,busy:()=>{t||(e=u(),o.current=e.promise,t=e.resolve,r=e.reject)},ready:e=>{t&&t(e),n()},failed:()=>{r&&r(),n()}};return o},h=e=>({then:t=>{if(t){const r=t(e);return r&&"function"==typeof r.then?r:h(r)}return h(e)}}),p=(()=>{const e={};return t=>{if(e[t])return e[t];const r=function(e){const t=s(),r=i+t;window[r]={};const n=document.createElement("script");n.src=e,n.dataset.id=t,n.async=!0,document.head.append(n);const o=()=>{n.remove(),delete window[r]};return new Promise(((e,t)=>{n.onload=()=>{if(!window[r].r||"function"!=typeof window[r].r)return t(new Error("Not valid script loaded")),void o();e(window[r].r),o()},n.onerror=()=>{o(),t(new Error("Script load error"))}}))}(t);return e[t]=r,r.catch((()=>{delete e[t]})),r}})(),y=e=>{const t=Date.now(),i={api:d(),load:d(),init:d()},s=e=>()=>i[e].current;let u=s("api"),y=s("init");const m=s("load");let f=()=>m().then(u);const w=l=>{(e=>{const t=document.createElement("link");t.rel="preload",t.as="script",t.href=e,document.head.appendChild(t)})(l.url+"/challenges/v2/"+l.siteKey),function(e,t,n,i){return o(this,null,(function*(){const n=[];for(let o=0;o<t;o++)try{return n.push(e(o>0)),yield a(c(n),3e3,i)}catch(e){if(o===t-1)throw e;yield r(100)}throw new Error(i+" ("+t+" attempts)")}))}((r=>{let n=0;return()=>{return o=function*(){++n;const o=Date.now();return(yield p((e||r.url)+"/client/v1.js")).bind(null,{s:o,r:n,i:t})},new Promise(((e,t)=>{var r=e=>{try{i(o.next(e))}catch(e){t(e)}},n=e=>{try{i(o.throw(e))}catch(e){t(e)}},i=t=>t.done?e(t.value):Promise.resolve(t.value).then(r,n);i((o=o.apply(void 0,null)).next())}));var o}})(l),l.reload?1:3,0,"Timeout").then((e=>t=>{e.reload&&(i.init.busy(),i.api.busy());const r=t();return f=()=>h(r),u=()=>h(r),i.api.ready(r),i.load.ready(),!0})(l)).catch((e=>t=>{const r=t instanceof Error?t.message:"UNKNOWN";i.api.ready((e=>{const t=()=>Promise.resolve(e);return{setUserId:n,runCaptcha:()=>({dispose:n,promise:Promise.reject()}),runChallenge:t,initChallenges:()=>Promise.resolve(t)}})("[Error] Main script load error "+r)),i.init.ready(),setTimeout((()=>{e.reload=!0,v(e)}),5e3)})(l))},v=e=>{const r=window[l];if(r&&"function"==typeof r.r){const e=r.r({s:Date.now(),r:0,i:t});return f=()=>h(e),y=()=>h(void 0),u=()=>h(e),i.api.ready(e),i.init.ready(),void i.load.ready()}w(e)},g=(()=>{let e=!0;return(...t)=>{e&&(e=!1,(e=>{v({url:e.url,reload:!1,siteKey:e.siteKey}),(e=>{f().then((t=>t.initChallenges(e))).then((()=>{y=()=>h(void 0),i.init.ready()}))})(e)})(...t))}})(),C=e=>y().then(u).then((t=>t.runChallenge(e)));return{initChallenges:e=>(g(e),y().then((()=>C))),runChallenge:C,runCaptcha:e=>{const t=f().then((({runCaptcha:t})=>t(e)));return{dispose:()=>{t.then((e=>e.dispose()))},promise:t.then((e=>e.promise))}},setUserId:e=>{f().then((t=>t.setUserId(e)))}}},m="https://verify.botbye.com",f=e=>{const{initChallenges:t,runChallenge:r,setUserId:n}=y(e||m);return{initChallenges:r=>t(Object.assign({url:r.api||e||m,siteKey:r.clientKey,ver:"1.2.0"},r)),runChallenge:r,setUserId:n}},{initChallenges:w,runChallenge:v,setUserId:g}=f(m);window.botbye=t})();
</script>

Configuration

Init BotBye on your script with your project client-key:

1
2
3
4
window.botbye.initChallenges({
  // Use your client-key
  clientKey: "00000000-0000-0000-0000-000000000000"
});

Usage

To run challenge and generate BotBye token call runChallenge:

1
2
3
4
/**
 * @return {Promise} Promise with BotBye token
 */
const botByeToken = await window.botbye.runChallenge();

2. Send this token in any convenient way to the backend. For example in x-botbye-token header:

1
2
3
4
5
6
7
8
9
fetch(
  'https://domain.com',
  {
    method: "POST",
    headers: {
        "x-botbye-token": botByeToken
    }
  }
)