Advertisement
ryan217

Pagespeed Original

Apr 24th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //API Key and URL
  2. var API_KEY = 'randomapikey';
  3. var YN_URL = 'http://www.google.com';
  4.  
  5. //Fetch PageSpeed Results
  6. var API_URL = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?';
  7. var CHART_API_URL = 'http://chart.apis.google.com/chart?';
  8.  
  9. // Object that will hold the callbacks that process results from the
  10. // PageSpeed Insights API.
  11. var callbacks = {};
  12.  
  13. // Invokes the PageSpeed Insights API. The response will contain
  14. // JavaScript that invokes our callback with the PageSpeed results.
  15. function runPagespeed() {
  16.   var s = document.createElement('script');
  17.   s.type = 'text/javascript';
  18.   s.async = true;
  19.   var query = [
  20.     'url=' + YN_URL,
  21.     'callback=runPagespeedCallbacks',
  22.     'key=' + API_KEY
  23.   ].join('&');
  24.   s.src = API_URL + query;
  25.   document.head.insertBefore(s, null);
  26. }
  27.  
  28. // Our JSONP callback. Checks for errors, then invokes our callback handlers.
  29. function runPagespeedCallbacks(result) {
  30.   if(result.error) {
  31.     var errors =result.error.errors;
  32.     for (var i = 0, len = errors.length; i < len; i++) {
  33.       if(errors[i].reason == 'badRequest' && API_KEY == '{yourAPIKey}') {
  34.         alert('Please specify your Google API key in the API_KEY variable.');
  35.       } else {
  36.         alert(errors[i].message);
  37.       }
  38.     }
  39.     return;
  40.   }
  41.   // Dispatch to each function on the callbacks object.
  42.   for (var fn in callbacks) {
  43.     var f = callbacks[fn];
  44.     if (typeof f == 'function') {
  45.       callbacks[fn](result);
  46.     }
  47.   }
  48. }
  49.  
  50. // Invoke the callback that fetches results. Async here so we're sure
  51. // to discover any callbacks registered below, but this can be
  52. // synchronous in your code.
  53. setTimeout(runPagespeed, 0);
  54.  
  55.  
  56.  
  57. //Google-O-Meter
  58. callbacks.displayPageSpeedScore = function(result) {
  59.   var score = result.score;
  60.   // Construct the query to send to the Google Chart Tools.
  61.   var query = [
  62.     'chtt=Page+Speed+score:+' + score,
  63.     'chs=250x100',
  64.     'cht=gom',
  65.     'chd=t:' + score,
  66.     'chxt=x,y',
  67.     'chxl=0:|' + score,
  68.   ].join('&');
  69.   var i = document.createElement('img');
  70.   i.src = CHART_API_URL + query;
  71.   i.setAttribute('id', 'ometer');
  72.   document.getElementById('left').appendChild(i);
  73. };
  74.  
  75. var RESOURCE_TYPE_INFO = [
  76.   {label: 'JavaScript', field: 'javascriptResponseBytes', color: 'e2192c'},
  77.   {label: 'Images', field: 'imageResponseBytes', color: 'f3ed4a'},
  78.   {label: 'CSS', field: 'cssResponseBytes', color: 'ff7008'},
  79.   {label: 'HTML', field: 'htmlResponseBytes', color: '43c121'},
  80.   {label: 'Flash', field: 'flashResponseBytes', color: 'f8ce44'},
  81.   {label: 'Text', field: 'textResponseBytes', color: 'ad6bc5'},
  82.   {label: 'Other', field: 'otherResponseBytes', color: '1051e8'},
  83. ];
  84.  
  85. callbacks.displayResourceSizeBreakdown = function(result) {
  86.   var stats = result.pageStats;
  87.   var labels = [];
  88.   var data = [];
  89.   var colors = [];
  90.   var totalBytes = 0;
  91.   var largestSingleCategory = 0;
  92.   for (var i = 0, len = RESOURCE_TYPE_INFO.length; i < len; ++i) {
  93.     var label = RESOURCE_TYPE_INFO[i].label;
  94.     var field = RESOURCE_TYPE_INFO[i].field;
  95.     var color = RESOURCE_TYPE_INFO[i].color;
  96.     if (field in stats) {
  97.       var val = Number(stats[field]);
  98.       totalBytes += val;
  99.       if (val > largestSingleCategory) largestSingleCategory = val;
  100.       labels.push(label);
  101.       data.push(val);
  102.       colors.push(color);
  103.     }
  104.   }
  105.   // Construct the query to send to the Google Chart Tools.
  106.   var query = [
  107.     'chs=300x140',
  108.     'cht=p3', //Chart type
  109.     'chts=' + ['000000', 16].join(','),
  110.     'chco=' + colors.join('|'), //Colors separated by |
  111.     'chd=t:' + data.join(','), //Pie data split by ,
  112.     'chdl=' + labels.join('|'), //Name of labels/colors split by |
  113.     'chdls=000000,14', //Font size
  114.     'chp=1.6',
  115.     'chds=0,' + largestSingleCategory,
  116.   ].join('&');
  117.   var i = document.createElement('img');
  118.   i.src = CHART_API_URL + query;
  119.   i.setAttribute('id', 'piechart');
  120.   document.getElementById('right').appendChild(i);
  121. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement