Skip to content

Instantly share code, notes, and snippets.

@Jabher
Created August 30, 2013 13:12
Show Gist options
  • Save Jabher/6389700 to your computer and use it in GitHub Desktop.
Save Jabher/6389700 to your computer and use it in GitHub Desktop.
rarjpeg detector
(function (rarDetected) {
function detectRar(blob, callback) {
var reader = new FileReader;
reader.readAsBinaryString(blob);
reader.onload = function () {
callback(reader.result.indexOf('Rar!') != -1)
}
}
function getBlob(url, callback) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.open('GET', url, true);
xhr.send();
xhr.onload = function () {
callback(new Blob([xhr.response]))
}
}
function imageProcessor(image) {
if (!image.complete) return image.addEventListener('load', imageProcessor.bind(image));
image.crossOrigin = 'Anonymous';
getBlob(image.src, function (blob) {
detectRar(blob, function (isRar) {
if (isRar) {
rarDetected(image.src, blob);
}
})
})
}
function imageDetector(nodeList) {
if (nodeList && nodeList.length) for (var i = 0; i < nodeList.length; i++) {
var node = nodeList[i];
if (node.children) setTimeout(imageDetector.bind(node.children), 100);
if (node.tagName == (new Image).tagName) imageProcessor(node);
}
}
function mutationRecordProcessor(mutationRecordList) {
mutationRecordList.forEach(function (mutationRecord) {
imageDetector(mutationRecord.addedNodes)
})
}
(new MutationObserver(mutationRecordProcessor)).observe(document.body, {
childList: true,
subtree: true
});
if (document.body) imageDetector(document.body.children);
})(function rarDetected(url, data) {
console.log(url)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment