Pass parameter to BLOB object URL

前端 未结 3 1047
醉话见心
醉话见心 2020-12-21 09:49

Say I\'ve got a reference to a html file as a Blob b and I create a URL for it, url = URL.createObjectURL(b);.

This gives

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 10:16

    If you are doing this with a Javascript Blob for say a WebWorker then you can just to add the parameters into the Blob constructor as a global variable:

    const parameters = 'parameters = ' + JSON.stringify({foo:'bar'});
    const body = response.body; // From some previous HTTP request
    const blob = new Blob([parameters, body], { type: 'application/javascript' });
    new Worker(URL.createObjectURL(blob));
    

    Or more general case just store the original URL on the location object

    const location = 'location.originalHref = "' + url + '";';
    const body = response.body; // From some previous HTTP request
    const blob = new Blob([location, body], { type: 'application/javascript' });
    new Worker(URL.createObjectURL(blob));
    

    You could also do this with HTML if you can add them say to the root tag as attributes or use the element for the url or insert them as a script tag but this would require you to modify the response HTML rather then just prepend some extra data

提交回复
热议问题