diff options
author | Sam Potts <sam@potts.es> | 2018-02-19 09:52:46 +1100 |
---|---|---|
committer | Sam Potts <sam@potts.es> | 2018-02-19 09:52:46 +1100 |
commit | 24b4220de5708e95c909fa28add772ed05c19c17 (patch) | |
tree | 20576792940de6a32fbab9381969000f45f8cd16 /src | |
parent | f1895a4cceecfcedca9761f63ce10351338fe047 (diff) | |
download | plyr-24b4220de5708e95c909fa28add772ed05c19c17.tar.lz plyr-24b4220de5708e95c909fa28add772ed05c19c17.tar.xz plyr-24b4220de5708e95c909fa28add772ed05c19c17.zip |
Fix IE CORS captions
Diffstat (limited to 'src')
-rw-r--r-- | src/js/captions.js | 35 | ||||
-rw-r--r-- | src/js/utils.js | 19 |
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); |