From 3422e2a66f223e32b942dba834e9c7ff5f51703c Mon Sep 17 00:00:00 2001 From: Cotes Chung <11371340+cotes2020@users.noreply.github.com> Date: Thu, 19 Dec 2019 03:07:02 +0800 Subject: [PATCH] Fixed a potential JSONP callback exception. --- assets/js/_src/pageviews.js | 44 ++++++++++++++++++++------------- assets/js/dist/pageviews.min.js | 2 +- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/assets/js/_src/pageviews.js b/assets/js/_src/pageviews.js index a9b641c..cf6184e 100644 --- a/assets/js/_src/pageviews.js +++ b/assets/js/_src/pageviews.js @@ -40,11 +40,15 @@ function countPV(path, rows) { return count; } -function displayPageviews(rows, hasInit) { - if (rows === undefined) { + +function displayPageviews(data) { + if (data === undefined) { return; } + var hasInit = getInitStatus(); + var rows = data.rows; + if ($("#post-list").length > 0) { // the Home page $(".post-preview").each(function() { var path = $(this).children("h1").children("a").attr("href"); @@ -78,29 +82,35 @@ function displayPageviews(rows, hasInit) { } + +var getInitStatus = (function() { + var hasInit = false; + return function() { + if (hasInit) { + return true; + } else { + hasInit = true; + return false; + } + } +})(); + + $(function() { // load pageview if this page has .pageviews if ($('.pageviews').length > 0) { - var hasInit = false; // Get data from daily cache. - $.getJSON('/assets/data/pageviews.json', function(data) { - displayPageviews(data.rows, hasInit); - hasInit = true; - }); + $.getJSON('/assets/data/pageviews.json', displayPageviews); - $.getJSON('/assets/data/proxy.json', function(data) { + $.getJSON('/assets/data/proxy.json', function(meta) { $.ajax({ - url: data.proxyUrl, + type: 'GET', + url: meta.proxyUrl, dataType: 'jsonp', - timeout: 2000, - success: function(data) { - displayPageviews(data.rows, hasInit); - }, - error: function(xhr, status, err) { - console.log("Failed to load pageviews from proxy server."); - xhr.abort(); - return; + jsonpCallback: "displayPageviews", + error: function(jqXHR, textStatus, errorThrown) { + console.log("Failed to load pageviews from proxy server: " + errorThrown); } }); diff --git a/assets/js/dist/pageviews.min.js b/assets/js/dist/pageviews.min.js index 4af99f6..e302390 100644 --- a/assets/js/dist/pageviews.min.js +++ b/assets/js/dist/pageviews.min.js @@ -1 +1 @@ -function countUp(c,a,b){if(c0){$(".post-preview").each(function(){var h=$(this).children("h1").children("a").attr("href");var g=countPV(h,d);g=(g==0?1:g);if(!b){$(this).find(".pageviews").text(g)}else{var f=parseInt($(this).find(".pageviews").text());if(g>f){countUp(f,g,$(this).find(".pageviews").attr("id"))}}})}else{if($(".post").length>0){var e=window.location.pathname;var c=countPV(e,d);c=(c==0?1:c);if(!b){$("#pv").text(c)}else{var a=parseInt($("#pv").text());if(c>a){countUp(a,c,"pv")}}}}}$(function(){if($(".pageviews").length>0){var a=false;$.getJSON("/assets/data/pageviews.json",function(b){displayPageviews(b.rows,a);a=true});$.getJSON("/assets/data/proxy.json",function(b){$.ajax({url:b.proxyUrl,dataType:"jsonp",timeout:2000,success:function(c){displayPageviews(c.rows,a)},error:function(e,c,d){console.log("Failed to load pageviews from proxy server.");e.abort();return}})})}}); \ No newline at end of file +function countUp(c,a,b){if(c0){$(".post-preview").each(function(){var i=$(this).children("h1").children("a").attr("href");var h=countPV(i,d);h=(h==0?1:h);if(!b){$(this).find(".pageviews").text(h)}else{var g=parseInt($(this).find(".pageviews").text());if(h>g){countUp(g,h,$(this).find(".pageviews").attr("id"))}}})}else{if($(".post").length>0){var f=window.location.pathname;var c=countPV(f,d);c=(c==0?1:c);if(!b){$("#pv").text(c)}else{var a=parseInt($("#pv").text());if(c>a){countUp(a,c,"pv")}}}}}var getInitStatus=(function(){var a=false;return function(){if(a){return true}else{a=true;return false}}})();$(function(){if($(".pageviews").length>0){$.getJSON("/assets/data/pageviews.json",displayPageviews);$.getJSON("/assets/data/proxy.json",function(a){$.ajax({type:"GET",url:a.proxyUrl,dataType:"jsonp",jsonpCallback:"displayPageviews",error:function(b,d,c){console.log("Failed to load pageviews from proxy server: "+c)}})})}}); \ No newline at end of file