How do I automatically download an image on danbooru when the site loads with userscript?

I have little experience in coding so it's probably a problem with me doing stuff wrong but I couldn't find this anywhere either. For nearly 2 hours I tried to make a userscript that automatically downlaods the image of the danbooru post on site load so I don't have to click the download button (ikr really usefull) but I didn't get anywhere. I looked at so many different peoples codes that did similar things and read other stuff to try and make this work but I just could not do it.

This is what I came up with after looking at others similar code an reading some other stuff but nothing below did anything and I accepted my defeat momentarily, at this point I just want to make it work out of spite.

// ==UserScript==
// @name aa
// @namespace aa
// @version 0.1
// @description aaaaaaaaaaa
// @author aa
// @match
// @icon none
// @grant none
// ==/UserScript==
(function(){ 'use strict';
document.querySelector('#post-option-download').click();
})();

also tried with document.querySelector('#post-option-download').getAttribute('href').click();

// ==UserScript==
// @name aaaaa
// @namespace aa
// @version 0.1
// @description aaaaaaaaaaaaaaaaaaa
// @author aa
// @match
// @icon none
// @grant none
// ==/UserScript==
(function(){ 'use strict';
document.querySelector('#post-option-download').click();
})();
// ==UserScript==
// @name aaaaaaaaaaaa
// @namespace aaa
// @version 0.1
// @description oh god help me
// @author aa
// @match
// @icon none
// @grant none
// ==/UserScript==
var button = document.querySelector('button[data-id="#post-option-download"]');
if (button) { button.click();
}
2

1 Answer

I managed to piece together a script, that works, on the hunt for answers. Even tho I don't have much experience I got it eventually feel free to use any of this.

// ==UserScript==
// @name Danbooru Download and Favorite
// @namespace AnimeRaupe
// @version 1.7
// @description downloads and favorites the post you open automatically
// @author AnimeRaupe
// @match
// @icon none
// @grant none
// @run-at document-end
// ==/UserScript==
(function() { 'use strict'; // favorites the Danbooru post document.querySelector("#add-to-favorites").click(); // fet the element with id "post-option-download" var downloadLink = document.querySelector('#post-option-download a'); var href = downloadLink.href; // hidden anchor element to trigger the download var hiddenAnchor = document.createElement('a'); hiddenAnchor.style.display = 'none'; hiddenAnchor.href = href; hiddenAnchor.download = downloadLink.getAttribute('download'); document.body.appendChild(hiddenAnchor); // download hiddenAnchor.click(); // clean up I think document.body.removeChild(hiddenAnchor);
})();

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like