Lockdown obfuscates code snippets by writing them to the canvas. This demo is compatible with IE7+, Firefox, Chrome, and Safari.

The use case for the particular project is a testing environment where the proctor wants to prevent test-takers from easily copying code and determining the answer using the console or a CLI.

Code blocks can either have random lines inserted, which are then removed using a regular expression before rendering to canvas, or they may be base-64 encoded, or both.

HTML5 Powered with CSS3 / Styling, and Graphics

Sample 1

function bar (baz) {
-- do not copy --
    return function () {
        alert("Do not copy " + baz + "!");
    };
-- do not copy --
}
-- do not copy --

Sample 2

function%20foo%20()%20%7B%0A%20%20alert('Hello%20world!')%0A%7D

Sample 3

var%20t%20%3D%20%24(%22%3Ctextarea%3E%22).prependTo(%22body%22)%3B%0Avar%20b%20%3D%20%24(%22%3Cbutton%3Eclick%20to%20encode%3C%2Fbutton%3E%22).click(function%20()%20%7B%0A%20%20var%20val%20%3D%20t.val()%3B%0A%20%20if%20(val)%20%7B%0A%20%20%20%20t.val(encodeURIComponent(val))%3B%0A%20%20%7D%0A%7D).insertBefore(t)%3B%0A%24(%22%3Cbr%3E%22).insertBefore(t)%3B

Implementation

Place jQuery, the Lockdown script and the Lockdown stylesheet on the page:

<link rel="stylesheet" href="css/lockdown-0.0.38-min.css">

<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/modernizr-2.0.6.min.js"></script>
<script src="js/lockdown-0.0.38-min.js"></script>

Once that's in place, you may replace code blocks using the Lockdown.lock method:

Modernizr.load([
    "js/lockdown-0.0.38-min.js",
    {
        test: Modernizr.canvas,
        nope: "js/flashcanvas/flashcanvas.js",
        complete: function () {
            $(function () {
    
                var $elements = $("pre.code");
                var options = {
                    jsdir: "lockdown/build/js",
                    width: 580,
                    filterexp: new RegExp("-- do not copy --")
                };
                Lockdown.init( options );
                Lockdown.lock( $elements );
    
            });
        }
    }
]);

Documentation is available here.

Dependencies

jQuery 1.7+, Modernizr New, Flashcanvas for older browser support

Contributing

License

MIT

Download

The latest release, 0.0.38, is available here.

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/draeton/lockdown