From e8c7dfa6a7e01f97ce5d323cfd89172319d09b8a Mon Sep 17 00:00:00 2001 From: Matt Walsh Date: Thu, 24 Sep 2020 19:51:29 -0500 Subject: [PATCH] concise handling of cors urls --- server/scripts/modules/almanac.js | 4 ++-- server/scripts/modules/radar.js | 4 ++-- server/scripts/modules/utilities.js | 21 +++++++++++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/server/scripts/modules/almanac.js b/server/scripts/modules/almanac.js index dfbc6e4..514d387 100644 --- a/server/scripts/modules/almanac.js +++ b/server/scripts/modules/almanac.js @@ -28,8 +28,8 @@ class Almanac extends WeatherDisplay { // get images for outlook const imagePromises = [ - utils.image.load('products/predictions/30day/off14_temp.gif'), - utils.image.load('products/predictions/30day/off14_prcp.gif'), + utils.image.load('https://www.cpc.ncep.noaa.gov/products/predictions/30day/off14_temp.gif', true), + utils.image.load('https://www.cpc.ncep.noaa.gov/products/predictions/30day/off14_prcp.gif', true), ]; // get sun/moon data diff --git a/server/scripts/modules/radar.js b/server/scripts/modules/radar.js index 4eb6fcd..cb8e1e4 100644 --- a/server/scripts/modules/radar.js +++ b/server/scripts/modules/radar.js @@ -53,12 +53,12 @@ class Radar extends WeatherDisplay { if (weatherParameters.State === 'HI') src = 'images/HawaiiRadarMap2.png'; this.baseMap = await utils.image.load(src); - const baseUrl = 'Conus/RadarImg/'; + const baseUrl = 'https://radar.weather.gov/Conus/RadarImg/'; let radarHtml; try { // get a list of available radars - radarHtml = await $.ajax({ + radarHtml = await $.ajaxCORS({ type: 'GET', url: baseUrl, dataType: 'text', diff --git a/server/scripts/modules/utilities.js b/server/scripts/modules/utilities.js index 80c57f2..765e5b5 100644 --- a/server/scripts/modules/utilities.js +++ b/server/scripts/modules/utilities.js @@ -1,7 +1,7 @@ 'use strict'; // radar utilities -/* globals _Units, Units, SuperGif */ +/* globals SuperGif */ // eslint-disable-next-line no-unused-vars const utils = (() => { // ****************************** weather data ******************************** @@ -23,7 +23,7 @@ const utils = (() => { // ****************************** load images ********************************* // load an image from a blob or url - const loadImg = (imgData) => { + const loadImg = (imgData, cors = false) => { return new Promise(resolve => { const img = new Image(); img.onload = (e) => { @@ -32,7 +32,9 @@ const utils = (() => { if (imgData instanceof Blob) { img.src = window.URL.createObjectURL(imgData); } 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'); }; + // ********************************* 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 { @@ -207,13 +217,16 @@ const utils = (() => { string: { wordWrap, }, + cors: { + rewriteUrl, + }, }; })(); // pass data through local server as CORS workaround $.ajaxCORS = function (e) { // modify the URL - e.url = e.url.replace('https://api.weather.gov/', ''); + e.url = utils.cors.rewriteUrl(e.url); // call the ajax function return $.ajax(e);