1

I have very little coding experience and just learn from looking things up and trying it. I've never dealt with javascript before so please try to be specific.

I'm looking to create a bookmarklet to automate video downloads. This is what I've found so far through searching:

document.getElementsByClassName('class_name')[0].innerHTML
// used to output to console the value of the class

document.getElementsByClassName('class_name')[0].click() 
// used to click on a specific class
function copyToClipboard(text) {

    var dummy = document.createElement("textarea");
    // to avoid breaking orgain page when copying more words
    // cant copy when adding below this code
    // dummy.style.display = 'none'
    document.body.appendChild(dummy);
    //Be careful if you use texarea. setAttribute('value', value), which works with "input" does not work with "textarea". – Eduard
    dummy.value = text;
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
}

//The above is a function used to copy data to the clipboard.

This is how I'm imagining it to work after clicking the bookmarklet:

copyToClipboard(document.getElementsByClassName('video_name')[0].innerHTML)
// this will copy the video name to the clipboard

document.getElementsByClassName('download_button')[0].click() 
// this will click the download button which opens the resolutions list

document.getElementsByClassName('resolution')[0].click() 
// this will click the top most resolution in the downloads list

This is the part where I'm stuck. The file explorer now opens up to set the location and the name to save as. How do I implement the bookmarklet such that it will paste the clipboard contents into the "File name" field in file explorer (it is already automatically selected on that field) and then how do I get it to click the "Save" button.

Thanks for the help!

afiend123
  • 11
  • 1
  • I'm pretty sure in-browser Javascript alone doesn't have that capability. You'll need a privileged environment (like an extension) *or* just use an extension that can do this for you. – CertainPerformance Dec 23 '19 at 04:38
  • @CertainPerformance know of any? I've looked around and haven't found any that work. – afiend123 Dec 23 '19 at 04:50
  • There's [DownThemAll](https://www.downthemall.net/), haven't used it in ages though – CertainPerformance Dec 23 '19 at 04:53
  • Javascript doesn't have the ability in the way you're going about it. Take a look at the [answers in this question](https://stackoverflow.com/questions/3102226/how-to-set-name-of-file-downloaded-from-browser) to see what your options are. – Ouroborus Dec 23 '19 at 08:48
  • @Ouroborus I see, thanks for answering. DownThemAll doesn't work how I'd like it to. Have tried it previously and just rechecked to make sure. For now, the code I have is good enough. I hit the bookmarklet, it copies the video name to clipboard, it opens the download window, and from there I just "ctrl + v" and enter. The one annoying thing is that ram and cpu usage spike insanely on chromium based browsers but the copy function doesn't work in firefox. – afiend123 Dec 23 '19 at 18:48
  • @afiend123 Access to the clipboard, in Firefox, may be restricted to the context of responding to a user click. Bookmarklets don't have that context. – Ouroborus Dec 23 '19 at 22:04
  • Yeah, that's what I've seen, at least. And I don't really want to go so far as to make an extension for this purpose (not that I'd know how) but that _would_ be a fun project, I guess. I was looking for a way to disable that security feature or bypass it since this is just for my personal use but couldn't find one. Current bookmarklet is good enough, Still saves me a good bit of time compared to before. Thanks for all the help! – afiend123 Dec 24 '19 at 19:47

0 Answers0