Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.