@ecopages/scripts-injector 0.1.1 → 0.1.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/package.json +11 -3
- package/scripts-injector.js +1 -1
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ecopages/scripts-injector",
|
|
3
3
|
"main": "scripts-injector.js",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "types.d.ts",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/ecopages/scripts-injector"
|
|
10
|
+
},
|
|
7
11
|
"exports": {
|
|
8
12
|
"./package.json": {
|
|
9
13
|
"import": "./package.json"
|
|
@@ -15,10 +19,14 @@
|
|
|
15
19
|
"./types": "./types.d.ts"
|
|
16
20
|
},
|
|
17
21
|
"scripts": {
|
|
18
|
-
"build:lib": "rm -rf dist && bun build --entry scripts-injector.ts --outdir . --minify"
|
|
22
|
+
"build:lib": "rm -rf dist && bun build --entry scripts-injector.ts --outdir . --minify",
|
|
23
|
+
"test": "vitest",
|
|
24
|
+
"test:run": "vitest run"
|
|
19
25
|
},
|
|
20
26
|
"devDependencies": {
|
|
21
|
-
"@types/bun": "latest"
|
|
27
|
+
"@types/bun": "latest",
|
|
28
|
+
"@vitest/browser-playwright": "^4.0.16",
|
|
29
|
+
"vitest": "^4.0.16"
|
|
22
30
|
},
|
|
23
31
|
"peerDependencies": {
|
|
24
32
|
"typescript": "^5.0.0"
|
package/scripts-injector.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var B;((k)=>k.DATA_LOADED="data-loaded")(B||={});var C=["on:visible","on:idle","on:interaction"];class w extends HTMLElement{intersectionObserver=null;scriptsToLoad=[];registeredEvents=[];conditionsMap={"on:visible":this.onVisible.bind(this),"on:idle":this.onIdle.bind(this),"on:interaction":this.onInteraction.bind(this)};constructor(){super();this.loadScripts=this.loadScripts.bind(this),this.listenToDataLoaded=this.listenToDataLoaded.bind(this)}connectedCallback(){let h=this.getAttribute("scripts");this.scriptsToLoad=h?h.split(",").map((k)=>k.trim()).filter(Boolean):[],document.addEventListener("data-loaded",this.listenToDataLoaded),this.applyConditions()}disconnectedCallback(){if(document.removeEventListener("data-loaded",this.listenToDataLoaded),this.unregisterEvents(),this.intersectionObserver)this.intersectionObserver.disconnect(),this.intersectionObserver=null}notifyInjectors(){document.dispatchEvent(new CustomEvent("data-loaded",{detail:{loadedScripts:this.scriptsToLoad}}))}applyConditions(){let h=Object.keys(this.conditionsMap);for(let k of h)if(this.hasAttribute(k))this.conditionsMap[k]()}onVisible(){this.setupIntersectionObserver()}onIdle(){this.loadScripts()}onInteraction(){let h=this.getAttribute("on:interaction");if(!h)return;for(let k of h.split(",")){let q=k.trim();if(q)this.addEventListener(q,this.loadScripts),this.registeredEvents.push({type:q,listener:this.loadScripts})}}listenToDataLoaded(h){if(this.hasAttribute("data-loaded"))return;let{loadedScripts:k}=h.detail;if(this.scriptsToLoad=this.scriptsToLoad.filter((q)=>!k.includes(q)),this.scriptsToLoad.length===0)this.setAttribute("data-loaded",""),this.unregisterEvents()}unregisterEvents(){this.intersectionObserver?.disconnect();for(let{type:h,listener:k}of this.registeredEvents)this.removeEventListener(h,k);this.registeredEvents=[]}loadScripts(){if(this.hasAttribute("data-loaded"))return;try{for(let h of this.scriptsToLoad)if(!this.isScriptLoaded(h))this.loadScript(h)}catch(h){console.error("[scripts-injector] Error loading scripts:",h)}finally{this.setAttribute("data-loaded",""),this.unregisterEvents(),this.notifyInjectors()}}isScriptLoaded(h){return document.querySelector(`script[src="${h}"]`)!==null}loadScript(h){let k=document.createElement("script");k.src=h,k.type="module",k.async=!0,k.onerror=(q)=>{console.error(`[scripts-injector] Failed to load script: ${h}`,q)},document.head.appendChild(k)}setupIntersectionObserver(){let h=this.getAttribute("on:visible"),q={rootMargin:h&&h!==""&&h!=="true"?h:"50px 0px",threshold:0.1};this.intersectionObserver=new IntersectionObserver((x)=>{for(let z of x)if(z.isIntersecting)this.loadScripts()},q),this.intersectionObserver.observe(this)}}if(!customElements.get("scripts-injector"))customElements.define("scripts-injector",w);export{C as conditions,w as ScriptsInjector,B as ScriptInjectorEvents};
|