diff --git a/packages/xo-lib/index.js b/packages/xo-lib/index.js index f0d5bb920..b50b1a1e5 100644 --- a/packages/xo-lib/index.js +++ b/packages/xo-lib/index.js @@ -92,6 +92,18 @@ exports.fixUrl = fixUrl; //==================================================================== +function getCurrentUrl() { + /* global window: false */ + + if (typeof window === undefined) { + throw new Error('cannot get current URL'); + } + + return window.location.host + window.location.pathname; +} + +//==================================================================== + var ConnectionLost = makeError('ConnectionLost'); // Low level interface to XO. @@ -100,7 +112,7 @@ function Api(url) { EventEmitter.call(this); // Fix the URL (ensure correct protocol and /api/ path). - this._url = fixUrl(url); + this._url = fixUrl(url || getCurrentUrl()); // Will contains the WebSocket. this._socket = null;