Image Download with Ctrl+Click/Shift/MMB [GLOBAL]

Image downloading for most single-image pages, with Ctrl+Click, Shift or MMB-Click combinations

  1. // ==UserScript==
  2. // @name Image Download with Ctrl+Click/Shift/MMB [GLOBAL]
  3. // @namespace imageSaving
  4. // @description Image downloading for most single-image pages, with Ctrl+Click, Shift or MMB-Click combinations
  5. // @author NightLancerX
  6. // @match *://*/*.jpg*
  7. // @match *://*/*.png*
  8. // @match *://*/*.jpeg*
  9. // @match *://*/*.gif*
  10. // @version 2.5
  11. // @homepageURL https://github.com/NightLancer/PixivPreview
  12. // @license MIT License
  13. // @grant none
  14. // @run-at document-end
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. let img = document.querySelectorAll('img')[0];
  21. let imgSrc = img.src;
  22.  
  23. let main = function()
  24. {
  25. let anchor = document.createElement('a'), name;
  26. name = (imgSrc.indexOf('?')>-1)? imgSrc.substring(imgSrc.lastIndexOf("/")+1, imgSrc.indexOf('?')): imgSrc.substring(imgSrc.lastIndexOf("/")+1);
  27. try {name = decodeURI(name);} catch(e){};
  28. anchor.href = img.src;
  29. anchor.target = '_self';
  30. anchor.download = name;
  31. document.body.appendChild(anchor);
  32. anchor.click();
  33. main = ()=>{};
  34. };
  35.  
  36. //save with Ctrl+Click
  37. img.onclick = function(e){
  38. if (e.ctrlKey){
  39. e.preventDefault();
  40. main();
  41. }
  42. };
  43.  
  44. //save with Shift
  45. document.onkeyup = function(e){
  46. if (e.keyCode == 16){
  47. main();
  48. }
  49. };
  50.  
  51. //save with MMB-click
  52. img.onmouseup = function(e){
  53. if (e.button == 1){
  54. main();
  55. }
  56. };
  57. })();