UNPKG

online-sales-sdk

Version:

OnlineSales.ai Web Tag SDK 封装,适用于 Vue/React 项目,纯JS实现

4 lines (3 loc) 7.14 kB
"use strict";function e(e,t,r,n,o,i,c){try{var a=e[i](c),u=a.value}catch(e){return void r(e)}a.done?t(u):Promise.resolve(u).then(n,o)}function t(t){return function(){var r=this,n=arguments;return new Promise(function(o,i){var c=t.apply(r,n);function a(t){e(c,o,i,a,u,"next",t)}function u(t){e(c,o,i,a,u,"throw",t)}a(void 0)})}}function r(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,i(n.key),n)}}function n(){ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e,t,r="function"==typeof Symbol?Symbol:{},i=r.iterator||"@@iterator",c=r.toStringTag||"@@toStringTag";function a(r,n,i,c){var a=n&&n.prototype instanceof s?n:s,f=Object.create(a.prototype);return o(f,"_invoke",function(r,n,o){var i,c,a,s=0,f=o||[],l=!1,d={p:0,n:0,v:e,a:h,f:h.bind(e,4),d:function(t,r){return i=t,c=0,a=e,d.n=r,u}};function h(r,n){for(c=r,a=n,t=0;!l&&s&&!o&&t<f.length;t++){var o,i=f[t],h=d.p,v=i[2];r>3?(o=v===n)&&(a=i[(c=i[4])?5:(c=3,3)],i[4]=i[5]=e):i[0]<=h&&((o=r<2&&h<i[1])?(c=0,d.v=n,d.n=i[1]):h<v&&(o=r<3||i[0]>n||n>v)&&(i[4]=r,i[5]=n,d.n=v,c=0))}if(o||r>1)return u;throw l=!0,n}return function(o,f,v){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&h(f,v),c=f,a=v;(t=c<2?e:a)||!l;){i||(c?c<3?(c>1&&(d.n=-1),h(c,a)):d.n=a:d.v=a);try{if(s=2,i){if(c||(o="next"),t=i[o]){if(!(t=t.call(i,a)))throw TypeError("iterator result is not an object");if(!t.done)return t;a=t.value,c<2&&(c=0)}else 1===c&&(t=i.return)&&t.call(i),c<2&&(a=TypeError("The iterator does not provide a '"+o+"' method"),c=1);i=e}else if((t=(l=d.n<0)?a:r.call(n,d))!==u)break}catch(t){i=e,c=1,a=t}finally{s=1}}return{value:t,done:l}}}(r,i,c),!0),f}var u={};function s(){}function f(){}function l(){}t=Object.getPrototypeOf;var d=[][i]?t(t([][i]())):(o(t={},i,function(){return this}),t),h=l.prototype=s.prototype=Object.create(d);function v(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,l):(e.__proto__=l,o(e,c,"GeneratorFunction")),e.prototype=Object.create(h),e}return f.prototype=l,o(h,"constructor",l),o(l,"constructor",f),f.displayName="GeneratorFunction",o(l,c,"GeneratorFunction"),o(h),o(h,c,"Generator"),o(h,i,function(){return this}),o(h,"toString",function(){return"[object Generator]"}),(n=function(){return{w:a,m:v}})()}function o(e,t,r,n){var i=Object.defineProperty;try{i({},"",{})}catch(e){i=0}o=function(e,t,r,n){if(t)i?i(e,t,{value:r,enumerable:!n,configurable:!n,writable:!n}):e[t]=r;else{function c(t,r){o(e,t,function(e){return this._invoke(t,r,e)})}c("next",0),c("throw",1),c("return",2)}},o(e,t,r,n)}function i(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}function c(e){return new Promise(function(t,r){if("undefined"==typeof window)return r(new Error("Not in browser"));if(window.osmos)return t(window.osmos);if(document.querySelector("script[data-osmos-sdk]")){var n=setInterval(function(){window.osmos&&(clearInterval(n),t(window.osmos))},50);setTimeout(function(){clearInterval(n),r(new Error("osmos SDK 加载超时"))},5e3)}else{var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src=e,o.setAttribute("data-osmos-sdk","1"),o.onload=function(){var e=setInterval(function(){window.osmos&&(clearInterval(e),t(window.osmos))},50);setTimeout(function(){clearInterval(e),r(new Error("osmos SDK 挂载超时"))},5e3)},o.onerror=function(e){return r(new Error("osmos SDK 加载失败: "+(null==e?void 0:e.message)))},document.head.appendChild(o)}})}var a=function(){return e=function e(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.scriptUrl=t.scriptUrl||"https://c.o-s.io/osmos-web-sdk/sdk.js",this.readyPromise=null,this._inited=!1},o=[{key:"load",value:(f=t(n().m(function e(){return n().w(function(e){for(;;)switch(e.n){case 0:return this.readyPromise||("undefined"!=typeof window?this.readyPromise=c(this.scriptUrl):this.readyPromise=import(this.scriptUrl)),e.n=1,this.readyPromise;case 1:return e.a(2,e.v)}},e,this)})),function(){return f.apply(this,arguments)})},{key:"initialize",value:(s=t(n().m(function e(t){var r;return n().w(function(e){for(;;)switch(e.n){case 0:return e.n=1,this.load();case 1:if(r=e.v){e.n=2;break}throw new Error("osmos SDK 加载失败");case 2:return e.n=3,r.initialize(t);case 3:this._inited=!0;case 4:return e.a(2)}},e,this)})),function(e){return s.apply(this,arguments)})},{key:"_checkInited",value:function(){if(!this._inited)throw new Error("请先调用 initialize() 初始化 SDK")}},{key:"fetchAd",value:(u=t(n().m(function e(t,r){var o,i;return n().w(function(e){for(;;)switch(e.n){case 0:return this._checkInited(),e.n=1,this.load();case 1:if(null!=(i=e.v)&&null!==(o=i.adFetcher)&&void 0!==o&&o[t]){e.n=2;break}throw new Error("方法 ".concat(t," 不存在"));case 2:return e.a(2,i.adFetcher[t](r))}},e,this)})),function(e,t){return u.apply(this,arguments)})},{key:"trackerSdk",value:(a=t(n().m(function e(t,r){var o,i;return n().w(function(e){for(;;)switch(e.n){case 0:return this._checkInited(),e.n=1,this.load();case 1:if(null!=(i=e.v)&&null!==(o=i.tracker)&&void 0!==o&&o[t]){e.n=2;break}throw new Error("方法 ".concat(t," 不存在"));case 2:return e.a(2,i.tracker[t](r))}},e,this)})),function(e,t){return a.apply(this,arguments)})},{key:"registerAdClickEvent",value:function(e){return this.trackerSdk("registerAdClickEvent",e)}},{key:"registerAdImpressionEvent",value:function(e){return this.trackerSdk("registerAdImpressionEvent",e)}},{key:"registerViewProductEvent",value:function(e){return this.trackerSdk("registerViewProductEvent",e)}},{key:"registerAdd2CartEvent",value:function(e){return this.trackerSdk("registerAdd2CartEvent",e)}},{key:"registerPurchaseEvent",value:function(e){return this.trackerSdk("registerPurchaseEvent",e)}},{key:"fetchDisplayAds",value:function(e){return this.fetchAd("fetchDisplayAds",e)}},{key:"fetchPLAProductPageAds",value:function(e){return this.fetchAd("fetchPLAProductPageAds",e)}},{key:"fetchPLASearchPageAds",value:function(e){return this.fetchAd("fetchPLASearchPageAds",e)}},{key:"fetchPLACategoryPageAd",value:function(e){return this.fetchAd("fetchPLACategoryPageAd",e)}},{key:"fetchPLATPAPageAd",value:function(e){return this.fetchAd("fetchPLATPAPageAd",e)}},{key:"fetchPLAPurchasePageAd",value:function(e){return this.fetchAd("fetchPLAPurchasePageAd",e)}},{key:"fetchPLAHomePageAd",value:function(e){return this.fetchAd("fetchPLAHomePageAd",e)}},{key:"fetchPLACustomPageAd",value:function(e){return this.fetchAd("fetchPLACustomPageAd",e)}}],o&&r(e.prototype,o),i&&r(e,i),Object.defineProperty(e,"prototype",{writable:!1}),e;var e,o,i,a,u,s,f}();var u=null;module.exports=function(e){return u||(u=new a(e)),u};