Codecademic Record

(by-)products of my activity as a codecademic

SCT Viewer

This JavaScript bookmarklet is based on the Submission Correctness Viewer originally written by minrice (Michael Tackes), with valuable contributions by Joah G. It uses Bootstrap’s Modal component (Codecademy already loads the CSS for it; the javascript part is loaded dynamically on first use).

The bookmarklet only works on Codecademy exercise pages (such as this one). To use it:

  1. Drag the link below to your Bookmarks Bar
  2. (optional) edit the bookmarklet, replacing the URL with this code
  3. Visit a Codecademy exercise page and click the bookmarklet

Cheat

It only works for certain types of exercise page. Tested on a couple of JavaScript, Python, and Ruby exercises. There may be bugs and I haven’t tested it on any browser other than the latest Chrome for Mac.

Source

The code is on GitHub, so if you have improvements, just send me a pull request!

SCT Viewer (sct-viewer.js) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/
Display SCT (Submission Correctness Test) of a Codecademy Exercise.
By Alex Jahraus (based on the original SCT Viewer Bookmarklet by Michael Tackes)
Works only in Codecademy’s New UI, see
http://www.codecademy.com/blog/62-introducing-a-new-learning-experience
Dependencies:
  - jQuery, Twitter Bootstrap’s CSS for modals and highlight.js (loaded by Codecademy)
  - bootstrap-modal.js (loaded dynamically)
How To Use: the minified code can be used in a bookmarklet to execute
the script on the page of the exercise you’re currently viewing.

Version: 0.3
Last updated: 19. April 2017 to match Codecademy’s new UI
/

(function(){

try {
  // step 1: get the SCT code and author name
  var c  = CCDATA.composer,
      pi = +location.pathname.match(/(\d+)\/\d+$/)[1],    // pi for project index
      ci = c.current_checkpoint_index,                    // ci for checkpoint index
      p  = c.course.projects[pi],                         // p is the project
      a  = p.author.handle,                               // a for author
      l  = c.course.language.toLowerCase(),               // l for language (syntax highlighting)
      t  = p.checkpoints[ci].test_functions;              // t is the test code

  // step 2: load bootstrap-modal.js
  if ($.fn.modal) sm()
  else $.getScript('//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.2.0/js/modal.min.js')
        .done(sm)
        .fail(function(r,s,e){throw e})
} catch (e) {
  return console.log(e.message);
}
function sm(){
  // step 3: compose the modal div
  var $m = $('#sct'); // the modal
  if ($m.length == 0) { // modal is not yet in the document
    $m = $('<div id=&#39;sct&#39; class=&#39;modal fade hide&#39; style=&#39;width:700px&#39;>')
      .append($('<div class=&#39;modal-header&#39;>')
        .append($('<button type=&#39;button&#39; class=&#39;close&#39; data-dismiss=&#39;modal&#39;>').text('×'))
        .append('<h3>Correctness Test <small> &nbsp; by '+a+'</small></h3>')
    ).append('<div class=&#39;modal-body&#39;><pre><code></code></pre></div>')
    .append($('<div class=&#39;modal-footer&#39;>')
      .append('<a target=&#39;blank&#39; href=&#39;//j.mp/17nuoIp&#39;>Feedback</a> &nbsp;')
      .append($('<button class=&#39;btn btn-small&#39; data-dismiss=&#39;modal&#39;>').text('Close'))
    ).appendTo($('body'));
  }

  // step 4: sanitize, insert, and highlight the code (currently not visible)
  t = t.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
  var $code = $m.find('code').eq(0)
    .attr('class','language-'+ (l == 'web' ? 'javascript' : l))
    .html(t)
  hljs && hljs.highlightBlock($code.get(0))

  // step 5: show the modal
  $m.modal('show')
}
})();

Now go try it out!

Comments