@playpilot/tpi 7.0.0-beta.6 → 7.0.0-beta.7

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
- (function(){"use strict";function m(){var e;return(e=window.PlayPilotLinkInjections)==null?void 0:e.token}const L="https://partner-api.playpilot.tech/1.0",y="EncryptedToken",k="articleReplacementEditToken",q="playpilot-editorial-mode";async function x({href:e,throwError:t}={href:window.location.href,throwError:!1}){try{const n=m();if(!n)throw new Error("No token was provided");const i=A(e);if(!i)throw new Error("Could not be authenticated");return await d(`/external-pages/edit-authorization?api-token=${n}`,{method:"POST",body:{private_token:i}}),I(i),!0}catch(n){if(console.error(n),t)throw n}finally{C()}return!1}function A(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(k);if(c)return decodeURI(c)}return((o=("; "+document.cookie).split(`; ${y}=`).pop())==null?void 0:o.split(";")[0])||""}function I(e){const t=new Date;t.setTime(t.getTime()+30*24*60*60*1e3);const i="expires="+t.toUTCString();document.cookie=y+"="+e+";"+i+";path=/"}function P(){var t;const e=(t=window==null?void 0:window.PlayPilotLinkInjections)==null?void 0:t.editorial_token;return new URLSearchParams(window.location.search).get(q)==="true"||!!e}function C(){const e=new URL(window.location.href);e.searchParams.delete(k),window.history.replaceState({},"",e)}const h={};async function d(e,{headers:t={},method:n="GET",body:i=null}={}){const o=!P()&&n==="GET";if(o&&e in h)return h[e];const c=n==="GET"?{}:{"Content-Type":"application/json"},s=await fetch(L+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=m();if(!e)throw new Error("No token was provided");try{return await await d(`/domains/config?api-token=${e}`)||null}catch{return null}}function N(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 M(){return(Math.random()+1).toString(36).substring(7)}const T=Object.freeze({English:"en-US",Swedish:"sv-SE",Danish:"da-DK"});function R(){var i,o;const e=(i=window.PlayPilotLinkInjections)==null?void 0:i.language,t=Object.values(T);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 T.English}function U(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(`
1
+ (function(){"use strict";function m(){var e;return(e=window.PlayPilotLinkInjections)==null?void 0:e.token}const q="https://partner-api.playpilot.tech/1.0",h={};async function d(e,{headers:t={},method:n="GET",body:i=null}={}){const o=!g()&&n==="GET";if(o&&e in h)return h[e];const c=n==="GET"?{}:{"Content-Type":"application/json"},s=await fetch(q+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}const y="EncryptedToken",k="articleReplacementEditToken",x="playpilot-editorial-mode";async function A({href:e,throwError:t}={href:window.location.href,throwError:!1}){try{const n=m();if(!n)throw new Error("No token was provided");const i=P(e);if(!i)throw new Error("Could not be authenticated");return await d(`/external-pages/edit-authorization?api-token=${n}`,{method:"POST",body:{private_token:i}}),I(i),!0}catch(n){if(console.error(n),t)throw n}finally{C()}return!1}function P(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(k);if(c)return decodeURI(c)}return((o=("; "+document.cookie).split(`; ${y}=`).pop())==null?void 0:o.split(";")[0])||""}function I(e){const t=new Date;t.setTime(t.getTime()+30*24*60*60*1e3);const i="expires="+t.toUTCString();document.cookie=y+"="+e+";"+i+";path=/"}function g(){var t;const e=(t=window==null?void 0:window.PlayPilotLinkInjections)==null?void 0:t.editorial_token;return new URLSearchParams(window.location.search).get(x)==="true"||!!e}function C(){const e=new URL(window.location.href);e.searchParams.delete(k),window.history.replaceState({},"",e)}async function O(){const e=m();if(!e)throw new Error("No token was provided");try{return await await d(`/domains/config?api-token=${e}`)||null}catch{return null}}function N(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 M(){return(Math.random()+1).toString(36).substring(7)}const T=Object.freeze({English:"en-US",Swedish:"sv-SE",Danish:"da-DK"});function R(){var i,o;const e=(i=window.PlayPilotLinkInjections)==null?void 0:i.language,t=Object.values(T);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 T.English}function U(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 $(e){const t=S(),n=U(t,(e==null?void 0:e.exclude_elements_selector)||""),i=v(n);return{parentElement:t,elements:n,pageText:i}}function E(){const e=S();return{content_heading:D(e),content_modified_time:z(e),content_published_time:G(e)}}function D(e){const t=e.querySelector("h1")||document.querySelector("h1");return(t==null?void 0:t.innerText.trim())||null}function z(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=b().dateModified||(t==null?void 0:t.getAttribute("content"))||(t==null?void 0:t.getAttribute("datetime"))||(t==null?void 0:t.innerText);return n?g(n):null}function G(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=b().datePublished||(t==null?void 0:t.getAttribute("content"))||(t==null?void 0:t.getAttribute("datetime"))||(t==null?void 0:t.innerText);return n?g(n):null}function g(e){try{return new Date(e).toISOString()}catch{return null}}function b(){const e=document.querySelector('[type="application/ld+json"]');if(!e)return{};try{return JSON.parse(e.textContent||"")}catch{return{}}}function _(){const e=location.pathname.replace(/\/+/g,"/");return location.protocol+"//"+location.host+e}function H(e){return console.log(_()),!!(e.exclude_urls_pattern&&_().match(new RegExp(e.exclude_urls_pattern)))}let w=null;async function f(e,{url:t=_(),hash:n=N(e||""),params:i={},method:o="GET"}={}){const c=m(),s=P()?await x():!1,l=R();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=E(),u=await d(r,{method:"POST",body:i})):u=await d(r+`&url=${t}`,{method:"GET"}),window.PlayPilotLinkInjections.last_successful_fetch=u,u}async function B(e,{requireCompletedResult:t=!1,runAiWhenRelevant:n=!1,pollInterval:i=3e3,maxTries:o=600,onpoll:c=()=>null}={}){let s=0;w&&clearTimeout(w);const l=async(r,u)=>{let a;try{try{a=await f(e)}catch(p){throw(p==null?void 0:p.status)===404&&(a=await f(e,{method:"POST"}),a.ai_enabled&&(a=await f(e,{params:{run_ai:!0},method:"POST"}))),new Error}if(a&&n&&(a=await F(e,a)),t&&a.ai_enabled&&a.ai_running)throw new Error;a.manual_injections=j(a.manual_injections||[]),a.ai_injections=j(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),w=setTimeout(()=>l(r),i)}};return new Promise(l)}async function F(e,t){if(!e||!t.ai_enabled||t.ai_running)return t;const n=E().content_modified_time,i=t.content_modified_time?g(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 f(e,{params:{run_ai:!0},method:"POST"})}function j(e){return e.map(t=>({...t,key:J(t.sid)}))}function J(e){return e+"-"+M()}function K(e){window.PlayPilotLinkInjections&&(window.PlayPilotLinkInjections.consents=e)}function W(){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}if(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&&K({ads:!0,pixels:!0,tracking:!0,split_tests:!0,affiliate:!0}),this.config=await O()||{},H(this.config))return;const{pageText:t}=$(this.config);this.initial_link_injections_promise=B(t,{maxTries:1,runAiWhenRelevant:!W()}),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.5/dist/mount.js",e.onload=()=>{var t;console.log("on load"),(t=window.PlayPilotMount)==null||t.mount()},document.body.insertAdjacentElement("beforeend",e)}}})();
3
+ `)}function $(e){const t=S(),n=U(t,(e==null?void 0:e.exclude_elements_selector)||""),i=v(n);return{parentElement:t,elements:n,pageText:i}}function E(){const e=S();return{content_heading:D(e),content_modified_time:z(e),content_published_time:G(e)}}function D(e){const t=e.querySelector("h1")||document.querySelector("h1");return(t==null?void 0:t.innerText.trim())||null}function z(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=b().dateModified||(t==null?void 0:t.getAttribute("content"))||(t==null?void 0:t.getAttribute("datetime"))||(t==null?void 0:t.innerText);return n?_(n):null}function G(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=b().datePublished||(t==null?void 0:t.getAttribute("content"))||(t==null?void 0:t.getAttribute("datetime"))||(t==null?void 0:t.innerText);return n?_(n):null}function _(e){try{return new Date(e).toISOString()}catch{return null}}function b(){const e=document.querySelector('[type="application/ld+json"]');if(!e)return{};try{return JSON.parse(e.textContent||"")}catch{return{}}}function j(){const e=location.pathname.replace(/\/+/g,"/");return location.protocol+"//"+location.host+e}function H(e){return!!(e.exclude_urls_pattern&&j().match(new RegExp(e.exclude_urls_pattern)))}let w=null;async function f(e,{url:t=j(),hash:n=N(e||""),params:i={},method:o="GET"}={}){const c=m(),s=g()?await A():!1,l=R();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=E(),u=await d(r,{method:"POST",body:i})):u=await d(r+`&url=${t}`,{method:"GET"}),window.PlayPilotLinkInjections.last_successful_fetch=u,u}async function B(e,{requireCompletedResult:t=!1,runAiWhenRelevant:n=!1,pollInterval:i=3e3,maxTries:o=600,onpoll:c=()=>null}={}){let s=0;w&&clearTimeout(w);const l=async(r,u)=>{let a;try{try{a=await f(e)}catch(p){throw(p==null?void 0:p.status)===404&&(a=await f(e,{method:"POST"}),a.ai_enabled&&(a=await f(e,{params:{run_ai:!0},method:"POST"}))),new Error}if(a&&n&&(a=await F(e,a)),t&&a.ai_enabled&&a.ai_running)throw new Error;a.manual_injections=L(a.manual_injections||[]),a.ai_injections=L(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),w=setTimeout(()=>l(r),i)}};return new Promise(l)}async function F(e,t){if(!e||!t.ai_enabled||t.ai_running)return t;const n=E().content_modified_time,i=t.content_modified_time?_(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 f(e,{params:{run_ai:!0},method:"POST"})}function L(e){return e.map(t=>({...t,key:J(t.sid)}))}function J(e){return e+"-"+M()}function K(e){window.PlayPilotLinkInjections&&(window.PlayPilotLinkInjections.consents=e)}function W(){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}if(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&&K({ads:!0,pixels:!0,tracking:!0,split_tests:!0,affiliate:!0}),this.config=await O()||{},H(this.config))return;const{pageText:t}=$(this.config);this.initial_link_injections_promise=B(t,{maxTries:1,runAiWhenRelevant:!W()}),this.mount()},destroy(){var e;(e=window.PlayPilotMount)==null||e.destroy()},mount(){const e=g()||P(),t=document.createElement("script");t.src=`https://cdn.jsdelivr.net/npm/@playpilot/tpi@7.0.0-beta.7/dist/${e?"editorial.":""}mount.js`,t.onload=()=>{var n;return(n=window.PlayPilotMount)==null?void 0:n.mount()},document.body.insertAdjacentElement("beforeend",t)}}})();