npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

content-fetch

v1.0.1

Published

ContentFetch is a JavaScript library for fetching and inserting content from one part of a webpage to another, or even from different webpages. It offers a simple API to handle these operations with ease, ensuring the content is sanitised and cached to im

Downloads

94

Readme

ContentFetch

ContentFetch is a JavaScript library for fetching and inserting content from one part of a webpage to another, or even from different webpages. It offers a simple API to handle these operations with ease, ensuring the content is sanitised and cached to improve performance.

NPM Version NPM Downloads License: MIT Known Vulnerabilities Tests Bundle Size GitHub stars

Features

  • Fetch content from the same or different webpages.
  • Insert content into the DOM with different modes (replace, append, prepend).
  • Sanitise fetched content to prevent XSS attacks.
  • Cache fetched content to improve performance.
  • Customisable loading, loaded, and error classes.
  • Debug mode for detailed logging.

Installation

You can install ContentFetch via npm:

npm install content-fetch

Usage

Basic Usage

Install from NPM

import ContentFetch from 'content-fetch'

const contentFetch = new ContentFetch()

// Fetch content from a URL and insert it into a target element
contentFetch.fromTo(
	{
		selector: '#source-element',
		url: 'https://example.com',
	},
	{
		destination: '#target-element',
		mode: 'replace',
	},
)

Install from CDN

<script src="https://cdn.jsdelivr.net/npm/content-fetch/bundled/index.min.js"></script>

Methods

from(params)

Fetches content from a specified URL or the current page.

Parameters:

  • selector (string, required): The CSS selector for the element to fetch.
  • url (string, optional): The URL to fetch content from. Defaults to the current page.
  • includeParent (boolean, optional): Whether to include the parent element in the fetched content. Defaults to false.
  • onStart (function, optional): Callback function to execute when fetching starts.
  • onEnd (function, optional): Callback function to execute when fetching ends.
  • onError (function, optional): Callback function to execute if an error occurs.

Example:

contentFetch.from({
	selector: '#source-element',
	url: 'https://example.com',
	onStart: () => console.log('Fetching started'),
	onEnd: (data) => console.log('Fetching ended', data),
	onError: (error) => console.error('Error fetching content', error),
})

to(params)

Inserts content into a specified destination element.

Parameters:

  • destination (string or HTMLElement, required): The CSS selector or DOM element to insert content into.
  • data (string, required): The HTML content to insert.
  • mode (string, optional): The mode of insertion (replace, append, prepend). Defaults to replace.
  • delay (number, optional): Delay in seconds before inserting content. Defaults to 0.
  • onStart (function, optional): Callback function to execute when insertion starts. Can return a promise to delay the insertion.
  • onEnd (function, optional): Callback function to execute when insertion ends.
  • onError (function, optional): Callback function to execute if an error occurs.

Example:

contentFetch.to({
	destination: '#target-element',
	data: '<p>New content</p>',
	mode: 'append',
	onStart: (destination, data) => console.log('Insertion started', destination, data),
	onEnd: (element) => console.log('Insertion ended', element),
	onError: (error) => console.error('Error inserting content', error),
})

If you wish to animate something inside onStart and delay the rest of the process, you can return a promise from the onStart callback. The insertion will wait until the promise resolves:

Example with Animation:

contentFetch.to({
	destination: content,
	data: defaultHtml.innerHTML,
	mode: 'replace',
	onStart: (destination, data) => {
		return new Promise((resolve) => {
			gsap.to(destination.querySelector('.loading'), 1, {
				opacity: 0,
				x: 30,
				onComplete: resolve,
			})
		})
	},
	onEnd: (element) => {
		animateContent(element)
	},
	onError: (error) => console.error('Error inserting content', error),
})

fromTo(fromParams, toParams)

Fetches content using from and then inserts it using to.

Parameters:

  • fromParams (object, required): Parameters for the from method.
  • toParams (object, required): Parameters for the to method.

Note: when using fromTo() you do not need a data property in the toParams, as it will automatically use the data supplied by fromParams.

Example:

contentFetch.fromTo(
	{
		selector: '#source-element',
		url: 'https://example.com',
	},
	{
		destination: '#target-element',
		mode: 'replace',
	},
)

Options

The ContentFetch constructor accepts an options object to customise behaviour:

| Option | Type | Default | Description | | -------------- | ------- | -------------- | ------------------------------------------- | | loadingClass | string | 'is-loading' | Class added to target element while loading | | loadedClass | string | 'has-loaded' | Class added to target element when loaded | | errorClass | string | 'has-error' | Class added to target element on error | | debugMode | boolean | false | Enables debug logging |

Example:

const contentFetch = new ContentFetch({
	loadingClass: 'loading',
	loadedClass: 'loaded',
	errorClass: 'error',
	debugMode: true,
})

Security Features

ContentFetch includes several security measures:

URL Validation

  • Only absolute URLs are allowed
  • Only http/https protocols are permitted
  • URLs are sanitised before use

Domain Whitelisting

// Development setup
const fetcher = new ContentFetch({
	allowedDomains: ['localhost'], // This will work for all localhost ports
	debugMode: true,
})

// Production setup
const fetcher = new ContentFetch({
	allowedDomains: ['mydomain.com', 'api.mydomain.com'],
})

Setting to localhost will allow:

  • http://localhost:3000
  • http://localhost:8080
  • http://127.0.0.1:3000
  • http://[::1]:8080

Same-Origin Policy

Cross-origin requests require explicit configuration via allowedDomains.

Content Sanitisation

All HTML content is sanitised using DOMPurify.

URL Handling

ContentFetch handles various URL formats:

// Absolute URLs
contentFetch.from({ url: 'http://localhost:3000/page.html' })

// Relative URLs (automatically converted using window.location.origin)
contentFetch.from({ url: '/page.html' })
contentFetch.from({ url: './page.html' })
contentFetch.from({ url: '../page.html' })

// If no URL is provided, current page URL is used
contentFetch.from({ selector: '.content' })

License

ContentFetch is licensed under the MIT License. See the LICENSE file for more details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Author

Written by Elliott Mangham at Code Resolution. Maintained by Code Resolution.