Pass large blob or file from chrome extension

前端 未结 2 1631
南笙
南笙 2020-12-20 19:48

I try to write an extension caching some large media files used on my website so you can locally cache those files when the extension is installed:

  1. I pass the
相关标签:
2条回答
  • 2020-12-20 20:23

    You're almost there.

    After creating the blob:-URL on the background page and passing it to the content script, don't forward it to the web page. Instead, retrieve the blob using XMLHttpRequest, create a new blob:-URL, then send it to the web page.

    // assuming that you've got a valid blob:chrome-extension-URL...
    var blobchromeextensionurlhere = 'blob:chrome-extension....';
    var x = new XMLHttpRequest();
    x.open('GET', blobchromeextensionurlhere);
    x.responseType = 'blob';
    x.onload = function() {
        var url = URL.createObjectURL(x.response);
        // Example: blob:http%3A//example.com/17e9d36c-f5cd-48e6-b6b9-589890de1d23
        // Now pass url to the page, e.g. using postMessage
    };
    x.send();
    

    If your current setup does not use content scripts, but e.g. the webRequest API to redirect request to the cached result, then another option is to use data-URIs (a File or Blob can be converted to a data-URI using <FileReader>.readAsDataURL. Data-URIs cannot be read using XMLHttpRequest, but this will be possible in future versions of Chrome (http://crbug.com/308768).

    0 讨论(0)
  • 2020-12-20 20:28

    Two possibilities I can think of.

    1) Employ externally_connectable.

    This method is described in the docs here.

    The essence of it: you can declare that such and such webpage can pass messages to your extension, and then chrome.runtime.connect and chrome.runtime.sendMessage will be exposed to the webpage.

    You can then probably make the webpage open a port to your extension and use it for data. Note that only the webpage can initiate the connection.

    2) Use window.PostMessage.

    The method is mentioned in the docs (note the obsolete mention of window.webkitPostMessage) and described in more detail here.

    You can, as far as I can tell from documentation of the method (from various places), pass any object with it, including blobs.

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