Skip to content

Instantly share code, notes, and snippets.

@joshje
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save joshje/f1a4cb69b068cbda4c31 to your computer and use it in GitHub Desktop.

Select an option

Save joshje/f1a4cb69b068cbda4c31 to your computer and use it in GitHub Desktop.

Revisions

  1. joshje revised this gist Oct 15, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions netmag-wordcount.js
    Original file line number Diff line number Diff line change
    @@ -5,15 +5,15 @@
    text = function(el) {
    if (el.innerText) return el.innerText;
    return el.textContent;
    },
    wordCount = text(body).split(' ').length;
    };

    var wordCount = text(body).split(' ').length;

    for (var i = codeBlocks.length - 1; i >= 0; i--) {
    var code = text(codeBlocks[i]);
    if (code) {
    codeLines += code.split('\n').length;
    var words = code.split(' ').length;
    wordCount -= words;
    wordCount -= code.split(' ').length;;
    }
    }
    var total = wordCount + codeLines * 10;
  2. joshje revised this gist Oct 15, 2014. 2 changed files with 31 additions and 9 deletions.
    35 changes: 26 additions & 9 deletions netmag-wordcount.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,15 @@
    (function() {
    var codeBlocks = document.getElementsByTagName('pre'),
    codeLines = 0,
    wordCount = document.getElementsByTagName('body')[0].innerText.split(' ').length;
    body = document.getElementsByTagName('body')[0],
    text = function(el) {
    if (el.innerText) return el.innerText;
    return el.textContent;
    },
    wordCount = text(body).split(' ').length;

    for (var i = codeBlocks.length - 1; i >= 0; i--) {
    var codeBlock = codeBlocks[i].getElementsByTagName('code');
    var code = codeBlock.length ? codeBlock[0].innerText : null;
    var code = text(codeBlocks[i]);
    if (code) {
    codeLines += code.split('\n').length;
    var words = code.split(' ').length;
    @@ -14,11 +18,24 @@
    }
    var total = wordCount + codeLines * 10;

    var message = [];
    message.push('Article has:');
    message.push(wordCount + ' words (not including code)');
    message.push(codeLines + ' lines of code (counting for ' + codeLines * 10 + ' words)');
    message.push(total + ' total');
    var list = [];
    list.push(wordCount + ' words (not including code)');
    list.push(codeLines + ' lines of code (counting for ' + codeLines * 10 + ' words)');
    list.push(total + ' total');
    var message = 'Article has:';
    message += '<ul style="margin: 0;padding-left: 20px;"><li>';
    message += list.join('</li><li>');
    message += '</li></ul>';

    window.alert(message.join("\n"));
    var el = document.createElement('p');
    el.style.color = '#000';
    el.style.position = 'relative';
    el.style['z-index'] = 1000000;
    el.style.border = '1px solid #999';
    el.style.padding = '10px';
    el.style.margin = '10px';
    el.style.background = '#FFF';
    el.style['border-radius'] = '2px';
    el.innerHTML = message;
    body.insertBefore(el, body.firstChild);
    })();
    5 changes: 5 additions & 0 deletions netmag-wordcount.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # How to install the bookmarklet

    1. Go to [http://gistlet.com/?gist=f1a4cb69b068cbda4c31](http://gistlet.com/?gist=f1a4cb69b068cbda4c31)
    2. Drag the bookmark to your URL bar
    3. You're done!
  3. joshje created this gist Oct 14, 2014.
    24 changes: 24 additions & 0 deletions netmag-wordcount.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    (function() {
    var codeBlocks = document.getElementsByTagName('pre'),
    codeLines = 0,
    wordCount = document.getElementsByTagName('body')[0].innerText.split(' ').length;

    for (var i = codeBlocks.length - 1; i >= 0; i--) {
    var codeBlock = codeBlocks[i].getElementsByTagName('code');
    var code = codeBlock.length ? codeBlock[0].innerText : null;
    if (code) {
    codeLines += code.split('\n').length;
    var words = code.split(' ').length;
    wordCount -= words;
    }
    }
    var total = wordCount + codeLines * 10;

    var message = [];
    message.push('Article has:');
    message.push(wordCount + ' words (not including code)');
    message.push(codeLines + ' lines of code (counting for ' + codeLines * 10 + ' words)');
    message.push(total + ' total');

    window.alert(message.join("\n"));
    })();