Skip to content

Instantly share code, notes, and snippets.

@justdanpo
Last active July 27, 2016 00:55
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 justdanpo/3ad1159df5663ad3b2f69bab9c19338e to your computer and use it in GitHub Desktop.
Save justdanpo/3ad1159df5663ad3b2f69bab9c19338e to your computer and use it in GitHub Desktop.
Ctrl+LeftClick to save image
// ==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