Reputation: 245
I'm building chrome extension for watching videos.
I have problem with adding subtitles and captions to video. I have subtitle as string (from ajax call), and problem is that <track>
tag in html5 requires a file, (url to file).
Is there a good way to create a file from string in chrome exstension / javascript, and than accessing it via url/path?
Thx
Upvotes: 1
Views: 274
Reputation: 348962
Creating an URL from a string is very easy with the Blob
constructor and URL.createObjectURL
:
var content = 'some string';
var url = URL.createObjectURL(new Blob([content], { type: 'text/plain' }));
If you're using AJAX, then you don't need to do a string-to-blob conversion. Just set responseType = 'blob';
directly:
var x = new XMLHttpRequest();
x.open('GET', 'http://example.com/');
x.responseType = 'blob';
x.onload = function() {
var url = URL.createObjectURL(x.response);
// ...
};
x.send();
Upvotes: 2
Reputation: 14657
Instead of creating a file, you can try generating a data URI:
src = 'data:text/plain,' + encodeURIComponent(subtitleString);
or:
src = 'data:text/plain;base64,' + btoa(subtitleString);
You'll need to add encoding info if you are dealing with non US-ASCII subtitles.
Upvotes: 1