Last active
February 13, 2017 11:28
-
-
Save beckettkev/77b7c7cf220826d0ca0f417d8b85e545 to your computer and use it in GitHub Desktop.
Helper functions for extracting the App Web and Host URL - JavaScript (es6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDocumentQueryStrings() { | |
const sections = document.URL.split('?'); | |
return sections.length > 1 ? sections[1].split('&') : []; | |
} | |
function S4() { | |
return (((1 + Math.random())*0x10000)|0).toString(16).substring(1); | |
} | |
module.exports = { | |
baseUrl(host) { | |
let hostname = host; | |
if (hostname && hostname.match(/[-][\w]+[-]/g)) { | |
hostname = hostname.match(/[-][\w]+[-]/g)[0]; | |
hostname = hostname.substring(1, hostname.length - 1); | |
hostname = `${window.location.protocol}//twad${hostname}.sharepoint.com${(window.location.port ? ':' + window.location.port : '')}`; | |
} | |
return hostname; | |
}, | |
baseAssetSiteUrl() { | |
return `${this.baseUrl(window.location.origin)}/sites/`; | |
}, | |
getGuid() { | |
return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase(); | |
}, | |
getQueryStringParameter(param) { | |
const el = getDocumentQueryStrings().filter(keyPair => keyPair.split('=')[0] === param); | |
return el.length > 0 ? window.decodeURIComponent(el[0].split('=')[1]) : null; | |
}, | |
getSpContaxtUrlParams() { | |
return { | |
hostWebUrl: window.decodeURIComponent(this.getQueryStringParameter('SPHostUrl')), | |
appWebUrl: window.decodeURIComponent(this.getQueryStringParameter('SPAppWebUrl')), | |
spLanguage: window.decodeURIComponent(this.getQueryStringParameter('SPLanguage')) | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment