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

@madgex/safeguard

v2.0.3

Published

A JavaScript library that tries to sandbox 3rd party content on a webpage

Downloads

16

Readme

Safeguard

A JavaScript library that tries to sandbox 3rd party content on your webpage.

Sometimes you want to block the cascade in CSS, i.e. you want to inline some HTML and associated CSS in your page but don't want the CSS from the host page to affect it.

Why not iframe it

IFrames were meant for embedding 3rd party content on your page, right? Yes, but there are inherent issues with this:

  • Search engines consider the content in iframes to belong to another website
  • If you want the content to be seamless to your page, it needs to be stand-alone content, i.e. no header/footer/nav etc.
  • IFrames don't resize to their content

How does Safeguard do it differently

Wrap any content in a <safe-guard> custom element and watch as Safeguard yanks the content into either a ShadowDOM or a generated, src-less iframe.

Demo

https://safeguard.now.sh/

Install

npm install @madgex/safeguard

Getting started

Either import the script of load from unpkg and you're set:

From CDN: Add the following script to the end of your section.

<script src="https://unpkg.com/@madgex/safeguard" defer></script>

From NPM: Install the package from NPM.

// App.js
import '@madgex/safeguard';

That's it. It will initialize itself.

Options

tag

You may have other blocks of HTML/CSS/JS to take along into the SafeGuard, you can tag elements by wrapping them in HTML comments:

<html>
  <head>
    <title>My awesome page</title>

    <!-- [sg:starttag:myWidget] -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
    <!-- [sg:endtag:myWidget] -->

    <link rel="stylesheet" href="/css/styles.css" />
  </head>
  <body>
    ...my document

    <safe-guard tag="myWidget">
      <h1>My document</h1>
      <p>Once mounted in SafeGuard, I'll only have bootstrap CSS applied, and nothing from /css/styles.css.</p>
    </safe-guard>
  </body>
</html>

Any other elements to take along?

  • tag: null

Force using an iframe

  • iframe: false

If iframe, inject the iframe-resizer to adapt window height

  • iframeResizer: true

Option pass-through to iframe-resizer

  • iframeResizerOptions: {}

Examples

  <safe-guard>
    <p>No styling.</p>
  </safe-guard>

Ensure the styles go with the content into ShadowDOM

  <head>
    <!-- [sg:starttag:myWidget] -->
    <style>
      .customStyles { color: red; }
    </style>
    <!-- [sg:endtag:myWidget] -->
  </head>

  <body>
    …
    <safe-guard tag="myWidget">
      <p class="customStyles">Custom styling</p>
    </safe-guard>
    …
  </body>

Force safeguard to use an iframe instead of ShadowDOM

  <head>
    <!-- [sg:starttag:myWidget] -->
    <style>
      .customStyles { color: red; }
    </style>
    <!-- [sg:endtag:myWidget] -->
  </head>

  <body>
    …
    <safe-guard iframe>
      <p class="customStyles">Custom styling</p>
    </safe-guard>
    …
  </body>