Permalink
Browse files

Ability to specify endpoint mappings.

Also fix a bug with safe id.
  • Loading branch information...
1 parent 125e1c2 commit 37c945ac158795b78f1182ec908941ed8835e038 @satazor satazor committed Dec 29, 2013
Showing with 40 additions and 2 deletions.
  1. +15 −0 README.md
  2. +25 −2 pmxdr-client.js
View
@@ -251,10 +251,25 @@ The client library has the following public configuration variables:
appended to. Initially set to `document.documentElement
|| document.getElementsByTagName("head")[0] || document.body ||
document.getElementsByTagName("body")[0]`.
+* `pmxdr.endpoints`: Array of endpoints so that you can change the default `pmxdr/api` per
+ origin at your desire.
* You can override defaults for a whole pmxdr instance by specifying the property under
the instance. For example: `var foo=new pmxdr("http://example.com"); foo.defaultTimeout
= 60000`.
+Example usage of `pmxdr.endpoints`:
+
+pmxdr.endpoints.push({
+ origin: 'http://example.com',
+ endpoint: '/utils/pmxdr/api
+});
+
+pmxdr.endpoints.push({
+ origin: /^https?:\/\/some\.domain\.com\/some\/project,
+ endpoint: '/some/project/pmxdr/api.php'
+});
+
+
The host library also has a private array named alwaysTrustedOrigins, which can be filled
with protocol+host+non-standard port strings and regular expressions. Any website with an
origin matching one of the strings or regular expressions in the array is allowed to skip
View
@@ -33,13 +33,14 @@
else if (instance.iFrame.attachEvent)
instance.iFrame.attachEvent("onload", onloadHandler);
- instance.iFrame.src = instance.origin + "/pmxdr/api";
+ instance.iFrame.src = instance.origin + pmxdr.getEndpoint(host);
if (typeof onload == "function") {
instance.onload = onload;
instance.init();
}
}
+ pmxdr.endpoints = [];
pmxdr.originRegex = /^([\w-]+:\/*\[?[\w\.:-]+\]?(?::\d+)?).*/; // RegExp.$1 = protocol+host+port (the square brackets are for ipv6)
pmxdr.request = function(req) {
if (typeof req == "string")
@@ -77,8 +78,30 @@
pmxdr.getSafeID = function() { // generate a random key that doesn't collide with any existing keys
var randID = Math.random().toString().substr(2); // Generate a random number, make it a string, and cut off the "0." to make it look nice
if (typeof pmxdr.requests[randID] == "undefined") return randID; // key doesn't collide
- else return safeRandID();
+ else return pmxdr.getSafeID();
}
+
+ pmxdr.getEndpoint = function(uri) {
+ var x,
+ endpoint,
+ endpoints = pmxdr.endpoints,
+ length = endpoints.length;
+
+ for (x = 0; x < length; x++) {
+ endpoint = endpoints[x];
+
+ if (typeof endpoint.origin === "string") {
+ if (!uri.indexOf(endpoint.origin)) {
+ break;
+ }
+ } else if (endpoint.origin.test(uri)) {
+ break;
+ }
+ }
+
+ return endpoint ? endpoint.endpoint : "/pmxdr/api";
+ }
+
pmxdr.prototype = {
init: function(onload) { // load or reload iframe

0 comments on commit 37c945a

Please sign in to comment.