Skip to content

Commit

Permalink
Support for XHR options (katspaugh#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
janhartmann authored and thijstriemstra committed Feb 14, 2018
1 parent e8dd12e commit 0730684
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ wavesurfer.js changelog
next (unreleased)
-----------------

- Added `xhr` option to configure util.ajax for authorization (#1310, #1038, #1100)
- Fix `setCurrentTime` method (#1292)
- Fix `getScrollX` method: Check bounds when `scrollParent: true` (#1312)
- Minimap plugin: fix initial load, canvas click did not work (#1265)
Expand Down
14 changes: 14 additions & 0 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ export default function ajax(options) {
let fired100 = false;
xhr.open(options.method || 'GET', options.url, true);
xhr.responseType = options.responseType || 'json';

if (options.xhr) {
if (options.xhr.requestHeaders) {
// add custom request headers
for (let header in options.xhr.requestHeaders) {
xhr.setRequestHeader(header.key, header.value);
}
}
if (options.xhr.withCredentials) {
// use credentials
xhr.withCredentials = true;
}
}

xhr.addEventListener('progress', e => {
instance.fireEvent('progress', e);
if (e.lengthComputable && e.loaded == e.total) {
Expand Down
7 changes: 5 additions & 2 deletions src/wavesurfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import PeakCache from './peakcache';
* the channels of the audio
* @property {string} waveColor='#999' The fill color of the waveform after the
* cursor.
* @property {object} xhr={} XHR options.
*/

/**
Expand Down Expand Up @@ -207,7 +208,8 @@ export default class WaveSurfer extends util.Observer {
scrollParent: false,
skipLength: 2,
splitChannels: false,
waveColor: '#999'
waveColor: '#999',
xhr: {}
};

/** @private */
Expand Down Expand Up @@ -1308,7 +1310,8 @@ export default class WaveSurfer extends util.Observer {
getArrayBuffer(url, callback) {
const ajax = util.ajax({
url: url,
responseType: 'arraybuffer'
responseType: 'arraybuffer',
xhr: this.params.xhr
});

this.currentAjax = ajax;
Expand Down

0 comments on commit 0730684

Please sign in to comment.