@linkitbe/sdk 1.0.10 → 1.0.12
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/linkitbe_sdk.js +39 -2
- package/dist/linkitbe_sdk.min.js +1 -1
- package/package.json +1 -1
package/dist/linkitbe_sdk.js
CHANGED
|
@@ -22,7 +22,9 @@
|
|
|
22
22
|
feedObserverRootSelector: null,
|
|
23
23
|
// true = перед POST integration-viewable запросить GET /v1/events/integration-viewable-token (нужен секрет на markup-api).
|
|
24
24
|
fetchImpressionTokenForViewable: true,
|
|
25
|
-
|
|
25
|
+
// null — mini-offer в конец contentBlock; CSS-селектор — вставить перед первым совпадением внутри блока.
|
|
26
|
+
miniOfferInsertBeforeSelector: null,
|
|
27
|
+
sdkVersion: '1.0.12',
|
|
26
28
|
styles: {
|
|
27
29
|
link_color: '#e2187b',
|
|
28
30
|
link_weight: '700',
|
|
@@ -743,6 +745,41 @@
|
|
|
743
745
|
if (padRight > 0) outro.style.paddingRight = `${padRight}px`;
|
|
744
746
|
}
|
|
745
747
|
|
|
748
|
+
function findMiniOfferInsertAnchor(contentBlock) {
|
|
749
|
+
const raw = config.miniOfferInsertBeforeSelector;
|
|
750
|
+
const selector = raw != null ? String(raw).trim() : '';
|
|
751
|
+
if (!selector) {
|
|
752
|
+
return null;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
try {
|
|
756
|
+
return contentBlock.querySelector(selector);
|
|
757
|
+
} catch (e) {
|
|
758
|
+
console.warn('[LinkItBe] miniOfferInsertBeforeSelector invalid:', selector, e);
|
|
759
|
+
return null;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function insertMiniOfferElement(contentBlock, outro) {
|
|
764
|
+
const anchor = findMiniOfferInsertAnchor(contentBlock);
|
|
765
|
+
if (!anchor || !contentBlock.contains(anchor) || anchor === outro) {
|
|
766
|
+
contentBlock.appendChild(outro);
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
try {
|
|
771
|
+
const parent = anchor.parentNode;
|
|
772
|
+
if (!parent) {
|
|
773
|
+
contentBlock.appendChild(outro);
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
parent.insertBefore(outro, anchor);
|
|
777
|
+
} catch (e) {
|
|
778
|
+
console.warn('[LinkItBe] mini-offer insertBefore failed, falling back to append', e);
|
|
779
|
+
contentBlock.appendChild(outro);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
|
|
746
783
|
function applyMiniOffer(contentBlock, integration, linkStyle, labelingTemplate, pageConfig) {
|
|
747
784
|
if (!contentBlock) {
|
|
748
785
|
console.warn('[LinkItBe] mini-offer: no content block');
|
|
@@ -780,7 +817,7 @@
|
|
|
780
817
|
outro.appendChild(marker.node);
|
|
781
818
|
}
|
|
782
819
|
|
|
783
|
-
contentBlock
|
|
820
|
+
insertMiniOfferElement(contentBlock, outro);
|
|
784
821
|
applyMobileSideInsetIfNeeded(outro, contentBlock);
|
|
785
822
|
return true;
|
|
786
823
|
}
|
package/dist/linkitbe_sdk.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){const e=Object.assign({domain:window.location.hostname,bootOnLoad:!0,contentSelector:null,showPreview:!0,plainBlockJoiner:"\n",plainBlockSelector:"p, blockquote, h1, h2, h3, h4, h5, h6",allowedFormats:["keyword","mini-offer"],apiBaseUrl:"https://api.linkitbe.com",partnerApiKey:null,articleIdResolver:null,enableFeedAutoProcess:!1,feedObserverRootSelector:null,fetchImpressionTokenForViewable:!0,sdkVersion:"1.0.10",styles:{link_color:"#e2187b",link_weight:"700",link_decoration:"underline"}},window.LinkItBeConfig||{});function t(t){if("function"==typeof e.articleIdResolver)try{const n=e.articleIdResolver(t),i=null!=n?String(n).trim():"";if(i)return i}catch(e){console.warn("[LinkItBe] articleIdResolver failed",e)}return null}function n(){const e="__lb_session_id";try{const t=window.localStorage.getItem(e);if(t&&t.trim())return t.trim();const n=window.crypto&&window.crypto.randomUUID?window.crypto.randomUUID():`${Date.now()}-${Math.random().toString(16).slice(2)}`;return window.localStorage.setItem(e,n),n}catch(e){return`${Date.now()}-${Math.random().toString(16).slice(2)}`}}function i(e){if(!Array.isArray(e))return null;for(const t of e){const e=t&&t.link?String(t.link):"";if(e)try{const t=new URL(e,window.location.origin).searchParams.get("processing_id"),n=Number(t);if(Number.isFinite(n)&&n>0)return n}catch(e){}}return null}async function o(t,o,r,l,s){const a=i([l]);if(!a)return;const c=e.partnerApiKey?String(e.partnerApiKey).trim():"";if(!c)return;const d=Object.assign({event_id:window.crypto&&window.crypto.randomUUID?window.crypto.randomUUID():`${Date.now()}-${Math.random().toString(16).slice(2)}`,event_type:t,event_time:(new Date).toISOString(),article_id:r,domain:e.domain,processing_id:a,placement_link:window.location.href,integration_type:l&&null!=l.type?l.type:null,sdk_version:String(e.sdkVersion||""),session_id:n(),referrer:document.referrer||null},s||{});if("integration_viewable"===t&&e.fetchImpressionTokenForViewable){const t=await async function(t,n){const i=e.partnerApiKey?String(e.partnerApiKey).trim():"";if(!i||!n)return null;try{const o=String(e.apiBaseUrl||"https://api.linkitbe.com").replace(/\/$/,""),r=new URL(o+"/v1/events/integration-viewable-token");r.searchParams.set("article_id",t),r.searchParams.set("domain",String(e.domain||"")),r.searchParams.set("processing_id",String(n));const l=await fetch(r.toString(),{method:"GET",headers:{Accept:"application/json","X-Partner-Api-Key":i},credentials:"omit"});if(!l.ok)return null;const s=await l.json();return s&&s.impression_token?String(s.impression_token):null}catch(e){return console.warn("[LinkItBe] impression token fetch failed",e),null}}(r,a);t&&(d.impression_token=t)}try{const t=String(e.apiBaseUrl||"https://api.linkitbe.com").replace(/\/$/,"");await fetch(`${t}${o}`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Partner-Api-Key":c},body:JSON.stringify(d),keepalive:!0,credentials:"omit"})}catch(e){console.warn("[LinkItBe] failed to send "+t,e)}}async function r(e,t,n){if(n<=0)return;const r=function(e){if(!Array.isArray(e))return null;for(let t=0;t<e.length;t++){const n=e[t];if(n&&i([n]))return n}return null}(t);r&&await o("view_rendered","/v1/events/view-rendered",e,r,{integrations_count:n})}function l(t){const n=e.allowedFormats;return null!=n&&Array.isArray(n)?t.filter(e=>n.includes(e.type)):t}function s(e,t,n){const i=12,o=e.offsetHeight||0,r=e.offsetWidth||0;let l=t+16,s=n-o+60;const a=window.innerWidth-r-i;l=Math.min(Math.max(l,i),Math.max(i,a));s+o>window.innerHeight-i&&(s=n-o-i),e.style.left=l+"px",e.style.top=Math.max(i,s)+"px"}function a(e,t,n,i,o){if(!t.preview||!t.preview.img)return void(e.style.display="none");e.replaceChildren(),e.appendChild(function(e){const t=e&&null!=e.erid?String(e.erid).trim():"",n=document.createElement("div");return n.style.cssText="font-size:10px;line-height:1.35;color:#aaa;margin-bottom:6px;font-weight:400;",n.textContent=t?`#реклама · erid: ${t}`:"#реклама",n}(t));const r=document.createElement("img");r.src=String(t.preview.img),r.style.cssText="width:100%;border-radius:8px;display:block;",e.appendChild(r);const l=document.createElement("div");l.style.cssText="margin-top:8px;font-size:13px;color:#343b4c;font-weight:600;",l.textContent=t.preview.title?String(t.preview.title):"",e.appendChild(l);const a=document.createElement("div");a.style.cssText="font-size:12px;color:#777;margin-top:2px;",a.textContent=t.preview.price?String(t.preview.price):"",e.appendChild(a);const c=document.createElement("div");c.style.cssText=`font-size:12px;color:${o.link_color};margin-top:2px;`,c.textContent=t.preview.cta?String(t.preview.cta):"",e.appendChild(c),e.style.display="block",s(e,n,i)}function c(e){e&&(e.style.display="none")}function d(e){const t=[],n=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,{acceptNode:e=>function(e){const t=e.nodeType===Node.TEXT_NODE?e.parentElement:e;return!!(t&&t.closest&&t.closest("a.__lb_link"))}(e)?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT});let i;for(;i=n.nextNode();)t.push(i);return t}function u(e,t){if(!t)return[];const n=new RegExp(String(t).replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"gi");const i=[];let o;for(;null!==(o=n.exec(e));)i.push({start:o.index,end:o.index+o[0].length}),0===o[0].length&&n.lastIndex===o.index&&n.lastIndex++;return i}function p(e){if(null==e)return null;try{const t=new URL(String(e),window.location.origin);return"http:"!==t.protocol&&"https:"!==t.protocol?null:t.toString()}catch(e){return null}}function f(e,t){const n=e&&null!=e.erid?String(e.erid).trim():"",i=p(e&&e.advertiser_info_url);if(i)return function(e,t){const n=null!=t?String(t).trim():"";if(!n||!e)return e;try{const t=new URL(String(e),window.location.origin);return t.searchParams.set("erid",n),t.toString()}catch(t){return e}}(i,n);const o=t&&t.advertiser_info_base_url,r=e&&null!=e.advertiser?String(e.advertiser).trim():"";if(!o||!r)return null;try{const e=new URL(String(o),window.location.origin);return e.searchParams.append("items[]",r),n&&e.searchParams.set("erid",n),e.toString()}catch(e){return null}}function m(e,t,n){const i=e&&"object"==typeof e?e:{},o=i.marker_type||"plain_text",r=i.position||"before_phrase",l=null!=i.marker_text?String(i.marker_text).trim():"",s="font-size:11px;color:#999;display:block;margin-bottom:4px;";if("marker_link"===o){const e=l||"#реклама",i=function(e,t,n,i){const o=f(e,i);if(!o)return console.warn("[LinkItBe] advertiser info url is missing for marker",e&&e.advertiser),null;const r=document.createElement("a");return r.href=o,r.target="_blank",r.rel="noopener noreferrer sponsored nofollow",r.className="__lb_link __lb_advertiser_info_link",r.style.cssText=n.replace(/\s+/g," ").trim(),r.textContent=t,r}(t,e,"font-size:11px;color:#999;font-weight:400;text-decoration:underline;cursor:pointer;",n);if(i){const e=document.createElement("span");return e.style.cssText=s.replace("margin-bottom:4px;",""),e.appendChild(i),{node:e,position:r}}console.warn("[LinkItBe] marker_link fallback to plain text:",e);const o=document.createElement("span");return o.style.cssText=s,o.textContent=e,{node:o,position:r}}"plain_text"!==o&&console.warn("[LinkItBe] unsupported marker_type, fallback to plain_text:",o);const a=document.createElement("span");return a.style.cssText=s,a.textContent=function(e){const t=String(e||"#реклама").trim();return t?t.endsWith(":")?t:t.replace(/:+$/,"")+":":"#реклама"}(l||"#реклама"),{node:a,position:r}}function h(e,t,n){const i=p(e.link);if(!i)return null;const o=document.createElement("a");return o.href=i,o.target="_blank",o.rel="noopener noreferrer sponsored nofollow",o.className="__lb_link",o.setAttribute("data-lb-integration",encodeURIComponent(JSON.stringify(e))),o.style.cssText=n.replace(/\s+/g," ").trim(),o.textContent=t,o}function w(t,n){const{plain:i,spans:o}=function(t){const n=null!=e.plainBlockJoiner?e.plainBlockJoiner:"\n",i=e.plainBlockSelector||"p, blockquote, h1, h2, h3, h4, h5, h6",o=t.querySelectorAll(i),r=[];let l="";return o.forEach((e,t)=>{t>0&&(l+=n);const i=d(e);for(const e of i){const t=e.textContent;if(!t.length)continue;const n=l.length;l+=t,r.push({g0:n,g1:l.length,node:e})}}),{plain:l,spans:r}}(t),r=function(e,t){if(!e.length)return null;if(1===e.length)return e[0];const n=t||{},i=n.match_index;if(null!=i&&Number.isFinite(Number(i))){const t=Number(i);if(t>=0&&t<e.length)return e[t];console.warn("[LinkItBe] match_index out of range:",i,"n=",e.length)}const o=n.offset_start,r=n.offset_end;if(null!=o&&Number.isFinite(Number(o))){const t=Number(o),n=null!=r&&Number.isFinite(Number(r))?Number(r):null;let i=e[0],l=1/0;for(const o of e){const e=1e9*Math.abs(o.start-t)+(null!=n?Math.abs(o.end-n):0);e<l&&(l=e,i=o)}return i}return console.warn("[LinkItBe] ambiguous matches, skipping (no match_index / offset_start):",e.length),null}(u(i,n.keyword),n.position);if(!r)return null;const l=i.slice(r.start,r.end);return{plain:i,spans:o,span:r,linkText:l}}function g(e,t,n){const i=w(e,t);if(!i)return console.warn("[LinkItBe] no span for keyword:",t.keyword),!1;const{plain:o,spans:r,span:l,linkText:s}=i,a=function(e,t,n){if(e<0||e>=n||!t.length)return null;const i=t.find(t=>e>=t.g0&&e<t.g1);return i?{node:i.node,offset:e-i.g0}:null}(l.start,r,o.length),c=function(e,t,n){if(!t.length)return null;if(e<=0)return{node:t[0].node,offset:0};if(e>=n){const e=t[t.length-1];return{node:e.node,offset:e.node.textContent.length}}for(let n=0;n<t.length;n++){const i=t[n];if(e>i.g0&&e<=i.g1)return{node:i.node,offset:e-i.g0};if(e===i.g0)return{node:i.node,offset:0}}for(let n=0;n<t.length;n++)if(e<t[n].g0)return{node:t[n].node,offset:0};return null}(l.end,r,o.length);if(!a||!c)return console.warn("[LinkItBe] could not map span to DOM"),!1;const d=o.slice(l.start,l.end);if(d.toLowerCase()!==s.toLowerCase())return console.warn("[LinkItBe] plain slice mismatch"),!1;const u=document.createRange();u.setStart(a.node,a.offset),u.setEnd(c.node,c.offset);const p=h(t,d,n);return p?(function(e,t){try{e.surroundContents(t)}catch(n){const i=e.extractContents();for(;i.firstChild;)t.appendChild(i.firstChild);e.insertNode(t)}}(u,p),!0):(console.warn("[LinkItBe] invalid integration link:",t.link),!1)}const b="{anchor_text}";function y(e){const t=e.getBoundingClientRect();return{left:t.left,right:window.innerWidth-t.right}}function _(t,n){if(!window.matchMedia("(max-width: 768px)").matches)return;const i=y(t);if(i.left>=8&&i.right>=8)return;const o=function(t,n){const i=e.plainBlockSelector||"p, blockquote, h1, h2, h3, h4, h5, h6",o=t.querySelectorAll(i);for(let e=0;e<o.length;e++){const t=o[e];if(t!==n&&!t.classList.contains("__lb_mini_offer")&&(t.textContent||"").trim())return t}return null}(n,t),r=o?y(o):null,l=r&&r.left>=8?r.left:16,s=r&&r.right>=8?r.right:16,a=i.left<8?Math.max(0,Math.round(l-i.left)):0,c=i.right<8?Math.max(0,Math.round(s-i.right)):0;a>0&&(t.style.paddingLeft=`${a}px`),c>0&&(t.style.paddingRight=`${c}px`)}function k(t,n,i,r,d,u){if(!i){console.warn("[LinkItBe] Content block not provided");return{appliedCount:0,integrations:l(n)}}const p=e.showPreview?function(){const e=document.createElement("div");return e.id="__lb_preview",e.style.cssText="\n position: fixed;\n display: none;\n background: white;\n border-radius: 12px;\n box-shadow: 0 4px 20px rgba(0,0,0,0.15);\n padding: 12px;\n z-index: 99999;\n width: 180px;\n font-family: Manrope, sans-serif;\n pointer-events: none;\n ",document.body.appendChild(e),e}():null,f=`\n color:${r.link_color};\n font-weight:${r.link_weight};\n text-decoration:${r.link_decoration};\n cursor:pointer;\n `,y=l(n);let k=0,v=y.filter(e=>"keyword"===e.type);for(;v.length;){const e=[];for(const t of v){const n=w(i,t);n&&e.push({integ:t,spanStart:n.span.start})}if(!e.length){v.forEach(e=>console.warn("[LinkItBe] unresolved keyword:",e.keyword));break}e.sort((e,t)=>t.spanStart-e.spanStart);const{integ:t}=e[0];g(i,t,f)&&(k+=1),v=v.filter(e=>e!==t)}return y.filter(e=>"mini-offer"===e.type).forEach(e=>{!function(e,t,n,i,o){if(!e)return console.warn("[LinkItBe] mini-offer: no content block"),!1;const r=document.createElement("p");r.className="__lb_mini_offer",r.style.cssText="margin-top:16px;font-size:15px;font-family:Manrope, sans-serif;color:#343b4c;";const l=m(i,t,o),s=document.createElement("span"),a=null!=t.text?String(t.text):"",c=null!=t.anchor_text?String(t.anchor_text):"",d=a.indexOf(b),u=d>=0?a.slice(0,d):a,p=d>=0?a.slice(d+13):"";u&&s.appendChild(document.createTextNode(u));const f=h(t,c,n);return f?(s.appendChild(f),p&&s.appendChild(document.createTextNode(p)),l&&"before_phrase"===l.position&&r.appendChild(l.node),r.appendChild(s),l&&"after_phrase"===l.position&&(r.appendChild(document.createElement("br")),r.appendChild(l.node)),e.appendChild(r),_(r,e),!0):(console.warn("[LinkItBe] mini-offer invalid integration link:",t.link),!1)}(i,e,f,d,u)?console.warn("[LinkItBe] mini-offer failed"):k+=1}),p&&(!function(e){if(!e||"1"===e.dataset.lbDismissBound)return;e.dataset.lbDismissBound="1";const t=()=>c(e);document.addEventListener("visibilitychange",t),window.addEventListener("blur",t),window.addEventListener("pagehide",t),window.addEventListener("pageshow",t)}(p),i.querySelectorAll(".__lb_link").forEach(e=>{let n;try{n=JSON.parse(decodeURIComponent(e.dataset.lbIntegration||""))}catch(e){return void console.warn("[LinkItBe] invalid integration payload for preview",e)}if(!n.preview||!n.preview.img)return;const i=()=>c(p);e.addEventListener("mouseenter",i=>{"visible"===document.visibilityState&&(a(p,n,i.clientX,i.clientY,r),"1"!==e.dataset.lbPreviewSent&&t&&(e.dataset.lbPreviewSent="1",o("preview_viewed","/v1/events/preview-viewed",t,n)))}),e.addEventListener("mousemove",e=>{"none"!==p.style.display&&s(p,e.clientX,e.clientY)}),e.addEventListener("mouseleave",i),e.addEventListener("click",i),e.addEventListener("touchstart",i,{passive:!0})})),function(e,t){if(!e)return;const n=e=>{const t=e._lbViewDwellTimer;t&&(clearTimeout(t),e._lbViewDwellTimer=null)},i=new IntersectionObserver(t=>{for(let r=0;r<t.length;r++){const l=t[r],s=l.target;"1"!==s.dataset.lbViewableSent&&(!l.isIntersecting||l.intersectionRatio<.5?(n(s),s._lbViewVisibleSince=null,s._lbViewLastIr=null):(s._lbViewLastIr=String(Math.min(1,Math.max(0,l.intersectionRatio))),s._lbViewVisibleSince||(s._lbViewVisibleSince=Date.now()),n(s),s._lbViewDwellTimer=setTimeout(()=>{if(s._lbViewDwellTimer=null,"1"===s.dataset.lbViewableSent)return;if("visible"!==document.visibilityState)return;let t;try{t=JSON.parse(decodeURIComponent(s.dataset.lbIntegration||""))}catch(e){return void i.unobserve(s)}s.dataset.lbViewableSent="1";const n=s._lbViewVisibleSince||Date.now(),r=Math.max(0,Date.now()-n),l=parseFloat(s._lbViewLastIr||"0.5");o("integration_viewable","/v1/events/integration-viewable",e,t,{tab_visible:"visible"===document.visibilityState,dwell_ms:Math.max(1e3,r),intersection_ratio:l,viewability_rule_version:"elitesdk-1"}),i.unobserve(s)},1e3)))}},{threshold:[.5]});t.querySelectorAll(".__lb_link").forEach(e=>i.observe(e))}(t,i),{appliedCount:k,integrations:y}}async function v(t,n){if(!function(e,t){return e?!!t||(console.warn("[LinkItBe] Content block not found for article:",e),!1):(console.warn("[LinkItBe] Article ID not found"),!1)}(t,n))return;if("1"===n.dataset.lbProcessed)return;const i=await async function(t){const n=`${String(e.apiBaseUrl||"https://api.linkitbe.com").replace(/\/$/,"")}/v1/markup?article_id=${encodeURIComponent(t)}&domain=${encodeURIComponent(e.domain)}`,i={Accept:"application/json"},o=e.partnerApiKey?String(e.partnerApiKey).trim():"";o&&(i["X-Partner-Api-Key"]=o);const r=await fetch(n,{method:"GET",headers:i,credentials:"omit"});return r.ok?await r.json():null}(t);i&&await async function(e,t,n){if("1"===t.dataset.lbProcessed)return;t.dataset.lbProcessed="1";const i=Array.isArray(n.integrations)?n.integrations:[],o=n.config&&"object"==typeof n.config?n.config:{},l=k(e,i,t,o.style||{},o.labeling_template||null,o);await r(e,l.integrations,l.appliedCount)}(t,n,i)}function x(n){if(n.nodeType!==Node.ELEMENT_NODE)return;const i=n.querySelector(e.contentSelector);if(!i)return;v(t(i),i)}function S(e){if(!e)return void console.warn("[LinkItBe] feedRoot not provided for auto markup");new MutationObserver(e=>{for(const t of e)for(let e=0;e<t.addedNodes.length;e++)x(t.addedNodes[e])}).observe(e,{childList:!0,subtree:!0})}async function I(){const n=document.querySelector(e.contentSelector),i=t(n);if(await v(i,n),!0!==e.enableFeedAutoProcess)return;const o=document.querySelector(e.feedObserverRootSelector);o?S(o):console.warn("[LinkItBe] feedObserverRootSelector not found:",e.feedObserverRootSelector)}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",I):!0===e.bootOnLoad&&I(),window.LinkItBe=Object.assign({},window.LinkItBe||{},{initArticle:v,startFeedAutoMarkup:S})}();
|
|
1
|
+
!function(){const e=Object.assign({domain:window.location.hostname,bootOnLoad:!0,contentSelector:null,showPreview:!0,plainBlockJoiner:"\n",plainBlockSelector:"p, blockquote, h1, h2, h3, h4, h5, h6",allowedFormats:["keyword","mini-offer"],apiBaseUrl:"https://api.linkitbe.com",partnerApiKey:null,articleIdResolver:null,enableFeedAutoProcess:!1,feedObserverRootSelector:null,fetchImpressionTokenForViewable:!0,miniOfferInsertBeforeSelector:null,sdkVersion:"1.0.12",styles:{link_color:"#e2187b",link_weight:"700",link_decoration:"underline"}},window.LinkItBeConfig||{});function t(t){if("function"==typeof e.articleIdResolver)try{const n=e.articleIdResolver(t),i=null!=n?String(n).trim():"";if(i)return i}catch(e){console.warn("[LinkItBe] articleIdResolver failed",e)}return null}function n(){const e="__lb_session_id";try{const t=window.localStorage.getItem(e);if(t&&t.trim())return t.trim();const n=window.crypto&&window.crypto.randomUUID?window.crypto.randomUUID():`${Date.now()}-${Math.random().toString(16).slice(2)}`;return window.localStorage.setItem(e,n),n}catch(e){return`${Date.now()}-${Math.random().toString(16).slice(2)}`}}function i(e){if(!Array.isArray(e))return null;for(const t of e){const e=t&&t.link?String(t.link):"";if(e)try{const t=new URL(e,window.location.origin).searchParams.get("processing_id"),n=Number(t);if(Number.isFinite(n)&&n>0)return n}catch(e){}}return null}async function r(t,r,o,l,s){const a=i([l]);if(!a)return;const c=e.partnerApiKey?String(e.partnerApiKey).trim():"";if(!c)return;const d=Object.assign({event_id:window.crypto&&window.crypto.randomUUID?window.crypto.randomUUID():`${Date.now()}-${Math.random().toString(16).slice(2)}`,event_type:t,event_time:(new Date).toISOString(),article_id:o,domain:e.domain,processing_id:a,placement_link:window.location.href,integration_type:l&&null!=l.type?l.type:null,sdk_version:String(e.sdkVersion||""),session_id:n(),referrer:document.referrer||null},s||{});if("integration_viewable"===t&&e.fetchImpressionTokenForViewable){const t=await async function(t,n){const i=e.partnerApiKey?String(e.partnerApiKey).trim():"";if(!i||!n)return null;try{const r=String(e.apiBaseUrl||"https://api.linkitbe.com").replace(/\/$/,""),o=new URL(r+"/v1/events/integration-viewable-token");o.searchParams.set("article_id",t),o.searchParams.set("domain",String(e.domain||"")),o.searchParams.set("processing_id",String(n));const l=await fetch(o.toString(),{method:"GET",headers:{Accept:"application/json","X-Partner-Api-Key":i},credentials:"omit"});if(!l.ok)return null;const s=await l.json();return s&&s.impression_token?String(s.impression_token):null}catch(e){return console.warn("[LinkItBe] impression token fetch failed",e),null}}(o,a);t&&(d.impression_token=t)}try{const t=String(e.apiBaseUrl||"https://api.linkitbe.com").replace(/\/$/,"");await fetch(`${t}${r}`,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","X-Partner-Api-Key":c},body:JSON.stringify(d),keepalive:!0,credentials:"omit"})}catch(e){console.warn("[LinkItBe] failed to send "+t,e)}}async function o(e,t,n){if(n<=0)return;const o=function(e){if(!Array.isArray(e))return null;for(let t=0;t<e.length;t++){const n=e[t];if(n&&i([n]))return n}return null}(t);o&&await r("view_rendered","/v1/events/view-rendered",e,o,{integrations_count:n})}function l(t){const n=e.allowedFormats;return null!=n&&Array.isArray(n)?t.filter(e=>n.includes(e.type)):t}function s(e,t,n){const i=12,r=e.offsetHeight||0,o=e.offsetWidth||0;let l=t+16,s=n-r+60;const a=window.innerWidth-o-i;l=Math.min(Math.max(l,i),Math.max(i,a));s+r>window.innerHeight-i&&(s=n-r-i),e.style.left=l+"px",e.style.top=Math.max(i,s)+"px"}function a(e,t,n,i,r){if(!t.preview||!t.preview.img)return void(e.style.display="none");e.replaceChildren(),e.appendChild(function(e){const t=e&&null!=e.erid?String(e.erid).trim():"",n=document.createElement("div");return n.style.cssText="font-size:10px;line-height:1.35;color:#aaa;margin-bottom:6px;font-weight:400;",n.textContent=t?`#реклама · erid: ${t}`:"#реклама",n}(t));const o=document.createElement("img");o.src=String(t.preview.img),o.style.cssText="width:100%;border-radius:8px;display:block;",e.appendChild(o);const l=document.createElement("div");l.style.cssText="margin-top:8px;font-size:13px;color:#343b4c;font-weight:600;",l.textContent=t.preview.title?String(t.preview.title):"",e.appendChild(l);const a=document.createElement("div");a.style.cssText="font-size:12px;color:#777;margin-top:2px;",a.textContent=t.preview.price?String(t.preview.price):"",e.appendChild(a);const c=document.createElement("div");c.style.cssText=`font-size:12px;color:${r.link_color};margin-top:2px;`,c.textContent=t.preview.cta?String(t.preview.cta):"",e.appendChild(c),e.style.display="block",s(e,n,i)}function c(e){e&&(e.style.display="none")}function d(e){const t=[],n=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,{acceptNode:e=>function(e){const t=e.nodeType===Node.TEXT_NODE?e.parentElement:e;return!!(t&&t.closest&&t.closest("a.__lb_link"))}(e)?NodeFilter.FILTER_REJECT:NodeFilter.FILTER_ACCEPT});let i;for(;i=n.nextNode();)t.push(i);return t}function u(e,t){if(!t)return[];const n=new RegExp(String(t).replace(/[.*+?^${}()|[\]\\]/g,"\\$&"),"gi");const i=[];let r;for(;null!==(r=n.exec(e));)i.push({start:r.index,end:r.index+r[0].length}),0===r[0].length&&n.lastIndex===r.index&&n.lastIndex++;return i}function f(e){if(null==e)return null;try{const t=new URL(String(e),window.location.origin);return"http:"!==t.protocol&&"https:"!==t.protocol?null:t.toString()}catch(e){return null}}function p(e,t){const n=e&&null!=e.erid?String(e.erid).trim():"",i=f(e&&e.advertiser_info_url);if(i)return function(e,t){const n=null!=t?String(t).trim():"";if(!n||!e)return e;try{const t=new URL(String(e),window.location.origin);return t.searchParams.set("erid",n),t.toString()}catch(t){return e}}(i,n);const r=t&&t.advertiser_info_base_url,o=e&&null!=e.advertiser?String(e.advertiser).trim():"";if(!r||!o)return null;try{const e=new URL(String(r),window.location.origin);return e.searchParams.append("items[]",o),n&&e.searchParams.set("erid",n),e.toString()}catch(e){return null}}function m(e,t,n){const i=e&&"object"==typeof e?e:{},r=i.marker_type||"plain_text",o=i.position||"before_phrase",l=null!=i.marker_text?String(i.marker_text).trim():"",s="font-size:11px;color:#999;display:block;margin-bottom:4px;";if("marker_link"===r){const e=l||"#реклама",i=function(e,t,n,i){const r=p(e,i);if(!r)return console.warn("[LinkItBe] advertiser info url is missing for marker",e&&e.advertiser),null;const o=document.createElement("a");return o.href=r,o.target="_blank",o.rel="noopener noreferrer sponsored nofollow",o.className="__lb_link __lb_advertiser_info_link",o.style.cssText=n.replace(/\s+/g," ").trim(),o.textContent=t,o}(t,e,"font-size:11px;color:#999;font-weight:400;text-decoration:underline;cursor:pointer;",n);if(i){const e=document.createElement("span");return e.style.cssText=s.replace("margin-bottom:4px;",""),e.appendChild(i),{node:e,position:o}}console.warn("[LinkItBe] marker_link fallback to plain text:",e);const r=document.createElement("span");return r.style.cssText=s,r.textContent=e,{node:r,position:o}}"plain_text"!==r&&console.warn("[LinkItBe] unsupported marker_type, fallback to plain_text:",r);const a=document.createElement("span");return a.style.cssText=s,a.textContent=function(e){const t=String(e||"#реклама").trim();return t?t.endsWith(":")?t:t.replace(/:+$/,"")+":":"#реклама"}(l||"#реклама"),{node:a,position:o}}function h(e,t,n){const i=f(e.link);if(!i)return null;const r=document.createElement("a");return r.href=i,r.target="_blank",r.rel="noopener noreferrer sponsored nofollow",r.className="__lb_link",r.setAttribute("data-lb-integration",encodeURIComponent(JSON.stringify(e))),r.style.cssText=n.replace(/\s+/g," ").trim(),r.textContent=t,r}function w(t,n){const{plain:i,spans:r}=function(t){const n=null!=e.plainBlockJoiner?e.plainBlockJoiner:"\n",i=e.plainBlockSelector||"p, blockquote, h1, h2, h3, h4, h5, h6",r=t.querySelectorAll(i),o=[];let l="";return r.forEach((e,t)=>{t>0&&(l+=n);const i=d(e);for(const e of i){const t=e.textContent;if(!t.length)continue;const n=l.length;l+=t,o.push({g0:n,g1:l.length,node:e})}}),{plain:l,spans:o}}(t),o=function(e,t){if(!e.length)return null;if(1===e.length)return e[0];const n=t||{},i=n.match_index;if(null!=i&&Number.isFinite(Number(i))){const t=Number(i);if(t>=0&&t<e.length)return e[t];console.warn("[LinkItBe] match_index out of range:",i,"n=",e.length)}const r=n.offset_start,o=n.offset_end;if(null!=r&&Number.isFinite(Number(r))){const t=Number(r),n=null!=o&&Number.isFinite(Number(o))?Number(o):null;let i=e[0],l=1/0;for(const r of e){const e=1e9*Math.abs(r.start-t)+(null!=n?Math.abs(r.end-n):0);e<l&&(l=e,i=r)}return i}return console.warn("[LinkItBe] ambiguous matches, skipping (no match_index / offset_start):",e.length),null}(u(i,n.keyword),n.position);if(!o)return null;const l=i.slice(o.start,o.end);return{plain:i,spans:r,span:o,linkText:l}}function g(e,t,n){const i=w(e,t);if(!i)return console.warn("[LinkItBe] no span for keyword:",t.keyword),!1;const{plain:r,spans:o,span:l,linkText:s}=i,a=function(e,t,n){if(e<0||e>=n||!t.length)return null;const i=t.find(t=>e>=t.g0&&e<t.g1);return i?{node:i.node,offset:e-i.g0}:null}(l.start,o,r.length),c=function(e,t,n){if(!t.length)return null;if(e<=0)return{node:t[0].node,offset:0};if(e>=n){const e=t[t.length-1];return{node:e.node,offset:e.node.textContent.length}}for(let n=0;n<t.length;n++){const i=t[n];if(e>i.g0&&e<=i.g1)return{node:i.node,offset:e-i.g0};if(e===i.g0)return{node:i.node,offset:0}}for(let n=0;n<t.length;n++)if(e<t[n].g0)return{node:t[n].node,offset:0};return null}(l.end,o,r.length);if(!a||!c)return console.warn("[LinkItBe] could not map span to DOM"),!1;const d=r.slice(l.start,l.end);if(d.toLowerCase()!==s.toLowerCase())return console.warn("[LinkItBe] plain slice mismatch"),!1;const u=document.createRange();u.setStart(a.node,a.offset),u.setEnd(c.node,c.offset);const f=h(t,d,n);return f?(function(e,t){try{e.surroundContents(t)}catch(n){const i=e.extractContents();for(;i.firstChild;)t.appendChild(i.firstChild);e.insertNode(t)}}(u,f),!0):(console.warn("[LinkItBe] invalid integration link:",t.link),!1)}const b="{anchor_text}";function y(e){const t=e.getBoundingClientRect();return{left:t.left,right:window.innerWidth-t.right}}function _(t,n){if(!window.matchMedia("(max-width: 768px)").matches)return;const i=y(t);if(i.left>=8&&i.right>=8)return;const r=function(t,n){const i=e.plainBlockSelector||"p, blockquote, h1, h2, h3, h4, h5, h6",r=t.querySelectorAll(i);for(let e=0;e<r.length;e++){const t=r[e];if(t!==n&&!t.classList.contains("__lb_mini_offer")&&(t.textContent||"").trim())return t}return null}(n,t),o=r?y(r):null,l=o&&o.left>=8?o.left:16,s=o&&o.right>=8?o.right:16,a=i.left<8?Math.max(0,Math.round(l-i.left)):0,c=i.right<8?Math.max(0,Math.round(s-i.right)):0;a>0&&(t.style.paddingLeft=`${a}px`),c>0&&(t.style.paddingRight=`${c}px`)}function k(t,n){const i=function(t){const n=e.miniOfferInsertBeforeSelector,i=null!=n?String(n).trim():"";if(!i)return null;try{return t.querySelector(i)}catch(e){return console.warn("[LinkItBe] miniOfferInsertBeforeSelector invalid:",i,e),null}}(t);if(i&&t.contains(i)&&i!==n)try{const e=i.parentNode;if(!e)return void t.appendChild(n);e.insertBefore(n,i)}catch(e){console.warn("[LinkItBe] mini-offer insertBefore failed, falling back to append",e),t.appendChild(n)}else t.appendChild(n)}function v(t,n,i,o,d,u){if(!i){console.warn("[LinkItBe] Content block not provided");return{appliedCount:0,integrations:l(n)}}const f=e.showPreview?function(){const e=document.createElement("div");return e.id="__lb_preview",e.style.cssText="\n position: fixed;\n display: none;\n background: white;\n border-radius: 12px;\n box-shadow: 0 4px 20px rgba(0,0,0,0.15);\n padding: 12px;\n z-index: 99999;\n width: 180px;\n font-family: Manrope, sans-serif;\n pointer-events: none;\n ",document.body.appendChild(e),e}():null,p=`\n color:${o.link_color};\n font-weight:${o.link_weight};\n text-decoration:${o.link_decoration};\n cursor:pointer;\n `,y=l(n);let v=0,x=y.filter(e=>"keyword"===e.type);for(;x.length;){const e=[];for(const t of x){const n=w(i,t);n&&e.push({integ:t,spanStart:n.span.start})}if(!e.length){x.forEach(e=>console.warn("[LinkItBe] unresolved keyword:",e.keyword));break}e.sort((e,t)=>t.spanStart-e.spanStart);const{integ:t}=e[0];g(i,t,p)&&(v+=1),x=x.filter(e=>e!==t)}return y.filter(e=>"mini-offer"===e.type).forEach(e=>{!function(e,t,n,i,r){if(!e)return console.warn("[LinkItBe] mini-offer: no content block"),!1;const o=document.createElement("p");o.className="__lb_mini_offer",o.style.cssText="margin-top:16px;font-size:15px;font-family:Manrope, sans-serif;color:#343b4c;";const l=m(i,t,r),s=document.createElement("span"),a=null!=t.text?String(t.text):"",c=null!=t.anchor_text?String(t.anchor_text):"",d=a.indexOf(b),u=d>=0?a.slice(0,d):a,f=d>=0?a.slice(d+13):"";u&&s.appendChild(document.createTextNode(u));const p=h(t,c,n);return p?(s.appendChild(p),f&&s.appendChild(document.createTextNode(f)),l&&"before_phrase"===l.position&&o.appendChild(l.node),o.appendChild(s),l&&"after_phrase"===l.position&&(o.appendChild(document.createElement("br")),o.appendChild(l.node)),k(e,o),_(o,e),!0):(console.warn("[LinkItBe] mini-offer invalid integration link:",t.link),!1)}(i,e,p,d,u)?console.warn("[LinkItBe] mini-offer failed"):v+=1}),f&&(!function(e){if(!e||"1"===e.dataset.lbDismissBound)return;e.dataset.lbDismissBound="1";const t=()=>c(e);document.addEventListener("visibilitychange",t),window.addEventListener("blur",t),window.addEventListener("pagehide",t),window.addEventListener("pageshow",t)}(f),i.querySelectorAll(".__lb_link").forEach(e=>{let n;try{n=JSON.parse(decodeURIComponent(e.dataset.lbIntegration||""))}catch(e){return void console.warn("[LinkItBe] invalid integration payload for preview",e)}if(!n.preview||!n.preview.img)return;const i=()=>c(f);e.addEventListener("mouseenter",i=>{"visible"===document.visibilityState&&(a(f,n,i.clientX,i.clientY,o),"1"!==e.dataset.lbPreviewSent&&t&&(e.dataset.lbPreviewSent="1",r("preview_viewed","/v1/events/preview-viewed",t,n)))}),e.addEventListener("mousemove",e=>{"none"!==f.style.display&&s(f,e.clientX,e.clientY)}),e.addEventListener("mouseleave",i),e.addEventListener("click",i),e.addEventListener("touchstart",i,{passive:!0})})),function(e,t){if(!e)return;const n=e=>{const t=e._lbViewDwellTimer;t&&(clearTimeout(t),e._lbViewDwellTimer=null)},i=new IntersectionObserver(t=>{for(let o=0;o<t.length;o++){const l=t[o],s=l.target;"1"!==s.dataset.lbViewableSent&&(!l.isIntersecting||l.intersectionRatio<.5?(n(s),s._lbViewVisibleSince=null,s._lbViewLastIr=null):(s._lbViewLastIr=String(Math.min(1,Math.max(0,l.intersectionRatio))),s._lbViewVisibleSince||(s._lbViewVisibleSince=Date.now()),n(s),s._lbViewDwellTimer=setTimeout(()=>{if(s._lbViewDwellTimer=null,"1"===s.dataset.lbViewableSent)return;if("visible"!==document.visibilityState)return;let t;try{t=JSON.parse(decodeURIComponent(s.dataset.lbIntegration||""))}catch(e){return void i.unobserve(s)}s.dataset.lbViewableSent="1";const n=s._lbViewVisibleSince||Date.now(),o=Math.max(0,Date.now()-n),l=parseFloat(s._lbViewLastIr||"0.5");r("integration_viewable","/v1/events/integration-viewable",e,t,{tab_visible:"visible"===document.visibilityState,dwell_ms:Math.max(1e3,o),intersection_ratio:l,viewability_rule_version:"elitesdk-1"}),i.unobserve(s)},1e3)))}},{threshold:[.5]});t.querySelectorAll(".__lb_link").forEach(e=>i.observe(e))}(t,i),{appliedCount:v,integrations:y}}async function x(t,n){if(!function(e,t){return e?!!t||(console.warn("[LinkItBe] Content block not found for article:",e),!1):(console.warn("[LinkItBe] Article ID not found"),!1)}(t,n))return;if("1"===n.dataset.lbProcessed)return;const i=await async function(t){const n=`${String(e.apiBaseUrl||"https://api.linkitbe.com").replace(/\/$/,"")}/v1/markup?article_id=${encodeURIComponent(t)}&domain=${encodeURIComponent(e.domain)}`,i={Accept:"application/json"},r=e.partnerApiKey?String(e.partnerApiKey).trim():"";r&&(i["X-Partner-Api-Key"]=r);const o=await fetch(n,{method:"GET",headers:i,credentials:"omit"});return o.ok?await o.json():null}(t);i&&await async function(e,t,n){if("1"===t.dataset.lbProcessed)return;t.dataset.lbProcessed="1";const i=Array.isArray(n.integrations)?n.integrations:[],r=n.config&&"object"==typeof n.config?n.config:{},l=v(e,i,t,r.style||{},r.labeling_template||null,r);await o(e,l.integrations,l.appliedCount)}(t,n,i)}function S(n){if(n.nodeType!==Node.ELEMENT_NODE)return;const i=n.querySelector(e.contentSelector);if(!i)return;x(t(i),i)}function I(e){if(!e)return void console.warn("[LinkItBe] feedRoot not provided for auto markup");new MutationObserver(e=>{for(const t of e)for(let e=0;e<t.addedNodes.length;e++)S(t.addedNodes[e])}).observe(e,{childList:!0,subtree:!0})}async function L(){const n=document.querySelector(e.contentSelector),i=t(n);if(await x(i,n),!0!==e.enableFeedAutoProcess)return;const r=document.querySelector(e.feedObserverRootSelector);r?I(r):console.warn("[LinkItBe] feedObserverRootSelector not found:",e.feedObserverRootSelector)}"loading"===document.readyState?document.addEventListener("DOMContentLoaded",L):!0===e.bootOnLoad&&L(),window.LinkItBe=Object.assign({},window.LinkItBe||{},{initArticle:x,startFeedAutoMarkup:I})}();
|