Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. Original code author: ExpertSystem
  2. http://stackoverflow.com/a/19831667/4101334
  3.  
  4. /* Download an img */
  5. function download(img) {
  6. var link = document.createElement("a");
  7. link.href = img.href;
  8. link.download = img.title;
  9. link.style.display = "none";
  10. var evt = new MouseEvent("click", {
  11. "view": window,
  12. "bubbles": true,
  13. "cancelable": true
  14. });
  15.  
  16. document.body.appendChild(link);
  17. link.dispatchEvent(evt);
  18. document.body.removeChild(link);
  19. console.log("Downloading...");
  20. }
  21.  
  22. /* Download all images in 'imgs'.
  23. * Optionaly filter them by extension (e.g. "jpg") and/or
  24. * download the 'limit' first only */
  25. function downloadAll(imgs, ext, limit) {
  26. /* If specified, filter images by extension */
  27. if (ext) {
  28. ext = "." + ext;
  29. imgs = [].slice.call(imgs).filter(function(img) {
  30. var src = img.src;
  31. return (src && (src.indexOf(ext, src.length - ext.length) !== -1));
  32. });
  33. }
  34.  
  35. /* Determine the number of images to download */
  36. limit = (limit && (0 <= limit) && (limit <= imgs.length))
  37. ? limit : imgs.length;
  38.  
  39. /* (Try to) download the images */
  40. for (var i = 0; i < limit; i++) {
  41. var img = imgs[i];
  42. console.log("WEBM: " + img.href + " (", img, ")");
  43. download(img);
  44. }
  45. }
  46.  
  47.  
  48. /* Callback for button's "click" event*/
  49. // Look only inside .desktop class elements
  50. // for href that starts with /b/src/ and ends with .webm
  51. function doit() {
  52. var imgs = $('.desktop[href^="/b/src/"][href$=".webm"]');
  53. downloadAll(imgs, "", -1);
  54. }
  55.  
  56. /* Callback for button's "click" event */
  57. // Look only inside .desktop class elements
  58. // for href that starts with /b/src/ and not ends with .webm
  59. function doit2() {
  60. var imgs = $('.desktop[href^="/b/src/"]').not('.desktop[href$=".webm"]');
  61. downloadAll(imgs, "", -1);
  62. }
  63.  
  64. /* Create and add a "download" button on the top, left corner */
  65. function addDownloadBtn() {
  66. var btn = document.createElement("button");
  67. btn.innerText = "Download all шебм";
  68. btn.addEventListener("click", doit);
  69. btn.style.position = "fixed";
  70. btn.style.top = btn.style.left = "0px";
  71. document.body.appendChild(btn);
  72.  
  73. var btn2 = document.createElement("button");
  74. btn2.innerText = "Download all images";
  75. btn2.addEventListener("click", doit2);
  76. btn2.style.position = "fixed";
  77. btn2.style.top = "0px";
  78. btn2.style.left = "150px";
  79. document.body.appendChild(btn2);
  80. }
  81. addDownloadBtn();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement