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

dior

v0.0.6

Published

data island only for read

Downloads

18

Readme

Dior (Data Island Only Read)

Dior is a simple library that extracts and manages structured data islands (<script> tags) embedded in HTML pages. Dior can retrieve and manipulate JSON or JSON-LD data embedded in web pages without needing additional network requests. It is designed to be lightweight and intuitive.

Features

  • Extract structured data: Retrieve data embedded in <script> tags of type application/json or application/ld+json.
  • Global access: Access the extracted data via the di object globally on your page.
  • Query string parsing: Automatically parse query string parameters into a global qs object.
  • Automatic updates: Update data islands directly in the DOM by modifying the di object.

Installation

You can use Dior by adding the script directly to your HTML page. No build step is needed.

Example HTML Setup

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Dior Example</title>
</head>
<body>
  <!-- Data Island -->
  <script id="app-data" type="application/json">
  {
    "user": {
      "name": "Alice",
      "role": "admin"
    },
    "theme": "dark"
  }
  </script>

  <!-- Include Dior Script -->
  <script>
    ;(() => {
      globalThis.qs = Object.fromEntries(
        new URLSearchParams(document.location.search)
      )

      globalThis.di = new Proxy(
        Array.from(
          document.querySelectorAll(
            'script[type="application/ld+json"], script[type="application/json"]'
          )
        )
          .map(island => [island.id, JSON.parse(island.text || JSON.stringify(''))])
          .reduce((obj, item) => {
            obj[item[0]] = item[1]
            return obj
          }, {}),
        {
          set: (obj, prop, value) => {
            obj[prop] = value
            var island = document.getElementById(prop)
            island.text = JSON.stringify(value, null, 2)
            return true
          }
        }
      )
    })()
  </script>

  <script>
    // Use Dior to access and update the structured data
    console.log('Data Island:', di['app-data']);
    di['app-data'].theme = 'light';  // Update the theme and DOM
  </script>
</body>
</html>

How It Works

  1. Global Query String (qs): The qs object automatically parses the query parameters from the URL, making them accessible globally.

    Example:

    // URL: example.com?page=home
    console.log(qs.page);  // Output: 'home'
  2. Global Data Island Object (di): Dior automatically retrieves all <script> elements with type="application/json" or type="application/ld+json", parses their contents, and stores them in the global di object, using the script's id as the key.

    Example:

    console.log(di['app-data']);  // Output: { user: { name: 'Alice', role: 'admin' }, theme: 'dark' }
  3. Automatic DOM Updates: You can modify the contents of the di object, and Dior will automatically update the corresponding <script> tag in the DOM.

    Example:

    di['app-data'].theme = 'light';
    // This automatically updates the `app-data` script content in the DOM.

Example Usage

Access Data

You can easily access any data island by its id using the di object.

// Accessing data from the app-data island
const appData = di['app-data'];
console.log(appData.user.name);  // Output: 'Alice'

Update Data

You can also update the data within the script tag, and the DOM will automatically reflect the changes.

// Changing the theme to light mode
di['app-data'].theme = 'light';

This will automatically update the corresponding <script> tag in the DOM:

<script id="app-data" type="application/json">
{
  "user": {
    "name": "Alice",
    "role": "admin"
  },
  "theme": "light"
}
</script>

Access Query Parameters

Dior also parses the query string and makes it accessible via the qs object.

// URL: example.com?user=alice&theme=dark
console.log(qs.user);  // Output: 'alice'
console.log(qs.theme);  // Output: 'dark'

Limitations

  • Dior only works with <script> tags containing application/json or application/ld+json types.
  • It’s meant to handle read and updates of structured data but is not designed for complex data binding or event handling.

Conclusion

Dior provides a lightweight solution for managing structured data islands in web pages, giving you the ability to access and update data directly from the DOM with minimal overhead. It offers simplicity for small-scale applications where data is embedded directly in HTML, reducing the need for additional requests to initialize the app.

Feel free to drop Dior into your web projects for fast, reliable data management on the client side!