aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/js/captions.js35
-rw-r--r--src/js/utils.js19
2 files changed, 47 insertions, 7 deletions
diff --git a/src/js/captions.js b/src/js/captions.js
index 6ab8c69e..c8bc5833 100644
--- a/src/js/captions.js
+++ b/src/js/captions.js
@@ -1,5 +1,6 @@
// ==========================================================================
// Plyr Captions
+// TODO: Create as class
// ==========================================================================
import support from './support';
@@ -45,7 +46,6 @@ const captions = {
return;
}
-
// Inject the container
if (!utils.is.element(this.elements.captions)) {
this.elements.captions = utils.createElement('div', utils.getAttributesFromSelector(this.config.selectors.captions));
@@ -56,11 +56,42 @@ const captions = {
// Set the class hook
utils.toggleClass(this.elements.container, this.config.classNames.captions.enabled, !utils.is.empty(captions.getTracks.call(this)));
+ // Get tracks
+ const tracks = captions.getTracks.call(this);
+
// If no caption file exists, hide container for caption text
- if (utils.is.empty(captions.getTracks.call(this))) {
+ if (utils.is.empty(tracks)) {
return;
}
+ // Get browser info
+ const browser = utils.getBrowser();
+
+ // Fix IE captions if CORS is used
+ // Fetch captions and inject as blobs instead (data URIs not supported!)
+ if (browser.isIE && window.URL) {
+ const elements = this.media.querySelectorAll('track');
+
+ Array.from(elements).forEach(track => {
+ const src = track.getAttribute('src');
+ const href = utils.parseUrl(src);
+
+ if (href.hostname !== window.location.href.hostname && [
+ 'http:',
+ 'https:',
+ ].includes(href.protocol)) {
+ utils
+ .fetch(src, 'blob')
+ .then(blob => {
+ track.setAttribute('src', window.URL.createObjectURL(blob));
+ })
+ .catch(() => {
+ utils.removeElement(track);
+ });
+ }
+ });
+ }
+
// Set language
captions.setLanguage.call(this);
diff --git a/src/js/utils.js b/src/js/utils.js
index 5fa4e1ed..3e176d10 100644
--- a/src/js/utils.js
+++ b/src/js/utils.js
@@ -83,7 +83,7 @@ const utils = {
// Fetch wrapper
// Using XHR to avoid issues with older browsers
- fetch(url) {
+ fetch(url, responseType = 'text') {
return new Promise((resolve, reject) => {
try {
const request = new XMLHttpRequest();
@@ -94,10 +94,15 @@ const utils = {
}
request.addEventListener('load', () => {
- try {
- resolve(JSON.parse(request.responseText));
- } catch(e) {
- resolve(request.responseText);
+ if (responseType === 'text') {
+ try {
+ resolve(JSON.parse(request.responseText));
+ } catch(e) {
+ resolve(request.responseText);
+ }
+ }
+ else {
+ resolve(request.response);
}
});
@@ -106,6 +111,10 @@ const utils = {
});
request.open('GET', url, true);
+
+ // Set the required response type
+ request.responseType = responseType;
+
request.send();
} catch (e) {
reject(e);