Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@madgex/safeguard

Package Overview
Dependencies
Maintainers
15
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@madgex/safeguard

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

  • 2.0.3
  • latest
  • npm
  • Socket score

Version published
Maintainers
15
Created
Source

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>

FAQs

Package last updated on 06 May 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc