GithubHelp home page GithubHelp logo

leandro-reis / jsbin-jsfiddle-clone Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swapnilmishra/jsbin-jsfiddle-clone

0.0 1.0 0.0 926 KB

Small project to show how sites like jsbin,jsfiddle,dabblet works

HTML 0.40% JavaScript 96.90% CSS 2.70%

jsbin-jsfiddle-clone's Introduction

How to build runners like jsbin, jsfiddle, dabblet

I have always wondered how tools like jsbin, jsfiddle or dabblet work. So, thought of putting together some code to do the same.

alt text

Show me some code

It basically boils down to creating an iframe and injecting the content(html,css,js) to update. Below is the code from createIframe.js file which takes raw HTML,JS,CSS and create an iframe from it there by showing the output.

let frameEl;
let createFrame = function ({html,js,css}){
    // memoize frame element
    if(frameEl){
        frameEl.remove()
    }
    frameEl = document.createElement('iframe')
    const el = document.querySelector("#root")
    frameEl.setAttribute("id","iframe")
    frameEl.setAttribute("class","output")    
    el.appendChild(frameEl)
    const doc = document.querySelector('#iframe').contentWindow.document
    const head = doc.querySelector("head")
    doc.body.innerHTML = html;
    head.appendChild(createEl('script',js))
    head.appendChild(createEl('style',css))
}

let createEl = function(type,innerhtml){
    const el = document.createElement(type)
    el.innerHTML = innerhtml
    return el;
}

How to run

npm install
npm start
npm run build // to build bundle.js as livereload server serves it from memory

Tooling used

What's not implemented here

Whatever I have implemented here are the easiest things to build for these type of tools. The real challenge is building it with security practices in place. In this link, creator of jsbin(Remy sharp), jsfiddle(Piotr Zalewa) and dabblet(Lea verou ) discusses various best practices and security aspects which are very important.

Few of these things are:

  • Handling user-accounts/login/signup/saving.
  • Ensuring the iframe is not used for XSS attacks.
  • Ensuring the high availablity and concurrency in case of multiple concurrent edits.
  • JSbin(which is my favorite) also keeps history of every edit made.
  • Linting CSS, JS, HTML.
  • Support for preprocessors.
  • Code collaboration using WebRTC.

I have tried to shed some light on how these tools can be built and hope this would be useful to many of you. We are very fortunate to have these smart people who have built all these awesome tools which, makes our life so easy. Thats why the web is the most vibrant open source community.

jsbin-jsfiddle-clone's People

Contributors

swapnilmishra avatar

Watchers

 avatar

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.