Skip to content

Instantly share code, notes, and snippets.

@gregsonar
Last active December 1, 2017 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregsonar/0198daa7a7c516660e6dfaff5479c325 to your computer and use it in GitHub Desktop.
Save gregsonar/0198daa7a7c516660e6dfaff5479c325 to your computer and use it in GitHub Desktop.
I perfectly know that the best way to protect image download is not putting it on internet in the first place. But sometimes there are bad days...
//Development version:
function prtctImg() {
var protected_images = document.getElementsByTagName("img");
for (index = 0; index < protected_images.length; ++index) {
protected_images[index].oncopy = function() {return false;};
protected_images[index].oncut = function() {return false;};
protected_images[index].onpaste = function() {return false;};
protected_images[index].ondrag = function() {return false;};
protected_images[index].ondragstart = function() {return false;};
protected_images[index].oncontextmenu = function() {return false;};
};
};
prtctImg();
var target = document.querySelector('body');
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
prtctImg();
});
});
var config = {subtree: true, childList: true }
observer.observe(target, config);
//Minified version:
function prtctImg(){var n=document.getElementsByTagName("img");for(index=0;index<n.length;++index)n[index].oncopy=function(){return!1},n[index].oncut=function(){return!1},n[index].onpaste=function(){return!1},n[index].ondrag=function(){return!1},n[index].ondragstart=function(){return!1},n[index].oncontextmenu=function(){return!1}}prtctImg();var target=document.querySelector("body"),observer=new MutationObserver(function(n){n.forEach(function(n){prtctImg()})}),config={subtree:!0,childList:!0};observer.observe(target,config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment