From f315376ac7ab8122407f5de6834155fde0121312 Mon Sep 17 00:00:00 2001 From: Sander Date: Wed, 9 Jan 2019 10:53:57 -0500 Subject: [PATCH 1/2] Switching json to jsonp to avoid CORS --- jquery.simplerWeather.js | 275 +++++++++++++++++++---------------- jquery.simplerWeather.min.js | 5 +- 2 files changed, 152 insertions(+), 128 deletions(-) diff --git a/jquery.simplerWeather.js b/jquery.simplerWeather.js index 7b29afc..69962c9 100644 --- a/jquery.simplerWeather.js +++ b/jquery.simplerWeather.js @@ -6,134 +6,161 @@ Based on SimpleWeather -- https://github.com/monkeecreate/jquery.simpleWeather Simple Weather Java Switches to DarkSky */ -( function( $ ) { - 'use strict'; - - function getAltTemp( unit, temp ) { - if( unit === 'f' ) { - return Math.round( ( 5.0 / 9.0 ) * ( temp - 32.0 ) ); - } else { - return Math.round( ( 9.0 / 5.0 ) * temp + 32.0 ); - } - } - - $.extend( { - simplerWeather: function( options ) { - options = $.extend( { - location: '', - units: 'f', - authmethod: 'apikey', - apikey: '', - proxyurl: '', - forecast: 'true', - forecastdays: '4', - success: function( weather ) {}, - error: function( message ) {} - }, options ); - - let location = ''; - - //Sets the units based on https://darksky.net/dev/docs - if( options.units.toLowerCase() === 'c' ) { - var units = 'si' - } else { - var units = 'us' - } - - //Check that the latitude and longitude has been set and generate the API URL based on authentication method - function getWeatherURL( authmethod ) { - if( /^\-?\d+(\.\d+)?,\s*\-?\d+(\.\d+)$/.test( options.location ) ) { - - let geoLocation = options.location.split( ',' ); - var lat = geoLocation[ 0 ]; - var lon = geoLocation[ 1 ]; - } else { - options.error( - 'Could not retrieve weather due to an invalid location. Must enter location as latitude,longitude' - ); - } - if( authmethod === "apikey" && options.apikey !== '' ) { - let apiKey = encodeURIComponent( options.apikey ); - return 'https://api.darksky.net/forecast/' + apiKey + '/' + - lat + ',' + lon + '/?units=' + units + - '&exclude=minutely,hourly,alerts,flags'; - } else if( authmethod === "proxy" && options.proxyurl !== '' ) { - return encodeURI( options.proxyurl + '?lat=' + lat + '&lon=' + - lon + '&units=' + units ); +;(function($) { + 'use strict' + + function getAltTemp(unit, temp) { + if (unit === 'f') { + return Math.round((5.0 / 9.0) * (temp - 32.0)) } else { - options.error( - 'Could not retrieve weather due to an invalid api key or proxy setting.' - ); + return Math.round((9.0 / 5.0) * temp + 32.0) } - } - - $.getJSON( - encodeURI( getWeatherURL( options.authmethod ) ), - function( data ) { - if( data !== null ) { - var result = data, - weather = {}; - - weather.temp = result.currently.temperature; - weather.currently = result.currently.summary; - weather.icon = result.currently.icon; - weather.pressure = result.currently.pressure; - weather.humidity = result.currently.humidity; - weather.visibility = result.currently.visibility; - weather.updated = result.currently.time; - - weather.high = result.daily.data[ 0 ].temperatureHigh; - weather.low = result.daily.data[ 0 ].temperatureLow; - weather.sunrise = result.daily.data[ 0 ].sunriseTime; - weather.sunset = result.daily.data[ 0 ].sunsetTime; - weather.description = result.daily.data[ 0 ].summary; - - weather.attributionlink = "https://darksky.net/"; - weather.unit = options.units.toLowerCase(); - - if( weather.unit === 'f' ) { - weather.altunit = 'c'; + } + + $.extend({ + simplerWeather: function(options) { + options = $.extend( + { + location: '', + units: 'f', + authmethod: 'apikey', + apikey: '', + proxyurl: '', + forecast: 'true', + forecastdays: '4', + success: function(weather) {}, + error: function(message) {} + }, + options + ) + + let location = '' + + //Sets the units based on https://darksky.net/dev/docs + if (options.units.toLowerCase() === 'c') { + var units = 'si' } else { - weather.altunit = 'f'; + var units = 'us' } - weather.alt = { - temp: getAltTemp( options.units, weather.temp ), - high: getAltTemp( options.unit, weather.high ), - low: getAltTemp( options.unit, weather.low ) - }; - - if( options.forcast && - parseInt( options.forecastdays ) !== "NaN" ) { - - weather.forecast = []; - - for( var i = 0; i < options.forecastdays; i++ ) { - var forecast = result.daily.data[ i ]; - forecast.date = forecast.time; - forecast.summary = forecast.summary; - forecast.high = forecast.temperatureHigh; - forecast.low = forecast.temperatureLow; - forecast.icon = forecast.icon; - - - forecast.alt = { - high: getAltTemp( options.units, forecast.temperatureHigh ), - low: getAltTemp( options.units, forecast.temperatureLow ) - }; - - weather.forecast.push( forecast ); - } + //Check that the latitude and longitude has been set and generate the API URL based on authentication method + function getWeatherURL(authmethod) { + if ( + /^\-?\d+(\.\d+)?,\s*\-?\d+(\.\d+)$/.test(options.location) + ) { + let geoLocation = options.location.split(',') + var lat = geoLocation[0] + var lon = geoLocation[1] + } else { + options.error( + 'Could not retrieve weather due to an invalid location. Must enter location as latitude,longitude' + ) + } + if (authmethod === 'apikey' && options.apikey !== '') { + let apiKey = encodeURIComponent(options.apikey) + return ( + 'https://api.darksky.net/forecast/' + + apiKey + + '/' + + lat + + ',' + + lon + + '/?units=' + + units + + '&exclude=minutely,hourly,alerts,flags' + ) + } else if (authmethod === 'proxy' && options.proxyurl !== '') { + return encodeURI( + options.proxyurl + + '?lat=' + + lat + + '&lon=' + + lon + + '&units=' + + units + ) + } else { + options.error( + 'Could not retrieve weather due to an invalid api key or proxy setting.' + ) + } } - options.success( weather ); - } else { - options.error( - 'There was a problem retrieving the latest weather information.' - ); - } + + $.ajax({ + url: encodeURI(getWeatherURL(options.authmethod)), + dataType: 'jsonp', + success: function(data) { + if (data !== null) { + var result = data, + weather = {} + + weather.temp = + Math.floor(result.currently.temperature) + '°' + weather.currently = result.currently.summary + weather.icon = result.currently.icon + weather.pressure = result.currently.pressure + weather.humidity = result.currently.humidity + weather.visibility = result.currently.visibility + weather.updated = result.currently.time + + weather.high = result.daily.data[0].temperatureHigh + weather.low = result.daily.data[0].temperatureLow + weather.sunrise = result.daily.data[0].sunriseTime + weather.sunset = result.daily.data[0].sunsetTime + weather.description = result.daily.data[0].summary + + weather.attributionlink = 'https://darksky.net/' + weather.unit = options.units.toLowerCase() + + if (weather.unit === 'f') { + weather.altunit = 'c' + } else { + weather.altunit = 'f' + } + + weather.alt = { + temp: getAltTemp(options.units, weather.temp), + high: getAltTemp(options.unit, weather.high), + low: getAltTemp(options.unit, weather.low) + } + + if ( + options.forcast && + parseInt(options.forecastdays) !== 'NaN' + ) { + weather.forecast = [] + + for (var i = 0; i < options.forecastdays; i++) { + var forecast = result.daily.data[i] + forecast.date = forecast.time + forecast.summary = forecast.summary + forecast.high = forecast.temperatureHigh + forecast.low = forecast.temperatureLow + forecast.icon = forecast.icon + + forecast.alt = { + high: getAltTemp( + options.units, + forecast.temperatureHigh + ), + low: getAltTemp( + options.units, + forecast.temperatureLow + ) + } + + weather.forecast.push(forecast) + } + } + options.success(weather) + } else { + options.error( + 'There was a problem retrieving the latest weather information.' + ) + } + } + }) + return this } - ); - return this; - } - } ); -} )( jQuery ); + }) +})(jQuery) diff --git a/jquery.simplerWeather.min.js b/jquery.simplerWeather.min.js index 25d82e5..684cab9 100644 --- a/jquery.simplerWeather.min.js +++ b/jquery.simplerWeather.min.js @@ -6,7 +6,4 @@ License: MIT Based on SimpleWeather -- https://github.com/monkeecreate/jquery.simpleWeather Simple Weather Java Switches to DarkSky */ -(function(f){function e(b,e){return"f"===b?Math.round(5/9*(e-32)):Math.round(1.8*e+32)}f.extend({simplerWeather:function(b){b=f.extend({location:"",units:"f",authmethod:"apikey",apikey:"",proxyurl:"",forecast:"true",forecastdays:"4",success:function(b){},error:function(b){}},b);var g="c"===b.units.toLowerCase()?"si":"us";f.getJSON(encodeURI(function(c){if(/^\-?\d+(\.\d+)?,\s*\-?\d+(\.\d+)$/.test(b.location))var a=b.location.split(","),e=a[0],a=a[1];else b.error("Could not retrieve weather due to an invalid location. Must enter location as latitude,longitude"); -if("apikey"===c&&""!==b.apikey)return"https://api.darksky.net/forecast/"+encodeURIComponent(b.apikey)+"/"+e+","+a+"/?units="+g+"&exclude=minutely,hourly,alerts,flags";if("proxy"===c&&""!==b.proxyurl)return encodeURI(b.proxyurl+"?lat="+e+"&lon="+a+"&units="+g);b.error("Could not retrieve weather due to an invalid api key or proxy setting.")}(b.authmethod)),function(c){if(null!==c){var a={};a.temp=c.currently.temperature;a.currently=c.currently.summary;a.icon=c.currently.icon;a.pressure=c.currently.pressure; -a.humidity=c.currently.humidity;a.visibility=c.currently.visibility;a.updated=c.currently.time;a.high=c.daily.data[0].temperatureHigh;a.low=c.daily.data[0].temperatureLow;a.sunrise=c.daily.data[0].sunriseTime;a.sunset=c.daily.data[0].sunsetTime;a.description=c.daily.data[0].summary;a.attributionlink="https://darksky.net/";a.unit=b.units.toLowerCase();a.altunit="f"===a.unit?"c":"f";a.alt={temp:e(b.units,a.temp),high:e(b.unit,a.high),low:e(b.unit,a.low)};if(b.forcast&&"NaN"!==parseInt(b.forecastdays)){a.forecast= -[];for(var f=0;f Date: Wed, 9 Jan 2019 10:57:53 -0500 Subject: [PATCH 2/2] Matched original formatting --- jquery.simplerWeather.js | 276 ++++++++++++++++------------------- jquery.simplerWeather.min.js | 2 +- 2 files changed, 126 insertions(+), 152 deletions(-) diff --git a/jquery.simplerWeather.js b/jquery.simplerWeather.js index 69962c9..33c4cf2 100644 --- a/jquery.simplerWeather.js +++ b/jquery.simplerWeather.js @@ -6,161 +6,135 @@ Based on SimpleWeather -- https://github.com/monkeecreate/jquery.simpleWeather Simple Weather Java Switches to DarkSky */ -;(function($) { - 'use strict' - - function getAltTemp(unit, temp) { - if (unit === 'f') { - return Math.round((5.0 / 9.0) * (temp - 32.0)) +( function( $ ) { + 'use strict'; + + function getAltTemp( unit, temp ) { + if( unit === 'f' ) { + return Math.round( ( 5.0 / 9.0 ) * ( temp - 32.0 ) ); + } else { + return Math.round( ( 9.0 / 5.0 ) * temp + 32.0 ); + } + } + + $.extend( { + simplerWeather: function( options ) { + options = $.extend( { + location: '', + units: 'f', + authmethod: 'apikey', + apikey: '', + proxyurl: '', + forecast: 'true', + forecastdays: '4', + success: function( weather ) {}, + error: function( message ) {} + }, options ); + + let location = ''; + + //Sets the units based on https://darksky.net/dev/docs + if( options.units.toLowerCase() === 'c' ) { + var units = 'si' + } else { + var units = 'us' + } + + //Check that the latitude and longitude has been set and generate the API URL based on authentication method + function getWeatherURL( authmethod ) { + if( /^\-?\d+(\.\d+)?,\s*\-?\d+(\.\d+)$/.test( options.location ) ) { + + let geoLocation = options.location.split( ',' ); + var lat = geoLocation[ 0 ]; + var lon = geoLocation[ 1 ]; } else { - return Math.round((9.0 / 5.0) * temp + 32.0) + options.error( + 'Could not retrieve weather due to an invalid location. Must enter location as latitude,longitude' + ); } - } - - $.extend({ - simplerWeather: function(options) { - options = $.extend( - { - location: '', - units: 'f', - authmethod: 'apikey', - apikey: '', - proxyurl: '', - forecast: 'true', - forecastdays: '4', - success: function(weather) {}, - error: function(message) {} - }, - options - ) - - let location = '' - - //Sets the units based on https://darksky.net/dev/docs - if (options.units.toLowerCase() === 'c') { - var units = 'si' + if( authmethod === "apikey" && options.apikey !== '' ) { + let apiKey = encodeURIComponent( options.apikey ); + return 'https://api.darksky.net/forecast/' + apiKey + '/' + + lat + ',' + lon + '/?units=' + units + + '&exclude=minutely,hourly,alerts,flags'; + } else if( authmethod === "proxy" && options.proxyurl !== '' ) { + return encodeURI( options.proxyurl + '?lat=' + lat + '&lon=' + + lon + '&units=' + units ); + } else { + options.error( + 'Could not retrieve weather due to an invalid api key or proxy setting.' + ); + } + } + + $.ajax({ + url: encodeURI(getWeatherURL(options.authmethod)), + dataType: 'jsonp', + success: function(data) { + if( data !== null ) { + var result = data, + weather = {}; + + weather.temp = result.currently.temperature; + weather.currently = result.currently.summary; + weather.icon = result.currently.icon; + weather.pressure = result.currently.pressure; + weather.humidity = result.currently.humidity; + weather.visibility = result.currently.visibility; + weather.updated = result.currently.time; + + weather.high = result.daily.data[ 0 ].temperatureHigh; + weather.low = result.daily.data[ 0 ].temperatureLow; + weather.sunrise = result.daily.data[ 0 ].sunriseTime; + weather.sunset = result.daily.data[ 0 ].sunsetTime; + weather.description = result.daily.data[ 0 ].summary; + + weather.attributionlink = "https://darksky.net/"; + weather.unit = options.units.toLowerCase(); + + if( weather.unit === 'f' ) { + weather.altunit = 'c'; } else { - var units = 'us' + weather.altunit = 'f'; } - //Check that the latitude and longitude has been set and generate the API URL based on authentication method - function getWeatherURL(authmethod) { - if ( - /^\-?\d+(\.\d+)?,\s*\-?\d+(\.\d+)$/.test(options.location) - ) { - let geoLocation = options.location.split(',') - var lat = geoLocation[0] - var lon = geoLocation[1] - } else { - options.error( - 'Could not retrieve weather due to an invalid location. Must enter location as latitude,longitude' - ) - } - if (authmethod === 'apikey' && options.apikey !== '') { - let apiKey = encodeURIComponent(options.apikey) - return ( - 'https://api.darksky.net/forecast/' + - apiKey + - '/' + - lat + - ',' + - lon + - '/?units=' + - units + - '&exclude=minutely,hourly,alerts,flags' - ) - } else if (authmethod === 'proxy' && options.proxyurl !== '') { - return encodeURI( - options.proxyurl + - '?lat=' + - lat + - '&lon=' + - lon + - '&units=' + - units - ) - } else { - options.error( - 'Could not retrieve weather due to an invalid api key or proxy setting.' - ) - } - } + weather.alt = { + temp: getAltTemp( options.units, weather.temp ), + high: getAltTemp( options.unit, weather.high ), + low: getAltTemp( options.unit, weather.low ) + }; - $.ajax({ - url: encodeURI(getWeatherURL(options.authmethod)), - dataType: 'jsonp', - success: function(data) { - if (data !== null) { - var result = data, - weather = {} - - weather.temp = - Math.floor(result.currently.temperature) + '°' - weather.currently = result.currently.summary - weather.icon = result.currently.icon - weather.pressure = result.currently.pressure - weather.humidity = result.currently.humidity - weather.visibility = result.currently.visibility - weather.updated = result.currently.time - - weather.high = result.daily.data[0].temperatureHigh - weather.low = result.daily.data[0].temperatureLow - weather.sunrise = result.daily.data[0].sunriseTime - weather.sunset = result.daily.data[0].sunsetTime - weather.description = result.daily.data[0].summary - - weather.attributionlink = 'https://darksky.net/' - weather.unit = options.units.toLowerCase() - - if (weather.unit === 'f') { - weather.altunit = 'c' - } else { - weather.altunit = 'f' - } - - weather.alt = { - temp: getAltTemp(options.units, weather.temp), - high: getAltTemp(options.unit, weather.high), - low: getAltTemp(options.unit, weather.low) - } - - if ( - options.forcast && - parseInt(options.forecastdays) !== 'NaN' - ) { - weather.forecast = [] - - for (var i = 0; i < options.forecastdays; i++) { - var forecast = result.daily.data[i] - forecast.date = forecast.time - forecast.summary = forecast.summary - forecast.high = forecast.temperatureHigh - forecast.low = forecast.temperatureLow - forecast.icon = forecast.icon - - forecast.alt = { - high: getAltTemp( - options.units, - forecast.temperatureHigh - ), - low: getAltTemp( - options.units, - forecast.temperatureLow - ) - } - - weather.forecast.push(forecast) - } - } - options.success(weather) - } else { - options.error( - 'There was a problem retrieving the latest weather information.' - ) - } - } - }) - return this + if( options.forcast && + parseInt( options.forecastdays ) !== "NaN" ) { + + weather.forecast = []; + + for( var i = 0; i < options.forecastdays; i++ ) { + var forecast = result.daily.data[ i ]; + forecast.date = forecast.time; + forecast.summary = forecast.summary; + forecast.high = forecast.temperatureHigh; + forecast.low = forecast.temperatureLow; + forecast.icon = forecast.icon; + + + forecast.alt = { + high: getAltTemp( options.units, forecast.temperatureHigh ), + low: getAltTemp( options.units, forecast.temperatureLow ) + }; + + weather.forecast.push( forecast ); + } + } + options.success( weather ); + } else { + options.error( + 'There was a problem retrieving the latest weather information.' + ); + } } - }) -})(jQuery) + }); + return this; + } + } ); +} )( jQuery ); diff --git a/jquery.simplerWeather.min.js b/jquery.simplerWeather.min.js index 684cab9..7790889 100644 --- a/jquery.simplerWeather.min.js +++ b/jquery.simplerWeather.min.js @@ -6,4 +6,4 @@ License: MIT Based on SimpleWeather -- https://github.com/monkeecreate/jquery.simpleWeather Simple Weather Java Switches to DarkSky */ -"use strict";!function(t){function e(t,e){return"f"===t?Math.round(5/9*(e-32)):Math.round(1.8*e+32)}t.extend({simplerWeather:function(r){if("c"===(r=t.extend({location:"",units:"f",authmethod:"apikey",apikey:"",proxyurl:"",forecast:"true",forecastdays:"4",success:function(t){},error:function(t){}},r)).units.toLowerCase())var i="si";else i="us";return t.ajax({url:encodeURI(function(t){if(/^\-?\d+(\.\d+)?,\s*\-?\d+(\.\d+)$/.test(r.location))var e=r.location.split(","),a=e[0],n=e[1];else r.error("Could not retrieve weather due to an invalid location. Must enter location as latitude,longitude");return"apikey"===t&&""!==r.apikey?"https://api.darksky.net/forecast/"+encodeURIComponent(r.apikey)+"/"+a+","+n+"/?units="+i+"&exclude=minutely,hourly,alerts,flags":"proxy"===t&&""!==r.proxyurl?encodeURI(r.proxyurl+"?lat="+a+"&lon="+n+"&units="+i):void r.error("Could not retrieve weather due to an invalid api key or proxy setting.")}(r.authmethod)),dataType:"jsonp",success:function(t){if(null!==t){var i=t,a={};if(a.temp=Math.floor(i.currently.temperature)+"°",a.currently=i.currently.summary,a.icon=i.currently.icon,a.pressure=i.currently.pressure,a.humidity=i.currently.humidity,a.visibility=i.currently.visibility,a.updated=i.currently.time,a.high=i.daily.data[0].temperatureHigh,a.low=i.daily.data[0].temperatureLow,a.sunrise=i.daily.data[0].sunriseTime,a.sunset=i.daily.data[0].sunsetTime,a.description=i.daily.data[0].summary,a.attributionlink="https://darksky.net/",a.unit=r.units.toLowerCase(),"f"===a.unit?a.altunit="c":a.altunit="f",a.alt={temp:e(r.units,a.temp),high:e(r.unit,a.high),low:e(r.unit,a.low)},r.forcast&&"NaN"!==parseInt(r.forecastdays)){a.forecast=[];for(var n=0;n