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...
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();
}
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);
})();