
Security News
RubyGems Adds Cooldown Feature to Bundler for Newly Published Gems
RubyGems and Bundler 4.0.13 introduced an opt-in cooldown feature that delays newly published gems during dependency resolution.
@ecopages/scripts-injector
Advanced tools
The Scripts Injector is a custom element designed to dynamically load scripts into your web page. It provides a way to load scripts based on certain conditions and events.
The Scripts Injector is a custom element designed to dynamically load scripts into your web page. It provides a way to load scripts based on certain conditions and events.
To use the Scripts Injector, you need to include it in your HTML:
<scripts-injector></scripts-injector>
<scripts-injector scripts="/analytics.js" on:idle>
<div>Content</div>
</scripts-injector>
<scripts-injector scripts="/heavy-chart.js" on:interaction="mouseenter,focusin">
<button>Open chart</button>
</scripts-injector>
<scripts-injector>
<script type="ecopages/injector-map">
{
"on:idle": { "scripts": ["/_assets/tracker.js"] },
"on:interaction": {
"value": "click",
"scripts": ["/_assets/heavy-ui.js"]
}
}
</script>
<div class="my-component">...</div>
</scripts-injector>
<script type="ecopages/global-injector-map">
{
"analytics-trigger": {
"on:idle": { "scripts": ["/_assets/tracker.js"] }
},
"card-trigger": {
"on:interaction": {
"value": "click",
"scripts": ["/_assets/card-interactions.js"]
}
}
}
</script>
data-eco-trigger<div data-eco-trigger="analytics-trigger"></div>
<button data-eco-trigger="card-trigger">Open Card</button>
import { initGlobalInjector } from '@ecopages/scripts-injector/global';
const injector = initGlobalInjector();
// Call after SPA route changes / page swaps when new trigger-map tags or
// new [data-eco-trigger] elements are inserted by your framework.
injector.refresh();
// Call during app teardown if needed.
injector.cleanup();
initGlobalInjector() returns a handle with:
refresh() — re-parse all global maps and bind any unbound elements with data-eco-triggercleanup() — disconnect observers and disable further refresh work for that instanceThe ScriptInjectorProps type defines the properties that can be used to control the behavior of the scripts-injector custom element. Here are the available properties:
"on:idle": This optional boolean property determines whether the script should be loaded once the DOM is ready. For example, <scripts-injector on:idle></scripts-injector> will load the script as soon as the DOM is ready.
"on:interaction": This optional property allows you to specify a series of events that will trigger the loading of the script. It can be either "touchstart,click" or "mouseenter,focusin". For example, <scripts-injector on:interaction="mouseenter, focusin"></scripts-injector> will load the script when the user either mouses over the element or focuses on it.
"on:visible": This optional property can be either a string or a boolean. If it's a string, it should specify the root margin for an Intersection Observer that will load the script when the element comes into view. If it's a boolean and it's true, the script will be loaded when the element comes into view with a default root margin. For example, <scripts-injector on:visible="50px 1px"></scripts-injector> will load the script when the element comes into view, with a root margin of 50px 1px.
scripts: This property should be a comma-separated string of scripts to be loaded. For example, <scripts-injector scripts="script1.js,script2.js"></scripts-injector> will load script1.js and script2.js.
For all interaction events, the Scripts Injector intercepts the initial event, prevents the default action, loads the required script, and then replays the event with all original properties preserved. This ensures that user actions are never lost, even if they happen before the script is fully loaded.
.click() for best compatibility with links and formsclientX, clientY, screenX, screenY, button, buttons, and modifier keyskey, code, location, repeat, and modifier keysrelatedTargettouches, targetTouches, changedTouches, and modifier keysMultiple script injectors requesting the same script will coordinate to prevent duplicate loading. A static registry tracks scripts currently being loaded, ensuring only one network request is made even when multiple injectors trigger simultaneously.
After loading, the element receives a data-load-reason attribute indicating what triggered the load:
idle - Loaded via on:idlevisible - Loaded via on:visibleinteraction:click, interaction:mouseenter, etc. - Loaded via the specific interaction eventIf any scripts fail to load, the element receives a data-error attribute containing the comma-separated list of failed script URLs. The DATA_LOADED event also includes a failedScripts array in its detail.
// Listen for load completion with error info
document.addEventListener('data-loaded', (event) => {
const { loadedScripts, failedScripts } = event.detail;
if (failedScripts.length > 0) {
console.warn('Some scripts failed to load:', failedScripts);
}
});
This passage provides a standard use case for the custom element. The script is designed to load when the user interacts with it, either through mouse entry or a focus event. If multiple script injectors are present with the same script, only the first one will execute the load. This is because once a script is loaded, all script injectors are notified and subsequently remove the script from their loading responsibilities.
<scripts-injector on:interaction="mouseenter,focusin" scripts={['path/to/my/element']}>
<lit-counter class="lit-counter" count={8}></lit-counter>
</scripts-injector>
To avoid deeply nested <scripts-injector> instances when different scripts inside a single component require different triggers, you can use the JSON map configuration approach. This allows you to flatten the DOM structure and configure multiple triggers simultaneously on a single injector element:
<scripts-injector>
<script type="ecopages/injector-map">
{
"on:idle": { "scripts": ["/_assets/tracker.js"] },
"on:interaction": {
"value": "click",
"scripts": ["/_assets/heavy-ui.js"]
}
}
</script>
<!-- Component Content -->
<div class="my-component">...</div>
</scripts-injector>
The on:interaction prop supports a wide range of DOM events, as defined in InteractionEvent.
click, dblclick, mousedown, mouseup, mouseenter, mouseleave, mousemove, mouseover, mouseouttouchstart, touchend, touchmove, touchcancelfocus, blur, focusin, focusoutkeydown, keypress, keyupinput, change, submitscroll, resizeAfter script loading completes, the following attributes are set on the element:
| Attribute | Description |
|---|---|
data-loaded | Present when all scripts have been loaded (or attempted) |
data-load-reason | The trigger that caused scripts to load (idle, visible, interaction:<event>) |
data-error | Comma-separated list of script URLs that failed to load (only if errors occurred) |
This is undeniably heavily influenced by 11ty/is-land, Astro island and the "component island" concept, originally introduced by Etsy's frontend architect, Katie Sylor-Miller.
FAQs
The Scripts Injector is a custom element designed to dynamically load scripts into your web page. It provides a way to load scripts based on certain conditions and events.
We found that @ecopages/scripts-injector demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
RubyGems and Bundler 4.0.13 introduced an opt-in cooldown feature that delays newly published gems during dependency resolution.

Security News
pnpm 11.5 now recognizes npm staged publish approvals in release metadata, preventing those releases from being mistaken for lower-trust package publishes.

Security News
Federal audit finds NIST lacked a plan to clear the NVD backlog, wasted funds on duplicate work, and delayed use of CISA data.