@madgex/safeguard

2.0.3 • Public • Published

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>

Readme

Keywords

none

Package Sidebar

Install

npm i @madgex/safeguard

Weekly Downloads

2

Version

2.0.3

License

MIT

Unpacked Size

1.07 MB

Total Files

24

Last publish

Collaborators

  • fernando.perez86
  • ali.hussnain
  • nabuckley
  • dbangamuwa
  • umar.mahmood
  • sjohnson-madgex
  • mbainwiley
  • luke.morgan
  • sadi.uddin
  • joseph.gavin
  • luis.sb.madgex
  • jameswragg
  • richp
  • madgex-npm
  • systems.team
  • celine.cozler
  • antoninoriesi
  • jackjohn86
  • rachel.godfrey
  • claudiayrr
  • nigelpevans