fix api endpoint detection

This commit is contained in:
Martin Zimmermann 2013-11-03 15:27:29 +01:00
parent d59fc2ef8e
commit d8a254c304

View File

@ -5,8 +5,7 @@ define(["q"], function(Q) {
Q.stopUnhandledRejectionTracking(); Q.stopUnhandledRejectionTracking();
Q.longStackSupport = true; Q.longStackSupport = true;
var endpoint = null, var salt = "Eech7co8Ohloopo9Ol6baimi",
salt = "Eech7co8Ohloopo9Ol6baimi",
location = window.location.pathname; location = window.location.pathname;
var rules = { var rules = {
@ -38,26 +37,30 @@ define(["q"], function(Q) {
* *
*/ */
var script, uri; var script, endpoint,
var port = window.location.port ? ":" + window.location.port : "", js = document.getElementsByTagName("script");
host = window.location.protocol + "//" + window.location.hostname + port;
var js = document.getElementsByTagName("script");
for (var i = 0; i < js.length; i++) { for (var i = 0; i < js.length; i++) {
if (js[i].src.match("require\\.js$") && js[i].dataset.main.match("/js/embed$")) { if (js[i].dataset.issoPrefix !== undefined) {
uri = js[i].dataset.main; endpoint = js[i].dataset.issoPrefix;
endpoint = uri.substr(0, uri.length - "/js/main".length);
} }
} }
if (! endpoint) { if (! endpoint) {
for (i = 0; i < js.length; i++) {
if (js[i].hasAttribute("async") || js[i].hasAttribute("defer")) {
throw "Isso's automatic configuration detection failed, please " +
"refer to https://github.com/posativ/isso#client-configuration " +
"and add a custom `data-isso-prefix` attribute.";
}
}
script = js[js.length - 1]; script = js[js.length - 1];
if (script.dataset.prefix) { if (script.dataset.prefix) {
endpoint = script.dataset.prefix; endpoint = script.dataset.prefix;
} else { } else {
uri = script.src.substring(host.length); endpoint = script.src.substring(0, script.src.length - "/js/embed.min.js".length);
endpoint = uri.substring(0, uri.length - "/js/embed.min.js".length);
} }
} }
@ -166,7 +169,7 @@ define(["q"], function(Q) {
var remote_addr = function() { var remote_addr = function() {
return curl("GET", endpoint + "/check-ip", null).then(function(rv) { return curl("GET", endpoint + "/check-ip", null).then(function(rv) {
return rv.body; return rv.body;
}); });
}; };
return { return {