From 422e0f5ac3a84681eb4b840853247e604520414d Mon Sep 17 00:00:00 2001 From: Lou Huang Date: Mon, 12 Jun 2017 16:16:17 -0400 Subject: [PATCH 1/5] add api key swap module --- theme/base.html | 1 + theme/js/apikeys.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 theme/js/apikeys.js diff --git a/theme/base.html b/theme/base.html index fe3b36b..a20adcc 100644 --- a/theme/base.html +++ b/theme/base.html @@ -123,6 +123,7 @@

{{ site_name }}

+ {%- for path in extra_javascript %} diff --git a/theme/js/apikeys.js b/theme/js/apikeys.js new file mode 100644 index 0000000..6d9059b --- /dev/null +++ b/theme/js/apikeys.js @@ -0,0 +1,26 @@ +// your-mapzen-api-key +(function () { + var placeholder = 'your-mapzen-api-key'; + var el = document.querySelector('.documentation-content'); + if (!el) return; + + // window.fetch('/api/keys.json', { credentials: 'same-origin '}) + // .then(function (response) { + // return response.json(); + // }) + // TEMPORARY: Use a placeholder key; don't actually fetch + Promise.resolve([{key: 'test-1234567'}]) + .then(function (data) { + // Silent fail if data is not an array, or is zero-length + if (!Array.isArray(data) || data.length === 0) return; + searchAndReplaceWithKey(el, data[0].key); + }) + .catch(function (err) { + console.error('Error fetching user API keys: ', err); + }); + + function searchAndReplaceWithKey(el, key) { + var regex = new RegExp(placeholder, 'g'); + el.innerHTML = el.innerHTML.replace(regex, key); + } +})(); From 07d1e29c4d705baeb662ceef6d7fb7586ba825cb Mon Sep 17 00:00:00 2001 From: Lou Huang Date: Tue, 13 Jun 2017 14:27:57 -0400 Subject: [PATCH 2/5] Fetch API key endpoint --- theme/js/apikeys.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/theme/js/apikeys.js b/theme/js/apikeys.js index 6d9059b..a55c051 100644 --- a/theme/js/apikeys.js +++ b/theme/js/apikeys.js @@ -1,15 +1,12 @@ -// your-mapzen-api-key (function () { var placeholder = 'your-mapzen-api-key'; var el = document.querySelector('.documentation-content'); if (!el) return; - // window.fetch('/api/keys.json', { credentials: 'same-origin '}) - // .then(function (response) { - // return response.json(); - // }) - // TEMPORARY: Use a placeholder key; don't actually fetch - Promise.resolve([{key: 'test-1234567'}]) + window.fetch('/api/keys.json', { credentials: 'same-origin '}) + .then(function (response) { + return response.json(); + }) .then(function (data) { // Silent fail if data is not an array, or is zero-length if (!Array.isArray(data) || data.length === 0) return; From 62348a072c076a545a8bbdfdf62b6a904c3dd2c9 Mon Sep 17 00:00:00 2001 From: Lou Huang Date: Tue, 13 Jun 2017 15:19:36 -0400 Subject: [PATCH 3/5] Fix typo in fetch credentials --- theme/js/apikeys.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/theme/js/apikeys.js b/theme/js/apikeys.js index a55c051..76060e3 100644 --- a/theme/js/apikeys.js +++ b/theme/js/apikeys.js @@ -3,7 +3,7 @@ var el = document.querySelector('.documentation-content'); if (!el) return; - window.fetch('/api/keys.json', { credentials: 'same-origin '}) + window.fetch('/api/keys.json', { credentials: 'same-origin' }) .then(function (response) { return response.json(); }) From 26e6b3ce6b2da8f1103015534e3b338ef04fdf4b Mon Sep 17 00:00:00 2001 From: Lou Huang Date: Thu, 15 Jun 2017 14:16:57 -0400 Subject: [PATCH 4/5] Add fallback text replacer regex that is more loose --- theme/js/apikeys.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/theme/js/apikeys.js b/theme/js/apikeys.js index 76060e3..f85cf09 100644 --- a/theme/js/apikeys.js +++ b/theme/js/apikeys.js @@ -20,4 +20,7 @@ var regex = new RegExp(placeholder, 'g'); el.innerHTML = el.innerHTML.replace(regex, key); } + + // document.querySelector('.documentation-content').innerHTML = document.querySelector('.documentation-content').innerHTML.replace(/[{[]?your[_-]mapzen[_-]api[_-]key[}\]]?/gi, 'hihihih-2302323'); + })(); From 72ae775c62e56b791ad9ede8c6c0187fbcead4cf Mon Sep 17 00:00:00 2001 From: Lou Huang Date: Fri, 23 Jun 2017 13:29:23 -0700 Subject: [PATCH 5/5] xhr fallback --- theme/js/apikeys.js | 62 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 12 deletions(-) diff --git a/theme/js/apikeys.js b/theme/js/apikeys.js index f85cf09..862eb11 100644 --- a/theme/js/apikeys.js +++ b/theme/js/apikeys.js @@ -3,18 +3,56 @@ var el = document.querySelector('.documentation-content'); if (!el) return; - window.fetch('/api/keys.json', { credentials: 'same-origin' }) - .then(function (response) { - return response.json(); - }) - .then(function (data) { - // Silent fail if data is not an array, or is zero-length - if (!Array.isArray(data) || data.length === 0) return; - searchAndReplaceWithKey(el, data[0].key); - }) - .catch(function (err) { - console.error('Error fetching user API keys: ', err); - }); + // Polyfill isArray + if (!Array.isArray) { + Array.isArray = function(arg) { + return Object.prototype.toString.call(arg) === '[object Array]'; + }; + } + + // Use fetch if we have it; otherwise use XHR until fetch is polyfilled for old browsers + if (window.fetch) { + window.fetch('/api/keys.json', { credentials: 'same-origin' }) + .then(function (response) { + return response.json(); + }) + .then(processKeys) + .catch(function (err) { + console.error('Error fetching user API keys: ', err); + }); + } else { + var request = new XMLHttpRequest(); + + request.open('GET', '/api/keys.json'); + request.onerror = function (err) { + throwError(err); + }; + request.onreadystatechange = function () { + if (request.readyState === 4) { + if (request.status === 200 || request.status === 304) { + try { + var response = JSON.parse(request.responseText); + processKeys(response); + } catch (err) { + throwError(err); + } + } else { + throwError(request.status); + } + } + }; + request.send(); + } + + function throwError(err) { + console.error('Error fetching user API keys: ', err); + } + + function processKeys(data) { + // Silent fail if data is not an array, or is zero-length + if (!Array.isArray(data) || data.length === 0) return; + searchAndReplaceWithKey(el, data[0].key); + } function searchAndReplaceWithKey(el, key) { var regex = new RegExp(placeholder, 'g');