@playpilot/tpi 7.0.0-beta.1 → 7.0.0-beta.3

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.
@@ -1,3 +1,3 @@
1
1
  function _(){var e;return(e=window.PlayPilotLinkInjections)==null?void 0:e.token}const j="https://partner-api.playpilot.tech/1.0",k="EncryptedToken",P="articleReplacementEditToken",L="playpilot-editorial-mode";async function q(e=window.location.href){try{const t=_();if(!t)throw new Error("No token was provided");const n=I(e);if(!n)throw new Error("Could not be authenticated");return await f(`/external-pages/edit-authorization?api-token=${t}`,{method:"POST",body:{private_token:n}}),A(n),!0}catch(t){throw console.error(t),t}finally{C()}}function I(e=""){var i,o;const t=(i=window==null?void 0:window.PlayPilotLinkInjections)==null?void 0:i.editorial_token;if(t)return t;if(e){const c=new URL(e).searchParams.get(P);if(c)return decodeURI(c)}return((o=("; "+document.cookie).split(`; ${k}=`).pop())==null?void 0:o.split(";")[0])||""}function A(e){const t=new Date;t.setTime(t.getTime()+30*24*60*60*1e3);const i="expires="+t.toUTCString();document.cookie=k+"="+e+";"+i+";path=/"}function T(){var t;const e=(t=window==null?void 0:window.PlayPilotLinkInjections)==null?void 0:t.editorial_token;return new URLSearchParams(window.location.search).get(L)==="true"||!!e}function C(){const e=new URL(window.location.href);e.searchParams.delete(P),window.history.replaceState({},"",e)}const h={};async function f(e,{headers:t={},method:n="GET",body:i=null}={}){const o=!T()&&n==="GET";if(o&&e in h)return h[e];const c=n==="GET"?{}:{"Content-Type":"application/json"},s=await fetch(j+e,{method:n,headers:new Headers({...c,...t}),body:i?JSON.stringify(i):null});if(!(s!=null&&s.ok))throw s;const l=await s.text();let r=l;try{l&&(r=JSON.parse(l))}catch{}return o&&(h[e]=r),r}async function O(){const e=_();if(!e)throw new Error("No token was provided");try{return await await f(`/domains/config?api-token=${e}`)||null}catch{return null}}function x(e){let t=0;for(let n=0;n<e.length;n++){const i=e.charCodeAt(n);t=t*31+i>>>0}return t.toString(16)}function N(){return(Math.random()+1).toString(36).substring(7)}const p=Object.freeze({English:"en-US",Swedish:"sv-SE",Danish:"da-DK"});function M(){var i,o;const e=(i=window.PlayPilotLinkInjections)==null?void 0:i.language,t=Object.values(p);if(e){if(t.includes(e))return e;console.warn(`PlayPilot Link Injections: ${e} is not an accepted language`)}const n=(o=document.querySelector("html"))==null?void 0:o.getAttribute("lang");if(n){if(t.includes(n))return n;const c=t.find(s=>s.toLowerCase().startsWith(n+"-"));if(c)return c}return p.English}function R(e,t=""){const n=[],i=[e];for(;i.length>0;){const o=i.pop();if(n.includes(o)||t&&o.matches(t)||/^(A|BUTTON|SCRIPT|NOSCRIPT|STYLE|IFRAME|FIGCAPTION|TIME|LABEL|BLOCKQUOTE|H1)$/.test(o.tagName)||o.offsetWidth<=1)continue;const c=Array.from(o.childNodes).some(r=>{var u;return r.nodeType===Node.TEXT_NODE&&((u=r.nodeValue)==null?void 0:u.trim())!==""});if(c&&o.tagName==="LI"){const r=o.closest("ul, ol");r&&!n.includes(r)&&n.push(r);continue}const s=o.tagName==="P"&&!!o.textContent;if(c||s){n.push(o);continue}const l=Array.from(o.children);for(let r=l.length-1;r>=0;r--)i.push(l[r])}return n}function S(){var t;const e=(t=window.PlayPilotLinkInjections)==null?void 0:t.selector;if(e){const n=e.replace(/(:)/g,"\\$1"),i=document.querySelector(n);if(i)return i}return document.querySelector("article")||document.querySelector("main")||document.body}function v(e){return e.map(t=>t.innerText).join(`
2
2
 
3
- `)}function b(){const e=S();return{content_heading:U(e),content_modified_time:$(e),content_published_time:D(e)}}function U(e){const t=e.querySelector("h1")||document.querySelector("h1");return(t==null?void 0:t.innerText.trim())||null}function $(e){const t=document.querySelector('meta[property*="modified_time"]')||document.querySelector('meta[name*="modified_time"]')||e.querySelector('[itemprop="dateModified"]')||document.querySelector('[itemprop="dateModified"]')||null,n=E().dateModified||(t==null?void 0:t.getAttribute("content"))||(t==null?void 0:t.getAttribute("datetime"))||(t==null?void 0:t.innerText);return n?w(n):null}function D(e){const t=document.querySelector('meta[property*="published_time"]')||document.querySelector('meta[name*="published_time"]')||e.querySelector('[itemprop="datePublished"]')||document.querySelector('[itemprop="datePublished"]')||e.querySelector("time")||document.querySelector("time")||e.querySelector("[datetime]")||document.querySelector("[datetime]")||null,n=E().datePublished||(t==null?void 0:t.getAttribute("content"))||(t==null?void 0:t.getAttribute("datetime"))||(t==null?void 0:t.innerText);return n?w(n):null}function w(e){try{return new Date(e).toISOString()}catch{return null}}function E(){const e=document.querySelector('[type="application/ld+json"]');if(!e)return{};try{return JSON.parse(e.textContent||"")}catch{return{}}}function z(){const e=location.pathname.replace(/\/+/g,"/");return location.protocol+"//"+location.host+e}let g=null;async function d(e,{url:t=z(),hash:n=x(e||""),params:i={},method:o="GET"}={}){const c=_(),s=T()?await q():!1,l=M();if(!c)throw new Error("No token was provided");const r=`/external-pages/?api-token=${c}&include_title_details=true${s?"&editorial_mode_enabled=true":""}&language=${l}`;let u;return o==="POST"?(i.run_ai&&(i.hash=n,i.page_text=e),i.url=t,i.metadata=b(),u=await f(r,{method:"POST",body:i})):u=await f(r+`&url=${t}`,{method:"GET"}),window.PlayPilotLinkInjections.last_successful_fetch=u,u}async function G(e,{requireCompletedResult:t=!1,runAiWhenRelevant:n=!1,pollInterval:i=3e3,maxTries:o=600,onpoll:c=()=>null}={}){let s=0;g&&clearTimeout(g);const l=async(r,u)=>{let a;try{try{a=await d(e)}catch(m){throw(m==null?void 0:m.status)===404&&(a=await d(e,{method:"POST"}),a.ai_enabled&&(a=await d(e,{params:{run_ai:!0},method:"POST"}))),new Error}if(a&&n&&(a=await H(e,a)),t&&a.ai_enabled&&a.ai_running)throw new Error;a.manual_injections=y(a.manual_injections||[]),a.ai_injections=y(a.ai_injections||[]),r(a);return}catch{if(s++,s>=o){o>1&&console.error("Max poll limit exceeded. Please return later."),r();return}a&&c(a),g=setTimeout(()=>l(r),i)}};return new Promise(l)}async function H(e,t){if(!e||!t.ai_enabled||t.ai_running)return t;const n=b().content_modified_time,i=t.content_modified_time?w(t.content_modified_time):null,o=n&&i===n;return t.ai_last_run&&o||new Date(n||0)<=new Date(i||0)?t:await d(e,{params:{run_ai:!0},method:"POST"})}function y(e){return e.map(t=>({...t,key:B(t.sid)}))}function B(e){return e+"-"+N()}function F(e){window.PlayPilotLinkInjections&&(window.PlayPilotLinkInjections.consents=e)}function J(){const e=navigator.userAgent.toLowerCase();return/bot|crawl|spider|google|baidu|bing|msn|teoma|slurp|yandex|facebookexternalhit|facebot/i.test(e)}window.PlayPilotLinkInjections={token:"",editorial_token:"",selector:"",after_article_selector:"",after_article_insert_position:"",language:null,region:null,organization_sid:null,domain_sid:null,last_successful_fetch:null,tracked_events:[],queued_tracking_events:[],split_test_identifiers:{},evaluated_link_injections:[],initial_link_injections_promise:null,ads:[],require_consent:!0,consents:{ads:!1,pixels:!1,tracking:!1,split_tests:!1,affiliate:!1},config:{},app:null,async initialize(e={token:"",selector:"",after_article_selector:"",after_article_insert_position:"",language:null,region:null,organization_sid:null,domain_sid:null,editorial_token:"",require_consent:!0}){if(!e.token){console.error("An API token is required.");return}this.token=e.token,this.editorial_token=e.editorial_token,this.selector=e.selector,this.after_article_selector=e.after_article_selector,this.after_article_insert_position=e.after_article_insert_position,this.language=e.language,this.region=e.region,this.organization_sid=e.organization_sid,this.domain_sid=e.domain_sid,this.require_consent=e.require_consent,this.require_consent===!1&&F({ads:!0,pixels:!0,tracking:!0,split_tests:!0,affiliate:!0}),this.config=await O();const t=S(),n=R(t,this.config.exclude_elements_selector||""),i=v(n);this.initial_link_injections_promise=G(i,{maxTries:1,runAiWhenRelevant:!J()}),this.mount()},destroy(){var e;(e=window.PlayPilotMount)==null||e.destroy()},mount(){const e=document.createElement("script");e.src="https://cdn.jsdelivr.net/npm/@playpilot/tpi@7.0.0-beta.1/dist/mount.js",e.onload=()=>{var t;console.log("on load"),(t=window.PlayPilotMount)==null||t.mount()},document.body.insertAdjacentElement("beforeend",e)}};
3
+ `)}function b(){const e=S();return{content_heading:U(e),content_modified_time:$(e),content_published_time:D(e)}}function U(e){const t=e.querySelector("h1")||document.querySelector("h1");return(t==null?void 0:t.innerText.trim())||null}function $(e){const t=document.querySelector('meta[property*="modified_time"]')||document.querySelector('meta[name*="modified_time"]')||e.querySelector('[itemprop="dateModified"]')||document.querySelector('[itemprop="dateModified"]')||null,n=E().dateModified||(t==null?void 0:t.getAttribute("content"))||(t==null?void 0:t.getAttribute("datetime"))||(t==null?void 0:t.innerText);return n?w(n):null}function D(e){const t=document.querySelector('meta[property*="published_time"]')||document.querySelector('meta[name*="published_time"]')||e.querySelector('[itemprop="datePublished"]')||document.querySelector('[itemprop="datePublished"]')||e.querySelector("time")||document.querySelector("time")||e.querySelector("[datetime]")||document.querySelector("[datetime]")||null,n=E().datePublished||(t==null?void 0:t.getAttribute("content"))||(t==null?void 0:t.getAttribute("datetime"))||(t==null?void 0:t.innerText);return n?w(n):null}function w(e){try{return new Date(e).toISOString()}catch{return null}}function E(){const e=document.querySelector('[type="application/ld+json"]');if(!e)return{};try{return JSON.parse(e.textContent||"")}catch{return{}}}function z(){const e=location.pathname.replace(/\/+/g,"/");return location.protocol+"//"+location.host+e}let g=null;async function d(e,{url:t=z(),hash:n=x(e||""),params:i={},method:o="GET"}={}){const c=_(),s=T()?await q():!1,l=M();if(!c)throw new Error("No token was provided");const r=`/external-pages/?api-token=${c}&include_title_details=true${s?"&editorial_mode_enabled=true":""}&language=${l}`;let u;return o==="POST"?(i.run_ai&&(i.hash=n,i.page_text=e),i.url=t,i.metadata=b(),u=await f(r,{method:"POST",body:i})):u=await f(r+`&url=${t}`,{method:"GET"}),window.PlayPilotLinkInjections.last_successful_fetch=u,u}async function G(e,{requireCompletedResult:t=!1,runAiWhenRelevant:n=!1,pollInterval:i=3e3,maxTries:o=600,onpoll:c=()=>null}={}){let s=0;g&&clearTimeout(g);const l=async(r,u)=>{let a;try{try{a=await d(e)}catch(m){throw(m==null?void 0:m.status)===404&&(a=await d(e,{method:"POST"}),a.ai_enabled&&(a=await d(e,{params:{run_ai:!0},method:"POST"}))),new Error}if(a&&n&&(a=await H(e,a)),t&&a.ai_enabled&&a.ai_running)throw new Error;a.manual_injections=y(a.manual_injections||[]),a.ai_injections=y(a.ai_injections||[]),r(a);return}catch{if(s++,s>=o){o>1&&console.error("Max poll limit exceeded. Please return later."),r();return}a&&c(a),g=setTimeout(()=>l(r),i)}};return new Promise(l)}async function H(e,t){if(!e||!t.ai_enabled||t.ai_running)return t;const n=b().content_modified_time,i=t.content_modified_time?w(t.content_modified_time):null,o=n&&i===n;return t.ai_last_run&&o||new Date(n||0)<=new Date(i||0)?t:await d(e,{params:{run_ai:!0},method:"POST"})}function y(e){return e.map(t=>({...t,key:B(t.sid)}))}function B(e){return e+"-"+N()}function F(e){window.PlayPilotLinkInjections&&(window.PlayPilotLinkInjections.consents=e)}function J(){const e=navigator.userAgent.toLowerCase();return/bot|crawl|spider|google|baidu|bing|msn|teoma|slurp|yandex|facebookexternalhit|facebot/i.test(e)}window.PlayPilotLinkInjections={token:"",editorial_token:"",selector:"",after_article_selector:"",after_article_insert_position:"",language:null,region:null,organization_sid:null,domain_sid:null,last_successful_fetch:null,tracked_events:[],queued_tracking_events:[],split_test_identifiers:{},evaluated_link_injections:[],initial_link_injections_promise:null,ads:[],require_consent:!0,consents:{ads:!1,pixels:!1,tracking:!1,split_tests:!1,affiliate:!1},config:{},app:null,async initialize(e={token:"",selector:"",after_article_selector:"",after_article_insert_position:"",language:null,region:null,organization_sid:null,domain_sid:null,editorial_token:"",require_consent:!0}){if(!e.token){console.error("An API token is required.");return}this.token=e.token,this.editorial_token=e.editorial_token,this.selector=e.selector,this.after_article_selector=e.after_article_selector,this.after_article_insert_position=e.after_article_insert_position,this.language=e.language,this.region=e.region,this.organization_sid=e.organization_sid,this.domain_sid=e.domain_sid,this.require_consent=e.require_consent,this.require_consent===!1&&F({ads:!0,pixels:!0,tracking:!0,split_tests:!0,affiliate:!0}),this.config=await O()||{};const t=S(),n=R(t,this.config.exclude_elements_selector||""),i=v(n);this.initial_link_injections_promise=G(i,{maxTries:1,runAiWhenRelevant:!J()}),this.mount()},destroy(){var e;(e=window.PlayPilotMount)==null||e.destroy()},mount(){const e=document.createElement("script");e.src="https://cdn.jsdelivr.net/npm/@playpilot/tpi@7.0.0-beta.3/dist/mount.js",e.onload=()=>{var t;console.log("on load"),(t=window.PlayPilotMount)==null||t.mount()},document.body.insertAdjacentElement("beforeend",e)}};