Extension for Chrome: Automatically transfer downloaded files to a designated input field

I have developed a small chrome extension to migrate images, which consists of 5 files:

popup.html - the plugin's HTML document

 This file contains HTML buttons and a script link to settings.js, which listens for the image download button to be clicked and sends a message to my content script, run.js, to find the images.

run.js - the content script (with access to the webpage's DOM).

This script receives messages from run.js, finds the image names and links I want to download, puts them into an object, and sends them via message to the background script: bootstrap.js.

bootstrap.js - runs the background page and has access to the Chrome API.

var Downloads = [];
chrome.extension.onMessage.addListener(function(request, sender, sendResponse)
{
if(request.message.method == 'download'){
    for( var d = 0; d <= request.message.imageLinks.length; d++ ){
      chrome.downloads.download( {url: request.message.imageLinks[d],
             filename: request.message.imageName[d]}, function(id){

        });
          sendResponse('done');
    }
}
}); 

Everything is working smoothly as it loops through the images and downloads them. Now, my next challenge is to take the downloaded images and insert them into the file upload fields on another website (which I have open in another tab) that have the same field names, etc.. I came across this line of code in the Chrome API documentation:

//Initiate dragging the downloaded file to another application. Call in a javascript ondragstart handler.
chrome.downloads.drag(integer downloadId)

Initially, I thought this drag function might work since you can manually drag the downloaded image into the HTML file upload field without having to click and select the file. However, I couldn't find any examples or further documentation on it.

Does anyone know if it is possible to achieve this using JavaScript and the Chrome API?

Answer №1

To begin, start by accessing the contents of the files that you have downloaded.

In order to do this, you will need to grant permission to file://* in your manifest file. Then, utilize a loop to iterate through your downloaded files using chrome.downloads.search, obtaining each file's path on the system through the DownloadItem.filename property. For each file, send a GET XMLHttpRequest to the URL file://{filepath} to retrieve its content.

After obtaining the content, convert it into a DataUrl and dynamically add the file to the FormData of your form (not directly to the inputs, but to the FormData that will be submitted at the end). Refer to this answer for detailed instructions on this process.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ajax Live Search Error Detected

Having trouble retrieving data from the database..! <html> <head> <title>Live Search</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> &l ...

In situations where there may be a duplicate, what alternative can I utilize in place of the id attribute?

I understand that almost any element in the DOM can have an "id" attribute, and I've used it to track each client in a table of clients. Although ids should not be repeated, my rows are assigned unique identifiers based on each person's "clientId ...

Capture all div elements from the table and store them in an array

Check out this code snippet that includes draggable divs and a droppable table: http://jsbin.com/exetuk/1/edit When dragging a div to the table, copies of the div with the class .draggable appear. I am looking to create an array with divs from the table ...

Is it possible to switch the background-image after a gif has finished loading?

I am currently using a GIF as a background image, but I would like it to show a static image until the GIF is fully loaded and ready to play. After reading several similar discussions, I understand that you can manipulate elements with JavaScript once the ...

Output a message to the Java console once my Selenium-created Javascript callback is triggered

My journey with Javascript has led me to mastering callback functions and grasping the concept of 'functional programming'. However, as a newcomer to the language, I struggle to test my syntax within my IntelliJ IDE. Specifically, I am working on ...

Showcasing text behind an element with reduced opacity when the element directly above it is selected using rails, CSS, and jQuery

I have a password field on my page where the password is hidden, but I want users to be able to copy and paste the clear text version of the password on another website. In order to achieve this, I followed the instructions in an article titled Mask text, ...

When transmitting information to the server, the browser initiates four requests

I am encountering an issue with my React component. The problem arises when I try to retrieve the current geographic coordinates, as they are being fetched 4 times consecutively. This same glitch occurs when attempting to send the coordinates to the serv ...

Automate CSS slideshow playback using JavaScript

Using only CSS, I created a basic slideshow where the margin of the element changes upon radio button click to display the desired slide. You can view the functionality in the code snippet below. Additionally, I implemented auto play for this slideshow usi ...

Ways to effectively pass arguments to the callback function within the catch function in JavaScript

While working on my code, I suddenly felt the need to pass an extra argument, "msg", to the callback function renderError(). This extra argument should be passed along with the default error argument generated by the catch function itself. I tried doing i ...

Tips for updating a value in a nested directive in AngularJS

My directive includes a menu toggle button and various nested directives with their own menus. The outer directive has an isolated scope, while the inner directive has no specified scope. I am trying to figure out how to show/hide the menu in the inner d ...

"The file upload function is populating the req.body object, but not the req.file

I successfully implemented a file upload API using multer and express, which functions well when accessed through POSTMAN. However, I encountered an issue when trying to utilize the same API with another file upload API: The code I used can be found below ...

As a result of the Chrome 53 bug, the Yahoo Weather API encountered an insecure certificate causing the jQuery AJAX request to fail

For the past year, I've relied on the Yahoo Weather API to provide me with weather updates. I've been utilizing the following link to access the data: https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (SELE ...

Adjusting CSS to switch up the color scheme

Is there a way to allow users to change the background color of pull quotes by using the input type="color tag within a form element? Below is my current HTML structure: <form action="change.php" method="post"> name: <input type="text"> ...

I attempted to create a test scenario to verify that the length of the tasks array is not negative. However, when trying to test this using 'not.toBe' in the code below, an error was encountered

app.js var todos=[]; todos=['to-do one', 'to-do two']; module.exports=todos; app.test.js const todos=require('./app') test('should have a length of at least 0',()=>{ expect(todos.length).toBeGreaterThanOrEqu ...

Why is it that when a boolean value is logged as a checkbox, it shows up as undefined?

In my code, I'm attempting to log the value of a variable named check, which corresponds to column 11 in a spreadsheet. This variable is meant to represent the state of a checkbox (true or false) based on whether it's been ticked or not. However, ...

What methods with JavaScript, Ajax, or jQuery can I apply to populate the student details?

For completing the StudentID section, please fill out the form data.cfm with the first name, last name, and Middle Name. <script language="Javascript"> $(function() { $( '#effective_date' ).datepicker(); jQuery.validator.addMetho ...

Ensure that both Vue methods are executed synchronously

I am working with two Vue methods: (1) this.retrieveSavedSearches() (2) this.updateDefaultSelectOption() Is there a way to ensure that method (2) only executes after method(1) has completed its execution? ...

Most effective method for displaying modals in React

I've recently started learning ReactJS and I'm exploring the use of modal windows. However, I'm uncertain about the most appropriate approach to take. Currently, I am implementing modals using callbacks. Reactstrap Modal Dialog Component: ...

What is the best way to determine the value of margin-left in inline CSS using jQuery?

Here is the code snippet I am working with: <div class="ongoing" style="width: +728px; margin-left: +12480px"> I need to extract the value of the margin-left property for scrolling purposes. The technique I used to retrieve the width value does no ...

EJS selectively displaying certain elements from an object

This particular issue has been baffling me. I have been passing an object into an ejs template and when I output that object, everything appears as expected: { _id: 5504a5e7ff67ac473dd7655c, code: 'GB', name: 'United Kingdom', slug: & ...