0

I am creating a chrome extension, that saves image urls from previous webpages. Is there any way for me, to upload the images for the user, to a webpages upload section, such as: Pages upload section

This is what the input html looks like:

<label>
 <form>
  <input accept="image/*,image/heif,image/heic" 
  class="mkhogb32" multiple="" type="file">
 </form>
</label>

Here is what I have so far:

  // load the image
       var img = new Image();
       img.setAttribute('crossOrigin', 'anonymous');
       img.src = result.products[pointer][3];

       // loaded
       img.onload = function() {
           // convert the image into a data url
           var dataUrl = getImageDataUrl(img);
           var blob = dataUrlToBlob(dataUrl);
           console.log(blob);

           var form = document.querySelectorAll('form');

           var formData = new FormData(form);
           formData.append("file", blob, "image_name.png");

           var request = new XMLHttpRequest();

           // upload
           request.open("POST", "/marketplace/create/item");
           request.send(formData);

          //  chrome.storage.local.get( function(result) {
          // })
       };

       //https://scontent-lga3-2.xx.fbcdn.net/v/t1.6435-9/194808255_1211628689294995_1686587396345915188_n.jpg?_nc_cat=107&ccb=1-3&_nc_sid=0d5531&_nc_ohc=mErP2svwNu0AX8Rb0uO&_nc_ht=scontent-lga3-2.xx&oh=c80e70ed525b3a956c4f5c8c70f58048&oe=60E32CCB


       // converts an image into a dataurl
       function getImageDataUrl(img) {
           // Create an empty canvas element
           var canvas = document.createElement("canvas");
           canvas.width = img.width;
           canvas.height = img.height;

           // Copy the image contents to the canvas
           var ctx = canvas.getContext("2d");
           ctx.drawImage(img, 0, 0);

           // NOTE:  this will throw a cross-origin security exception
           // if not done in an extension and if the image is cross-origin
           return canvas.toDataURL("image/png");
       }

       // converts a dataurl into a blob
       function dataUrlToBlob(dataUrl) {
           var binary = atob(dataUrl.split(',')[1]);
           var array = [];
           for(var i = 0; i < binary.length; i++) {
               array.push(binary.charCodeAt(i));
           }
           return new Blob([new Uint8Array(array)], {type: 'image/png'});
       }

I want the images to upload when the user clicks my html injected button on the page. This what I want to happen once my injected html button is clicked:

  1. Send image to webpages server (WORKS FINE)

  2. Have that image uploaded to facebooks "Add Photos" section. (NO PHOTO IS BEING PREVIEWED ON THE ADD PHOTO SECTION)

Is it possible?

  • You are going to need to explain the process you have in mind in more detail. What you have now is far too vague for constructive answers. See [ask] – charlietfl Jun 07 '21 at 23:54
  • You can look at: https://stackoverflow.com/questions/60502702/how-to-use-chrome-extension-to-upload-photos-to-ebay-listing. Or you can look at my previous post: https://stackoverflow.com/questions/67879801/can-my-chrome-extension-upload-images-to-a-webpages. These posts explain the essence of what I am trying to do. WHAT I AM TRYING TO DO: Suppose there is an upload image section on a page and I have image urls saved in my chrome storage. Is there a way to upload these image urls to the upload image section of said page? Must I convert the image to a blob? @charlietfl – Shane Marotta Jun 08 '21 at 00:08
  • The second link is to this very question. Questions need to be self explanatory and self contained if you want help. [edit] the question to include the step by step details – charlietfl Jun 08 '21 at 00:12
  • I just updated the question, I really don't believe anything else is necessary now. Could you take a look? @charlietfl – Shane Marotta Jun 08 '21 at 00:41
  • If you have images uploading already just send them from server to facebook – charlietfl Jun 08 '21 at 00:46
  • Isn't that what I am already doing? If not, how would I send it to facebook and would that automatically upload the photo to the "Add Photos" section? When an image is manually added to that section, it previews the image, I want my image to react the same as if it were done manually. @charlietfl – Shane Marotta Jun 08 '21 at 00:51

0 Answers0