Last active
July 27, 2016 00:55
-
-
Save justdanpo/3ad1159df5663ad3b2f69bab9c19338e to your computer and use it in GitHub Desktop.
Ctrl+LeftClick to save image
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
// ==UserScript== | |
// @name Ctrl+LeftClick to save image | |
// @namespace https://gist.github.com/justdanpo | |
// @version 0.1 | |
// @description Ctrl+LeftClick to save image | |
// @author den_po | |
// @match http://*/* | |
// @match https://*/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
function checkCommon(evt) { | |
if (evt.target.tagName != "IMG") | |
return false; | |
if (evt.button != 0) | |
return false; | |
if (!evt.ctrlKey || evt.altKey || evt.shiftKey) | |
return false; | |
evt.preventDefault(); | |
evt.stopPropagation(); | |
return true; | |
} | |
window.addEventListener('mousedown', function(evt) { | |
if (checkCommon(evt)) { | |
var a = document.createElement('a'); | |
a.href = evt.target.src; | |
a.setAttribute('download', ''); | |
a.dispatchEvent(new MouseEvent('click')); | |
} | |
}, true); | |
window.addEventListener('click', function(evt) { | |
checkCommon(evt); | |
}, true); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment