concise handling of cors urls

This commit is contained in:
Matt Walsh 2020-09-24 19:51:29 -05:00
parent 34e7775838
commit e8c7dfa6a7
3 changed files with 21 additions and 8 deletions

View file

@ -28,8 +28,8 @@ class Almanac extends WeatherDisplay {
// get images for outlook // get images for outlook
const imagePromises = [ const imagePromises = [
utils.image.load('products/predictions/30day/off14_temp.gif'), utils.image.load('https://www.cpc.ncep.noaa.gov/products/predictions/30day/off14_temp.gif', true),
utils.image.load('products/predictions/30day/off14_prcp.gif'), utils.image.load('https://www.cpc.ncep.noaa.gov/products/predictions/30day/off14_prcp.gif', true),
]; ];
// get sun/moon data // get sun/moon data

View file

@ -53,12 +53,12 @@ class Radar extends WeatherDisplay {
if (weatherParameters.State === 'HI') src = 'images/HawaiiRadarMap2.png'; if (weatherParameters.State === 'HI') src = 'images/HawaiiRadarMap2.png';
this.baseMap = await utils.image.load(src); this.baseMap = await utils.image.load(src);
const baseUrl = 'Conus/RadarImg/'; const baseUrl = 'https://radar.weather.gov/Conus/RadarImg/';
let radarHtml; let radarHtml;
try { try {
// get a list of available radars // get a list of available radars
radarHtml = await $.ajax({ radarHtml = await $.ajaxCORS({
type: 'GET', type: 'GET',
url: baseUrl, url: baseUrl,
dataType: 'text', dataType: 'text',

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
// radar utilities // radar utilities
/* globals _Units, Units, SuperGif */ /* globals SuperGif */
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
const utils = (() => { const utils = (() => {
// ****************************** weather data ******************************** // ****************************** weather data ********************************
@ -23,7 +23,7 @@ const utils = (() => {
// ****************************** load images ********************************* // ****************************** load images *********************************
// load an image from a blob or url // load an image from a blob or url
const loadImg = (imgData) => { const loadImg = (imgData, cors = false) => {
return new Promise(resolve => { return new Promise(resolve => {
const img = new Image(); const img = new Image();
img.onload = (e) => { img.onload = (e) => {
@ -32,7 +32,9 @@ const utils = (() => {
if (imgData instanceof Blob) { if (imgData instanceof Blob) {
img.src = window.URL.createObjectURL(imgData); img.src = window.URL.createObjectURL(imgData);
} else { } else {
img.src = imgData; let url = imgData;
if (cors) url = rewriteUrl(imgData);
img.src = url;
} }
}); });
}; };
@ -172,6 +174,14 @@ const utils = (() => {
return r.join('\n').replace(/\n /g, '\n'); return r.join('\n').replace(/\n /g, '\n');
}; };
// ********************************* cors ********************************************
// rewrite some urls for local server
const rewriteUrl = (url) => {
url = url.replace('https://api.weather.gov/', '');
url = url.replace('https://radar.weather.gov/', '');
url = url.replace('https://www.cpc.ncep.noaa.gov/', '');
return url;
};
// return an orderly object // return an orderly object
return { return {
@ -207,13 +217,16 @@ const utils = (() => {
string: { string: {
wordWrap, wordWrap,
}, },
cors: {
rewriteUrl,
},
}; };
})(); })();
// pass data through local server as CORS workaround // pass data through local server as CORS workaround
$.ajaxCORS = function (e) { $.ajaxCORS = function (e) {
// modify the URL // modify the URL
e.url = e.url.replace('https://api.weather.gov/', ''); e.url = utils.cors.rewriteUrl(e.url);
// call the ajax function // call the ajax function
return $.ajax(e); return $.ajax(e);