@eventvisor/module-pixel 0.0.2
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/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/jest.config.js +5 -0
- package/lib/index.d.ts +5 -0
- package/package.json +45 -0
- package/src/index.ts +93 -0
- package/tsconfig.cjs.json +8 -0
- package/tsconfig.esm.json +8 -0
- package/webpack.config.js +80 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Fahad Heylaal (https://fahad19.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @eventvisor/module-pixel
|
|
2
|
+
|
|
3
|
+
> Pixel module for Eventvisor, for injecting marking and tracking pixels.
|
|
4
|
+
|
|
5
|
+
Visit [https://eventvisor.org/docs/modules/pixel/](https://eventvisor.org/docs/modules/pixel/) for more information.
|
|
6
|
+
|
|
7
|
+
## License
|
|
8
|
+
|
|
9
|
+
MIT © [Fahad Heylaal](https://fahad19.com)
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.EventvisorPixelModule=t():e.EventvisorPixelModule=t()}(this,()=>(()=>{"use strict";var e={};return{166:function(e,t){var r=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))(function(o,a){function i(e){try{u(n.next(e))}catch(e){a(e)}}function l(e){try{u(n.throw(e))}catch(e){a(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(i,l)}u((n=n.apply(e,t||[])).next())})},n=this&&this.__generator||function(e,t){var r,n,o,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},i=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return i.next=l(0),i.throw=l(1),i.return=l(2),"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function l(l){return function(u){return function(l){if(r)throw new TypeError("Generator is already executing.");for(;i&&(i=0,l[0]&&(a=0)),a;)try{if(r=1,n&&(o=2&l[0]?n.return:l[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,l[1])).done)return o;switch(n=0,o&&(l=[2&l[0],o.value]),l[0]){case 0:case 1:o=l;break;case 4:return a.label++,{value:l[1],done:!1};case 5:a.label++,n=l[1],l=[0];continue;case 7:l=a.ops.pop(),a.trys.pop();continue;default:if(!((o=(o=a.trys).length>0&&o[o.length-1])||6!==l[0]&&2!==l[0])){a=0;continue}if(3===l[0]&&(!o||l[1]>o[0]&&l[1]<o[3])){a.label=l[1];break}if(6===l[0]&&a.label<o[1]){a.label=o[1],o=l;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(l);break}o[2]&&a.ops.pop(),a.trys.pop();continue}l=t.call(e,a)}catch(e){l=[6,e],n=0}finally{r=o=0}if(5&l[0])throw l[1];return{value:l[0]?l[1]:void 0,done:!0}}([l,u])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.createPixelModule=function(e){var t=this;void 0===e&&(e={});var o=e.name;return{name:void 0===o?"pixel":o,handle:function(e,o){return r(t,[e,o],void 0,function(e,t){var r,o,a,i,l,u,c,s,f,p,d,h,y,m,b=e.step,v=e.payload,x=t.logger,w=t.sourceResolver;return n(this,function(e){switch(e.label){case 0:if(!(r=b.params))return x.error("[module-pixel] No params found in step",{step:b}),[2];if(!r.snippet)return x.error("[module-pixel] No snippet found in params",{params:r}),[2];if(o=r.selector||"body",!(a=document.querySelector(o)))return x.error("[module-pixel] No element found with selector",{selector:o}),[2];i=r.snippet,l=[];try{l=Array.from(i.matchAll(/\{\{\s*([^}]+)\s*\}\}/g),function(e){return e})}catch(e){x.error("[module-pixel] Error parsing snippet for variable keys",{snippet:i,error:e})}if(!(l.length>0))return[3,4];u=0,c=l,e.label=1;case 1:return u<c.length?(s=c[u],f=s[0],p=s[1],d=p.trim(),[4,w.resolve(d,{payload:v})]):[3,4];case 2:h=e.sent(),i=i.replace(f,h),e.label=3;case 3:return u++,[3,1];case 4:return y=new DOMParser,(m=y.parseFromString(i,"text/html")).querySelectorAll("script").forEach(function(e){var t=document.createElement("script");Array.from(e.attributes).forEach(function(e){t.setAttribute(e.name,e.value)}),t.textContent=e.textContent,a.appendChild(t)}),Array.from(m.body.children).filter(function(e){return"SCRIPT"!==e.tagName}).forEach(function(e){a.appendChild(e.cloneNode(!0))}),[2]}})})}}}}}[166](0,e),e})());
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","mappings":"CAAA,SAA2CA,EAAMC,GAC1B,iBAAZC,SAA0C,iBAAXC,OACxCA,OAAOD,QAAUD,IACQ,mBAAXG,QAAyBA,OAAOC,IAC9CD,OAAO,GAAIH,GACe,iBAAZC,QACdA,QAA+B,sBAAID,IAEnCD,EAA4B,sBAAIC,GACjC,CATD,CASGK,KAAM,I,uBCNLC,EAAsB,CAAC,E,miDCG3B,6BAAkCC,GAAlC,gBAAkC,IAAAA,IAAAA,EAAA,IACxB,MAAmBA,EAAO,KAElC,MAAO,CACLC,UAHU,IAAG,UAAO,EAKpBC,OAAQ,+CAAO,EAAmB,G,gCAAjBC,EAAI,OAAEC,EAAO,UAAMC,EAAM,SAAEC,EAAc,iB,iDAGxD,KAFQC,EAAWJ,EAAI,QAKrB,OAFAE,EAAOG,MAAM,yCAA0C,CAAEL,KAAI,IAE7D,IAGF,IAAKI,EAAOE,QAGV,OAFAJ,EAAOG,MAAM,4CAA6C,CAAED,OAAM,IAElE,IAOF,GAJMG,EAAWH,EAAOG,UAAY,SAE9BC,EAAKC,SAASC,cAAcH,IAKhC,OAFAL,EAAOG,MAAM,gDAAiD,CAAEE,SAAQ,IAExE,IAGED,EAAUF,EAAOE,QAEjBK,EAAoD,GAExD,IACEA,EAAeC,MAAMC,KACnBP,EAAQQ,SAAS,0BACjB,SAACC,GAAe,OAAAA,CAAA,EAEpB,CAAE,MAAOC,GACPd,EAAOG,MAAM,yDAA0D,CACrEC,QAAO,EACPD,MAAOW,GAEX,C,KAEIL,EAAaM,OAAS,GAAtB,Y,IACmC,EAAAN,E,wBAAA,YAA1B,OAACO,EAAO,KAAEC,EAAW,KACxBC,EAASD,EAAYE,OACL,GAAMlB,EAAemB,QAAQF,EAAQ,CAAEnB,QAAO,MAFrB,M,OAEzCsB,EAAgB,SAEtBjB,EAAUA,EAAQY,QAAQA,EAASK,G,wBAJA,I,oBASjCC,EAAS,IAAIC,WACbC,EAAMF,EAAOG,gBAAgBrB,EAAS,cAGxBsB,iBAAiB,UAC7BC,QAAQ,SAACC,GACf,IAAMC,EAAYtB,SAASuB,cAAc,UAGzCpB,MAAMC,KAAKiB,EAAOG,YAAYJ,QAAQ,SAACK,GACrCH,EAAUI,aAAaD,EAAKpC,KAAMoC,EAAKE,MACzC,GAGAL,EAAUM,YAAcP,EAAOO,YAG/B7B,EAAG8B,YAAYP,EACjB,GAGsBnB,MAAMC,KAAKa,EAAIa,KAAKC,UAAUC,OAClD,SAACC,GAAU,MAAkB,WAAlBA,EAAMC,OAAN,GAECd,QAAQ,SAACe,GACrBpC,EAAG8B,YAAYM,EAAQC,WAAU,GACnC,G,UAGN,C,GDxFoB,KAAK,EAAGjD,G","sources":["webpack://EventvisorPixelModule/webpack/universalModuleDefinition","webpack://EventvisorPixelModule/webpack/startup","webpack://EventvisorPixelModule/./src/index.ts"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine([], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"EventvisorPixelModule\"] = factory();\n\telse\n\t\troot[\"EventvisorPixelModule\"] = factory();\n})(this, () => {\nreturn ","// startup\n// Load entry module and return exports\n// This entry module is referenced by other modules so it can't be inlined\nvar __webpack_exports__ = {};\n__webpack_modules__[166](0, __webpack_exports__);\n","import { Module } from \"@eventvisor/sdk\";\n\nexport type PixelModuleOptions = {\n name?: string;\n};\n\nexport function createPixelModule(options: PixelModuleOptions = {}): Module {\n const { name = \"pixel\" } = options;\n\n return {\n name,\n\n handle: async ({ step, payload }, { logger, sourceResolver }) => {\n const { params } = step;\n\n if (!params) {\n logger.error(\"[module-pixel] No params found in step\", { step });\n\n return;\n }\n\n if (!params.snippet) {\n logger.error(\"[module-pixel] No snippet found in params\", { params });\n\n return;\n }\n\n const selector = params.selector || \"body\";\n\n const el = document.querySelector(selector);\n\n if (!el) {\n logger.error(\"[module-pixel] No element found with selector\", { selector });\n\n return;\n }\n\n let snippet = params.snippet;\n\n let variableKeys: [replace: string, source: string][] = [];\n\n try {\n variableKeys = Array.from(\n snippet.matchAll(/\\{\\{\\s*([^}]+)\\s*\\}\\}/g),\n (match: any) => match,\n );\n } catch (e) {\n logger.error(\"[module-pixel] Error parsing snippet for variable keys\", {\n snippet,\n error: e,\n });\n }\n\n if (variableKeys.length > 0) {\n for (const [replace, variableKey] of variableKeys) {\n const source = variableKey.trim();\n const variableValue = await sourceResolver.resolve(source, { payload });\n\n snippet = snippet.replace(replace, variableValue); // @TODO: /g?\n }\n }\n\n // Parse the snippet to handle both script tags and regular HTML\n const parser = new DOMParser();\n const doc = parser.parseFromString(snippet, \"text/html\");\n\n // Handle script tags separately to ensure they execute\n const scripts = doc.querySelectorAll(\"script\");\n scripts.forEach((script) => {\n const newScript = document.createElement(\"script\");\n\n // Copy all attributes from the original script\n Array.from(script.attributes).forEach((attr) => {\n newScript.setAttribute(attr.name, attr.value);\n });\n\n // Copy the script content\n newScript.textContent = script.textContent;\n\n // Append to the target element\n el.appendChild(newScript);\n });\n\n // Handle other HTML content (non-script elements)\n const otherElements = Array.from(doc.body.children).filter(\n (child) => child.tagName !== \"SCRIPT\",\n );\n otherElements.forEach((element) => {\n el.appendChild(element.cloneNode(true));\n });\n },\n };\n}\n"],"names":["root","factory","exports","module","define","amd","this","__webpack_exports__","options","name","handle","step","payload","logger","sourceResolver","params","error","snippet","selector","el","document","querySelector","variableKeys","Array","from","matchAll","match","e","length","replace","variableKey","source","trim","resolve","variableValue","parser","DOMParser","doc","parseFromString","querySelectorAll","forEach","script","newScript","createElement","attributes","attr","setAttribute","value","textContent","appendChild","body","children","filter","child","tagName","element","cloneNode"],"sourceRoot":""}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var e={d:(r,t)=>{for(var o in t)e.o(t,o)&&!e.o(r,o)&&Object.defineProperty(r,o,{enumerable:!0,get:t[o]})},o:(e,r)=>Object.prototype.hasOwnProperty.call(e,r)},r={};e.d(r,{T:()=>o});var t=function(e,r,t,o){return new(t||(t=Promise))(function(n,a){function i(e){try{c(o.next(e))}catch(e){a(e)}}function l(e){try{c(o.throw(e))}catch(e){a(e)}}function c(e){var r;e.done?n(e.value):(r=e.value,r instanceof t?r:new t(function(e){e(r)})).then(i,l)}c((o=o.apply(e,r||[])).next())})};function o(e={}){const{name:r="pixel"}=e;return{name:r,handle:(e,r)=>t(this,[e,r],void 0,function*({step:e,payload:r},{logger:t,sourceResolver:o}){const{params:n}=e;if(!n)return void t.error("[module-pixel] No params found in step",{step:e});if(!n.snippet)return void t.error("[module-pixel] No snippet found in params",{params:n});const a=n.selector||"body",i=document.querySelector(a);if(!i)return void t.error("[module-pixel] No element found with selector",{selector:a});let l=n.snippet,c=[];try{c=Array.from(l.matchAll(/\{\{\s*([^}]+)\s*\}\}/g),e=>e)}catch(e){t.error("[module-pixel] Error parsing snippet for variable keys",{snippet:l,error:e})}if(c.length>0)for(const[e,t]of c){const n=t.trim(),a=yield o.resolve(n,{payload:r});l=l.replace(e,a)}const s=(new DOMParser).parseFromString(l,"text/html");s.querySelectorAll("script").forEach(e=>{const r=document.createElement("script");Array.from(e.attributes).forEach(e=>{r.setAttribute(e.name,e.value)}),r.textContent=e.textContent,i.appendChild(r)}),Array.from(s.body.children).filter(e=>"SCRIPT"!==e.tagName).forEach(e=>{i.appendChild(e.cloneNode(!0))})})}}const n=r.T;export{n as createPixelModule};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","mappings":"AACA,IAAIA,EAAsB,CCA1BA,EAAwB,CAACC,EAASC,KACjC,IAAI,IAAIC,KAAOD,EACXF,EAAoBI,EAAEF,EAAYC,KAASH,EAAoBI,EAAEH,EAASE,IAC5EE,OAAOC,eAAeL,EAASE,EAAK,CAAEI,YAAY,EAAMC,IAAKN,EAAWC,MCJ3EH,EAAwB,CAACS,EAAKC,IAAUL,OAAOM,UAAUC,eAAeC,KAAKJ,EAAKC,I,4TCM3E,SAASI,EAAkBC,EAA8B,CAAC,GAC/D,MAAM,KAAEC,EAAO,SAAYD,EAE3B,MAAO,CACLC,OAEAC,OAAQ,OAAyD,EAAD,6BAAjD,KAAEC,EAAI,QAAEC,IAAW,OAAEC,EAAM,eAAEC,IAC1C,MAAM,OAAEC,GAAWJ,EAEnB,IAAKI,EAGH,YAFAF,EAAOG,MAAM,yCAA0C,CAAEL,SAK3D,IAAKI,EAAOE,QAGV,YAFAJ,EAAOG,MAAM,4CAA6C,CAAED,WAK9D,MAAMG,EAAWH,EAAOG,UAAY,OAE9BC,EAAKC,SAASC,cAAcH,GAElC,IAAKC,EAGH,YAFAN,EAAOG,MAAM,gDAAiD,CAAEE,aAKlE,IAAID,EAAUF,EAAOE,QAEjBK,EAAoD,GAExD,IACEA,EAAeC,MAAMC,KACnBP,EAAQQ,SAAS,0BAChBC,GAAeA,EAEpB,CAAE,MAAOC,GACPd,EAAOG,MAAM,yDAA0D,CACrEC,UACAD,MAAOW,GAEX,CAEA,GAAIL,EAAaM,OAAS,EACxB,IAAK,MAAOC,EAASC,KAAgBR,EAAc,CACjD,MAAMS,EAASD,EAAYE,OACrBC,QAAsBnB,EAAeoB,QAAQH,EAAQ,CAAEnB,YAE7DK,EAAUA,EAAQY,QAAQA,EAASI,EACrC,CAIF,MACME,GADS,IAAIC,WACAC,gBAAgBpB,EAAS,aAG5BkB,EAAIG,iBAAiB,UAC7BC,QAASC,IACf,MAAMC,EAAYrB,SAASsB,cAAc,UAGzCnB,MAAMC,KAAKgB,EAAOG,YAAYJ,QAASK,IACrCH,EAAUI,aAAaD,EAAKnC,KAAMmC,EAAKE,SAIzCL,EAAUM,YAAcP,EAAOO,YAG/B5B,EAAG6B,YAAYP,KAIKlB,MAAMC,KAAKW,EAAIc,KAAKC,UAAUC,OACjDC,GAA4B,WAAlBA,EAAMC,SAELd,QAASe,IACrBnC,EAAG6B,YAAYM,EAAQC,WAAU,KAErC,GAEJ,C","sources":["webpack://@eventvisor/module-pixel/webpack/bootstrap","webpack://@eventvisor/module-pixel/webpack/runtime/define property getters","webpack://@eventvisor/module-pixel/webpack/runtime/hasOwnProperty shorthand","webpack://@eventvisor/module-pixel/./src/index.ts"],"sourcesContent":["// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","import { Module } from \"@eventvisor/sdk\";\n\nexport type PixelModuleOptions = {\n name?: string;\n};\n\nexport function createPixelModule(options: PixelModuleOptions = {}): Module {\n const { name = \"pixel\" } = options;\n\n return {\n name,\n\n handle: async ({ step, payload }, { logger, sourceResolver }) => {\n const { params } = step;\n\n if (!params) {\n logger.error(\"[module-pixel] No params found in step\", { step });\n\n return;\n }\n\n if (!params.snippet) {\n logger.error(\"[module-pixel] No snippet found in params\", { params });\n\n return;\n }\n\n const selector = params.selector || \"body\";\n\n const el = document.querySelector(selector);\n\n if (!el) {\n logger.error(\"[module-pixel] No element found with selector\", { selector });\n\n return;\n }\n\n let snippet = params.snippet;\n\n let variableKeys: [replace: string, source: string][] = [];\n\n try {\n variableKeys = Array.from(\n snippet.matchAll(/\\{\\{\\s*([^}]+)\\s*\\}\\}/g),\n (match: any) => match,\n );\n } catch (e) {\n logger.error(\"[module-pixel] Error parsing snippet for variable keys\", {\n snippet,\n error: e,\n });\n }\n\n if (variableKeys.length > 0) {\n for (const [replace, variableKey] of variableKeys) {\n const source = variableKey.trim();\n const variableValue = await sourceResolver.resolve(source, { payload });\n\n snippet = snippet.replace(replace, variableValue); // @TODO: /g?\n }\n }\n\n // Parse the snippet to handle both script tags and regular HTML\n const parser = new DOMParser();\n const doc = parser.parseFromString(snippet, \"text/html\");\n\n // Handle script tags separately to ensure they execute\n const scripts = doc.querySelectorAll(\"script\");\n scripts.forEach((script) => {\n const newScript = document.createElement(\"script\");\n\n // Copy all attributes from the original script\n Array.from(script.attributes).forEach((attr) => {\n newScript.setAttribute(attr.name, attr.value);\n });\n\n // Copy the script content\n newScript.textContent = script.textContent;\n\n // Append to the target element\n el.appendChild(newScript);\n });\n\n // Handle other HTML content (non-script elements)\n const otherElements = Array.from(doc.body.children).filter(\n (child) => child.tagName !== \"SCRIPT\",\n );\n otherElements.forEach((element) => {\n el.appendChild(element.cloneNode(true));\n });\n },\n };\n}\n"],"names":["__webpack_require__","exports","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","createPixelModule","options","name","handle","step","payload","logger","sourceResolver","params","error","snippet","selector","el","document","querySelector","variableKeys","Array","from","matchAll","match","e","length","replace","variableKey","source","trim","variableValue","resolve","doc","DOMParser","parseFromString","querySelectorAll","forEach","script","newScript","createElement","attributes","attr","setAttribute","value","textContent","appendChild","body","children","filter","child","tagName","element","cloneNode"],"sourceRoot":""}
|
package/jest.config.js
ADDED
package/lib/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@eventvisor/module-pixel",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Eventvisor pixel module",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"import": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"transpile": "echo 'Nothing to transpile'",
|
|
18
|
+
"dist": "webpack --config ./webpack.config.js && tsc --project tsconfig.esm.json --declaration --emitDeclarationOnly --declarationDir dist",
|
|
19
|
+
"build": "npm run transpile && npm run dist",
|
|
20
|
+
"testJest": "jest --config jest.config.js --verbose --coverage",
|
|
21
|
+
"test": "echo 'Nothing to test'"
|
|
22
|
+
},
|
|
23
|
+
"author": {
|
|
24
|
+
"name": "Fahad Heylaal",
|
|
25
|
+
"url": "https://fahad19.com"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://eventvisor.org",
|
|
28
|
+
"keywords": [],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/eventvisor/eventvisor.git"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public",
|
|
35
|
+
"registry": "https://registry.npmjs.org/"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/eventvisor/eventvisor/issues"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@eventvisor/sdk": "0.0.2"
|
|
42
|
+
},
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"gitHead": "373d5c00cb6fa0d6f4baaa05a10e57e505474847"
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Module } from "@eventvisor/sdk";
|
|
2
|
+
|
|
3
|
+
export type PixelModuleOptions = {
|
|
4
|
+
name?: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export function createPixelModule(options: PixelModuleOptions = {}): Module {
|
|
8
|
+
const { name = "pixel" } = options;
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
name,
|
|
12
|
+
|
|
13
|
+
handle: async ({ step, payload }, { logger, sourceResolver }) => {
|
|
14
|
+
const { params } = step;
|
|
15
|
+
|
|
16
|
+
if (!params) {
|
|
17
|
+
logger.error("[module-pixel] No params found in step", { step });
|
|
18
|
+
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!params.snippet) {
|
|
23
|
+
logger.error("[module-pixel] No snippet found in params", { params });
|
|
24
|
+
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const selector = params.selector || "body";
|
|
29
|
+
|
|
30
|
+
const el = document.querySelector(selector);
|
|
31
|
+
|
|
32
|
+
if (!el) {
|
|
33
|
+
logger.error("[module-pixel] No element found with selector", { selector });
|
|
34
|
+
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let snippet = params.snippet;
|
|
39
|
+
|
|
40
|
+
let variableKeys: [replace: string, source: string][] = [];
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
variableKeys = Array.from(
|
|
44
|
+
snippet.matchAll(/\{\{\s*([^}]+)\s*\}\}/g),
|
|
45
|
+
(match: any) => match,
|
|
46
|
+
);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
logger.error("[module-pixel] Error parsing snippet for variable keys", {
|
|
49
|
+
snippet,
|
|
50
|
+
error: e,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (variableKeys.length > 0) {
|
|
55
|
+
for (const [replace, variableKey] of variableKeys) {
|
|
56
|
+
const source = variableKey.trim();
|
|
57
|
+
const variableValue = await sourceResolver.resolve(source, { payload });
|
|
58
|
+
|
|
59
|
+
snippet = snippet.replace(replace, variableValue); // @TODO: /g?
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Parse the snippet to handle both script tags and regular HTML
|
|
64
|
+
const parser = new DOMParser();
|
|
65
|
+
const doc = parser.parseFromString(snippet, "text/html");
|
|
66
|
+
|
|
67
|
+
// Handle script tags separately to ensure they execute
|
|
68
|
+
const scripts = doc.querySelectorAll("script");
|
|
69
|
+
scripts.forEach((script) => {
|
|
70
|
+
const newScript = document.createElement("script");
|
|
71
|
+
|
|
72
|
+
// Copy all attributes from the original script
|
|
73
|
+
Array.from(script.attributes).forEach((attr) => {
|
|
74
|
+
newScript.setAttribute(attr.name, attr.value);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// Copy the script content
|
|
78
|
+
newScript.textContent = script.textContent;
|
|
79
|
+
|
|
80
|
+
// Append to the target element
|
|
81
|
+
el.appendChild(newScript);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Handle other HTML content (non-script elements)
|
|
85
|
+
const otherElements = Array.from(doc.body.children).filter(
|
|
86
|
+
(child) => child.tagName !== "SCRIPT",
|
|
87
|
+
);
|
|
88
|
+
otherElements.forEach((element) => {
|
|
89
|
+
el.appendChild(element.cloneNode(true));
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const path = require("path");
|
|
2
|
+
|
|
3
|
+
module.exports = [
|
|
4
|
+
// cjs
|
|
5
|
+
{
|
|
6
|
+
entry: {
|
|
7
|
+
"index.cjs": path.join(__dirname, "src", "index.ts"),
|
|
8
|
+
},
|
|
9
|
+
output: {
|
|
10
|
+
path: path.join(__dirname, "dist"),
|
|
11
|
+
filename: "index.js",
|
|
12
|
+
library: "EventvisorPixelModule",
|
|
13
|
+
libraryTarget: "umd",
|
|
14
|
+
globalObject: "this",
|
|
15
|
+
},
|
|
16
|
+
mode: "production",
|
|
17
|
+
devtool: "source-map",
|
|
18
|
+
resolve: {
|
|
19
|
+
extensions: [".ts", ".tsx", ".js"],
|
|
20
|
+
},
|
|
21
|
+
module: {
|
|
22
|
+
rules: [
|
|
23
|
+
{
|
|
24
|
+
test: /\.(ts|tsx)$/,
|
|
25
|
+
exclude: /(node_modules)/,
|
|
26
|
+
use: [
|
|
27
|
+
{
|
|
28
|
+
loader: "ts-loader",
|
|
29
|
+
options: {
|
|
30
|
+
configFile: path.join(__dirname, "tsconfig.cjs.json"),
|
|
31
|
+
transpileOnly: true,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
performance: {
|
|
39
|
+
hints: false,
|
|
40
|
+
},
|
|
41
|
+
optimization: {
|
|
42
|
+
minimize: true,
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
// esm
|
|
47
|
+
{
|
|
48
|
+
entry: path.join(__dirname, "src", "index.ts"),
|
|
49
|
+
output: {
|
|
50
|
+
path: path.join(__dirname, "dist"),
|
|
51
|
+
filename: "index.mjs",
|
|
52
|
+
library: {
|
|
53
|
+
type: "module",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
experiments: {
|
|
57
|
+
outputModule: true,
|
|
58
|
+
},
|
|
59
|
+
mode: "production",
|
|
60
|
+
devtool: "source-map",
|
|
61
|
+
resolve: {
|
|
62
|
+
extensions: [".ts", ".tsx", ".js"],
|
|
63
|
+
},
|
|
64
|
+
module: {
|
|
65
|
+
rules: [
|
|
66
|
+
{
|
|
67
|
+
test: /\.(ts|tsx)$/,
|
|
68
|
+
exclude: /(node_modules)/,
|
|
69
|
+
loader: "ts-loader",
|
|
70
|
+
options: {
|
|
71
|
+
configFile: path.join(__dirname, "tsconfig.esm.json"),
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
performance: {
|
|
77
|
+
hints: false,
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
];
|