This is now possible with vanilla JS
We can fetch the file in our content script, so no background script headaches, convert the file into a blob and create a file in the memory using the new File constructor.
After that, we can select the input tag using standard query selectors, create a Data Transfer, pass the file to the input tag, and dispatch the change event with bubbles sounds simple dun it.
Here is the code snippet:
// This function does the following
// 1. fetches the PDF resume from the url provided
// 2. creates a file object with the resume data
// 3. triggers the change event on the file input element
// 4. the file input element gets the file object
// 5. the file object is uploaded to the website
async function handleResumeInput(remoteResumeURL) {
const designFile = await createFile(remoteResumeURL);
const input = document.querySelector('input[type="file"]');
const dt = new DataTransfer();
dt.items.add(designFile);
input.files = dt.files;
const event = new Event("change", {
bubbles: !0,
});
input.dispatchEvent(event);
}
async function createFile(url) {
let response = await fetch(url);
let data = await response.blob();
let metadata = {
type: "image/png",
};
return new File([data], "app-logo.png", metadata);
}
This post explains it in details
https://medium.com/@dev_agam/automate-file-upload-with-chrome-extension-7ee6989d58e9