automatically detect the btcpay server url in btcpay.js

This commit is contained in:
nicolas.dorier
2018-11-10 23:43:48 +09:00
parent 16b57f24a2
commit dc749462ec
2 changed files with 18 additions and 4 deletions

View File

@@ -6,10 +6,6 @@
@section HeadScripts {
<script src="~/modal/btcpay.js"></script>
<script type="text/javascript">
btcpay.setApiUrlPrefix("@rootUrl");
</script>
}
<section>

View File

@@ -1,5 +1,19 @@
/* jshint browser: true, strict: false, maxlen: false, maxstatements: false */
(function () {
var supportsCurrentScript = ("currentScript" in document);
var thisScript = "";
if (supportsCurrentScript) {
thisScript = document.currentScript.src;
}
else {
for (var i = 0; i < document.scripts.length; i++) {
var script = document.scripts[i];
if (script.src.endsWith("btcpay.js")) {
thisScript = script.src;
}
}
}
function warn() {
if (window.console && window.console.warn) {
window.console.warn.apply(window.console, arguments);
@@ -25,6 +39,10 @@
iframe.style.zIndex = '2000';
var origin = 'http://slack.btcpayserver.org join us there, and initialize this with your origin url through setApiUrlPrefix';
if (thisScript.endsWith("/modal/btcpay.js")) {
// We can't just take the domain as btcpay can run under a sub path with RootPath
origin = thisScript.substr(0, thisScript.length - "/modal/btcpay.js".length);
}
// urlPrefix should be site root without trailing slash
function setApiUrlPrefix(urlPrefix) {
origin = stripTrailingSlashes(urlPrefix);