Created
November 20, 2015 15:12
-
-
Save mattandrews/0bdc4562e5466fc82213 to your computer and use it in GitHub Desktop.
Get all unique characters on a webpage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
var chars = []; | |
var logCharacters = function(str) { | |
var letters = str.split(''); | |
letters.forEach(function(l){ | |
l = l.trim(); // we don't need spaces | |
if (chars.indexOf(l) === -1) { | |
chars.push(l); | |
} | |
}); | |
}; | |
var walkDOM = function () { | |
var elements = document.getElementsByTagName('*'); | |
var l = elements.length; | |
for (var i = 0; i < l; i++) { | |
var element = elements[i]; | |
for (var j = 0; j < element.childNodes.length; j++) { | |
var node = element.childNodes[j]; | |
if (node.nodeType === 3) { | |
var text = node.nodeValue; | |
logCharacters(text); | |
} | |
} | |
console.log('Processed element ' + i + '/' + l); | |
} | |
console.log('Found ' + chars.length + ' unique characters'); | |
console.log(chars); | |
}; | |
walkDOM(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment