Created
August 30, 2013 13:12
-
-
Save Jabher/6389700 to your computer and use it in GitHub Desktop.
rarjpeg detector
This file contains 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 (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