build a chrome extension in order to upload images (from clipboard)

后端 未结 1 947
名媛妹妹
名媛妹妹 2020-12-15 02:11

I wanted to write a simple chrome extension in order to substitute the following sequence of steps which i have to do very often for university:

    <
相关标签:
1条回答
  • 2020-12-15 02:39

    To answer your questions, I will give you some hints and resources to do what you want:

    1 - Screenshot using Chrome Extensions API

    You can use chrome.tabs.captureVisibleTab to screenshot what you see.

    chrome.tabs.captureVisibleTab(null, {format:'png'}, function(dataURL) {
      // Your image is in the dataURL
    });
    

    2 - Edit Screenshot using HTML5

    Well, here is a tricky one, why do you want to use Paint while you can use HTML5 or a web service? If you want to use paint, then the only way doing this is natively through NPAPI (C++). Exposing something natively is really discouraged since it poses additional security risks to users. And it requires manual review for submission and a deadly warning when installing.

    Why can't you use HTML5 Canvas to modify the screenshot? Or even, use Picnik online photo editing http://www.picnik.com/

    var image_buffer = document.createElement('img');
    image_buffer.src = dataURL; // From #1 capture tab
    image_buffer.onload = function() {
      var canvas = document.createElement('canvas');
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
      var ctx = canvas.getContext('2d');
      ctx.drawImage(image_buffer, 0, 0);
    
      // Draw something extra ontop of it such as a circle.
      ctx.beginPath();
      ctx.arc(0, 0, 10, 0, Math.PI*2, true); 
      ctx.closePath();
      ctx.fill();
    
      // Convert that back to a dataURL
      var dataURL = canvas.toDataURL('image/png');
    
      // Base64 image only.
      var image64 = dataURL.replace(/data:image\/png;base64,/, '');
    };
    

    3 - Save Image to Hard drive

    This is another tricky one, right now, if you use a "service" like Picnick, then you can use their saving utility to save to your harddrive, otherwise you can use HTML5 FileWriter API which is currently being developed.

    If you really want to with your MSPaint route, then you would need to use NPAPI as mentioned above.

    But when you use HTML5, you can do the following, but it is still early in spec:

    var bb = new BlobBuilder();
    bb.append(image64); // From #2 when done editing.
    var blob = bb.getBlob(); 
    location.href = createObjectURL(blob);
    

    4 - Upload image to an Online Image Service

    You can use http://imgurl.com to upload too, it has a neat API that you can use. All you need to know is plain old javascript, just send an XHR request to request the service and communicate with it.

    To do that, just use their API:

    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://api.imgur.com/2/upload.json?key=' + apikey, true); 
    xhr.setRequestHeader('Cache-Control', 'no-cache');
    xhr.onreadystatechange = function() {
      if (this.readyState == 4) {
          var response = JSON.parse(xhr.response);
          if (response.error) {
            alert('Error: ' + response.error.message);
            return;
          }
          var image_url = response.upload.links.original;
      }
    };
    xhr.send(image64); // From #2 where we edit the screenshot.
    

    5 - Share link of image with others.

    Same as above, this is just plain old javascript, depends on the service (Facebook, Twitter, Buzz), you use their API or another service does that already for you to share the images.

    Note:

    I would say the best way to do this extension is using HTML5. You can use XHR to communicate to external websites, Canvas to edit the photos, and FileWriter to persist that to disk.

    I would highly discourage the NPAPI approach for such extension since it isn't needed. Plus, if you go through NPAPI, you would need to make it cross platform and develop plugins for each browser.

    0 讨论(0)
提交回复
热议问题