Giter Site home page Giter Site logo

elizabethhudnott / one-way-sandbox Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 58 KB

Lets a parent frame control a sandboxed iframe in a jQuery-like way without letting the iframe access the parent.

Home Page: https://elizabethhudnott.github.io/one-way-sandbox/demo/index.html

JavaScript 99.01% HTML 0.99%
dom dom-manipulation iframe iframes popups popup-window popup-windows postmessage postmessage-api postmessage-library

one-way-sandbox's Introduction

Purpose

Have you ever wanted to isolate some web content so its scripts and CSS didn't mess with the rest of your page? Perhaps you want users to be able to write their own code? Simple. Use a sandboxed iframe, right? But sandboxing also prevents the parent page from accessing the embedded page. All your parent page's goodness and clever manipulations won't work on content inside the iframe. What about when you want the sandbox restriction to be one-way, so the parent issues commands to the embedded page and the child obeys? You'll find numerous tutorials on the web explaining how to do this using Window.postMessage. But what if you don't want to spend time developing your own protocol for describing your application's commands, encoding them into some specially designed format, and decoding them at the other end (not to mention doing the whole process in reverse if the function in the child frame returns a value)? Can't it be made to "just work" in a general way? Maybe something a bit like a cross-frame version of jQuery? That's the purpose of this library.

Getting Started

Set Up

1. On the Parent Frame or Window Opener

  1. Add the script: <script src="page-modifier.js"></script>
  2. Register a sandboxed iframe (Unsandbox.addElement(frame);) or open and register a pop-up (popup = Unsandbox.addWindow(url, 'myWindow');).

2. Inside an iFrame or Opened Window

Add the script: <script src="page-modification.js"></script>

Sending Commands from Parent to Child

Unsandbox.send(frame, {
	op: 'setStyle',
	selector: 'body',
	name: 'background-color',
	value: 'lavender',
});

Getting a Return Value Back

Return values are catered for using promises.

Unsandbox.send(frame, {
	op: 'getAttribute',
	selector: 'a',
	name: 'href'
}).then (function (urls) {
	// urls in an array of URL strings.
});

A complete list of commands and their current implementation status is available. There's also a demo page and accompanying source code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.