UNPKG

affiliate

Version:

A platform agnostic tool to quickly add affiliate links onto your website

2 lines 4.75 kB
(()=>{"use strict";var t={d:(e,s)=>{for(var o in s)t.o(s,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:s[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>u});const s=/WHERE (.+?) SET (.+?)\s*(?:AND|$)/g,o=()=>{const t=document.getElementById("aff-js");if("object"==typeof t&&t){var e;const o=null==t||null===(e=t.dataset)||void 0===e?void 0:e.autoAffiliate;if("string"==typeof o){const t=[],e=o.match(s);if(!e)return;return Object.values(e).forEach((e=>{const o=s.exec(e);if(!o||3!==o.length)return;const r=o[1],i=o[2];t.push({hosts:r.split(",").map((t=>t.trim())),query:i.split(",").reduce(((t,e)=>{const[s,o]=e.split("=");return s&&o&&(t[s.trim()]=o.trim()),t}),{})})})),{tags:t}}}},r="object"==typeof window&&!(void 0===window.MutationObserver),i="function"==typeof URL&&"function"==typeof URLSearchParams;function n(t,...e){const s="[Affiliate] ";"object"==typeof console&&(t?console.error(s,...e):console.log(s,...e))}const a="_aff",c=t=>{var e;return null!==(e=t._aff)&&void 0!==e?e:{}},h=(t,e)=>{Object.assign(t,{[a]:e})};function f(t,e,s){return e in t?Object.defineProperty(t,e,{value:s,enumerable:!0,configurable:!0,writable:!0}):t[e]=s,t}const l=class{traverse(t=document.body){if("object"!=typeof t||"function"!=typeof t.getElementsByTagName)return this;if(!i)return this.log(!0,"This browser needs a URL polyfill."),this;this.log(!1,"Traversing DOM...");const e=t.getElementsByTagName("a");let s=Object.values(e);return"a"===t.nodeName.toLowerCase()&&(s=[t]),this.log(!1,`Found ${s.length+1} nodes...`),s.forEach((t=>{if(!t||!("href"in t))return;var e;const s=new URL(null!==(e=t.href)&&void 0!==e?e:"",window.location.origin);this.state.hosts.includes(s.host)&&this.modifyURL(s,t)})),this}constructor(t){var e;this.state={attached:!1,config:{tags:[]},hosts:[]},this.observer=void 0,this.modifyURL=(t,e)=>{const s=c(e);if(s.is&&s.is===t.href)return;const o=t.href;this.log(!1,"Discovered URL: "+t.href);const r=this.convert(t);e.href=r,h(e,{was:o,is:t.href})},this.convert=t=>{if("object"==typeof t&&(t=t.href),!i)return this.log(!0,"This browser needs a URL polyfill."),t;const e=new URL(t,window.location.origin);if(!this.state.hosts.includes(e.host))return e.href;for(const t of this.state.config.tags)if(t.hosts.includes(e.host)){var s,o;if(t.query)Object.keys(null!==(o=t.query)&&void 0!==o?o:{}).forEach((s=>{"object"==typeof t.query&&e.searchParams.set(s,t.query[s])}));if("function"==typeof t.modify)try{let s=t.modify(e);"object"==typeof s&&(s=s.href),e.href=s}catch(t){n(!0,t)}return null===(s=t.replace)||void 0===s||s.forEach((t=>{e.href=e.href.replace(t.from,t.to)})),e.href}return e.href},this.attach=()=>{if(this.state.attached||"undefined"==typeof document)return this;const{readyState:t}=document;return"complete"===t||"interactive"===t?(this.state.attached=!0,this.traverse(),r&&this.observer?this.observer.observe(document.body,{childList:!0,subtree:!0,attributes:!0,characterData:!0,attributeFilter:["href"]}):this.log(!1,"Browser does not support MutationObserver.")):window.addEventListener("DOMContentLoaded",this.attach),this},this.detach=()=>r&&this.observer?(this.state.attached=!1,this.observer.disconnect(),this.log(!1,"Observer disconnected."),this):this,(t=null!=t?t:{}).tags=null!==(e=t.tags)&&void 0!==e?e:[],t.tags.map(((e,s)=>{t&&t.tags&&("string"==typeof e.hosts&&(e.hosts=[e.hosts]),t.tags[s]=function(t){for(var e=1;e<arguments.length;e++){var s=null!=arguments[e]?arguments[e]:{},o=Object.keys(s);"function"==typeof Object.getOwnPropertySymbols&&(o=o.concat(Object.getOwnPropertySymbols(s).filter((function(t){return Object.getOwnPropertyDescriptor(s,t).enumerable})))),o.forEach((function(e){f(t,e,s[e])}))}return t}({query:{},replace:[]},e),this.state.hosts=[...this.state.hosts,...t.tags[s].hosts])})),this.log=t.log?n:()=>{},this.log(!1,"New Instance",t),r&&(this.observer=new window.MutationObserver((t=>{let e=!1;t.forEach((t=>{if("attributes"===t.type){if("href"!==t.attributeName)return;const e=t.target.href,s=c(t.target);if(s.is&&s.is===e)return}e||(this.log(!1,"DOM Mutation",t),e=!0),this.traverse(t.target)}))}))),this.state.config=t}};const u=new class{get instances(){return[...this.state.instances]}constructor(){this.state={instances:[]},this.create=t=>{const e=new l(t);return this.state.instances.push(e),e},this.detachAll=()=>{this.state.instances.forEach((t=>t.detach()))},this.revert=()=>{this.detachAll();Object.values(document.body.getElementsByTagName("a")).forEach((t=>{const e=c(t);e&&"string"==typeof e.was&&(t.href=e.was,h(t,{}))}))};try{const t=o();if("object"==typeof t){const e=this.create(t);n(!1,e),this.state.auto=e,e.attach()}}catch(t){n(!0,t)}}};window.Affiliate=e.default})(); //# sourceMappingURL=affiliate.web.js.map