Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Original code author: ExpertSystem
- http://stackoverflow.com/a/19831667/4101334
- /* Download an img */
- function download(img) {
- var link = document.createElement("a");
- link.href = img.href;
- link.download = img.title;
- link.style.display = "none";
- var evt = new MouseEvent("click", {
- "view": window,
- "bubbles": true,
- "cancelable": true
- });
- document.body.appendChild(link);
- link.dispatchEvent(evt);
- document.body.removeChild(link);
- console.log("Downloading...");
- }
- /* Download all images in 'imgs'.
- * Optionaly filter them by extension (e.g. "jpg") and/or
- * download the 'limit' first only */
- function downloadAll(imgs, ext, limit) {
- /* If specified, filter images by extension */
- if (ext) {
- ext = "." + ext;
- imgs = [].slice.call(imgs).filter(function(img) {
- var src = img.src;
- return (src && (src.indexOf(ext, src.length - ext.length) !== -1));
- });
- }
- /* Determine the number of images to download */
- limit = (limit && (0 <= limit) && (limit <= imgs.length))
- ? limit : imgs.length;
- /* (Try to) download the images */
- for (var i = 0; i < limit; i++) {
- var img = imgs[i];
- console.log("WEBM: " + img.href + " (", img, ")");
- download(img);
- }
- }
- /* Callback for button's "click" event*/
- // Look only inside .desktop class elements
- // for href that starts with /b/src/ and ends with .webm
- function doit() {
- var imgs = $('.desktop[href^="/b/src/"][href$=".webm"]');
- downloadAll(imgs, "", -1);
- }
- /* Callback for button's "click" event */
- // Look only inside .desktop class elements
- // for href that starts with /b/src/ and not ends with .webm
- function doit2() {
- var imgs = $('.desktop[href^="/b/src/"]').not('.desktop[href$=".webm"]');
- downloadAll(imgs, "", -1);
- }
- /* Create and add a "download" button on the top, left corner */
- function addDownloadBtn() {
- var btn = document.createElement("button");
- btn.innerText = "Download all шебм";
- btn.addEventListener("click", doit);
- btn.style.position = "fixed";
- btn.style.top = btn.style.left = "0px";
- document.body.appendChild(btn);
- var btn2 = document.createElement("button");
- btn2.innerText = "Download all images";
- btn2.addEventListener("click", doit2);
- btn2.style.position = "fixed";
- btn2.style.top = "0px";
- btn2.style.left = "150px";
- document.body.appendChild(btn2);
- }
- addDownloadBtn();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement