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

@guardian/fence

v0.2.13

Published

Utility to render fenced embeds in iframes

Downloads

2,102

Readme

Fence – safer embed sandbox

Fence provides a safer way to embed custom code on a website.

Embedding third-party HTML onto a website can endanger the stability of a page, e.g. by calling destructive web APIs (document.write) or triggering JavaScript errors. They sometimes expect to be present in the page on load in order to load scripts synchronously, though you may want to inject them dynamically.

This library provides a safer abstraction to wrap custom code and render it on demand.

Fence uses <iframe> as sandboxing mechanism, through the srcdoc attribute (polyfilled for older browsers).

Note that fence does not improve security (the sandboxed code still runs on the same origin), only the reliability of the embedded content.

Usage

The fence library is distributed as an AMD module, so make sure you are using an AMD loader, for instance RequireJS, and use it to load in the library:

require(['fence'], function(fence) {
  // use fence as per the instructions below...
});

To wrap any HTML code into a fenced iframe, pass it to the wrap function:

var iframe = '<script src="http://example.com/script.js"></script>';
var embedHtml = fence.wrap(iframe);

// you can then add it to your page, e.g.
someContainer.innerHTML = embedHtml;

Note that the code will only be wrapped if it is unsafe (i.e. risks of destructive or averse side-effects). If it is safe (e.g. it's already an iframe), it will just be returned as-is.

If you just want to check if some HTML is safe to embed, use isSafeCode:

var iframe = '<iframe src="http://example.com/iframe"></iframe>';
fence.isSafeCode(); // => true

var iframe = '<script src="http://example.com/script.js"></script>';
fence.isSafeCode(); // => false

Once you have a fenced iframe in your page, you can render it by passing its DOM node or id to the render function:

// Render using a reference to the node
var node = document.querySelector('.content iframe.fenced');
fence.render(node);

// ... or just by id, assuming you have:
//  <iframe id="some-fenced-iframe" class="fenced" srcdoc="..."></iframe>
fence.render('some-fenced-iframe');

If you just want to render all fenced iframes on the page, you can simply call:

fence.renderAll();

Installation

The easiest way to install fence to use in your web application is through NPM:

$ npm install @guardian/fence

Then simply point your AMD config to the fence.js file and you're ready to go!

Examples

To try the examples locally, simply run:

$ npm install
$ grunt connect

Then open http://localhost:9001/examples/ in your browser.

FAQ

Does it actually work?

Yes it does. It was developed for and is being used on the Guardian website.

What is the browser support

Fence has been tested on the following browsers:

  • Firefox
  • Chrome
  • IE (9+)