Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var runPagespeedCallbacks = (function() {
- //API Key and URL
- var API_KEY = 'randomapikey';
- var YN_URL = 'http://www.google.com';
- //Fetch PageSpeed Results
- var API_URL = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed?';
- var CHART_API_URL = 'http://chart.apis.google.com/chart?';
- // Object that will hold the callbacks that process results from the
- // PageSpeed Insights API.
- var callbacks = {};
- // Invokes the PageSpeed Insights API. The response will contain
- // JavaScript that invokes our callback with the PageSpeed results.
- function runPagespeed() {
- var s = document.createElement('script');
- s.type = 'text/javascript';
- s.async = true;
- var query = [
- 'url=' + YN_URL,
- 'callback=runPagespeedCallbacks',
- 'key=' + API_KEY
- ].join('&');
- s.src = API_URL + query;
- document.head.insertBefore(s, null);
- }
- // Invoke the callback that fetches results. Async here so we're sure
- // to discover any callbacks registered below, but this can be
- // synchronous in your code.
- setTimeout(runPagespeed, 0);
- //Google-O-Meter
- callbacks.displayPageSpeedScore = function(result) {
- var score = result.score;
- // Construct the query to send to the Google Chart Tools.
- var query = [
- 'chtt=Page+Speed+score:+' + score,
- 'chs=250x100',
- 'cht=gom',
- 'chd=t:' + score,
- 'chxt=x,y',
- 'chxl=0:|' + score,
- ].join('&');
- var i = document.createElement('img');
- i.src = CHART_API_URL + query;
- i.setAttribute('id', 'ometer');
- document.getElementById('left').appendChild(i);
- };
- var RESOURCE_TYPE_INFO = [
- {label: 'JavaScript', field: 'javascriptResponseBytes', color: 'e2192c'},
- {label: 'Images', field: 'imageResponseBytes', color: 'f3ed4a'},
- {label: 'CSS', field: 'cssResponseBytes', color: 'ff7008'},
- {label: 'HTML', field: 'htmlResponseBytes', color: '43c121'},
- {label: 'Flash', field: 'flashResponseBytes', color: 'f8ce44'},
- {label: 'Text', field: 'textResponseBytes', color: 'ad6bc5'},
- {label: 'Other', field: 'otherResponseBytes', color: '1051e8'},
- ];
- callbacks.displayResourceSizeBreakdown = function(result) {
- var stats = result.pageStats;
- var labels = [];
- var data = [];
- var colors = [];
- var totalBytes = 0;
- var largestSingleCategory = 0;
- for (var i = 0, len = RESOURCE_TYPE_INFO.length; i < len; ++i) {
- var label = RESOURCE_TYPE_INFO[i].label;
- var field = RESOURCE_TYPE_INFO[i].field;
- var color = RESOURCE_TYPE_INFO[i].color;
- if (field in stats) {
- var val = Number(stats[field]);
- totalBytes += val;
- if (val > largestSingleCategory) largestSingleCategory = val;
- labels.push(label);
- data.push(val);
- colors.push(color);
- }
- }
- // Construct the query to send to the Google Chart Tools.
- var query = [
- 'chs=300x140',
- 'cht=p3', //Chart type
- 'chts=' + ['000000', 16].join(','),
- 'chco=' + colors.join('|'), //Colors separated by |
- 'chd=t:' + data.join(','), //Pie data split by ,
- 'chdl=' + labels.join('|'), //Name of labels/colors split by |
- 'chdls=000000,14', //Font size
- 'chp=1.6',
- 'chds=0,' + largestSingleCategory,
- ].join('&');
- var i = document.createElement('img');
- i.src = CHART_API_URL + query;
- i.setAttribute('id', 'piechart');
- document.getElementById('right').appendChild(i);
- };
- // Our JSONP callback. Checks for errors, then invokes our callback handlers.
- return function runPagespeedCallbacks(result) {
- if(result.error) {
- var errors =result.error.errors;
- for (var i = 0, len = errors.length; i < len; i++) {
- if(errors[i].reason == 'badRequest' && API_KEY == '{yourAPIKey}') {
- alert('Please specify your Google API key in the API_KEY variable.');
- } else {
- alert(errors[i].message);
- }
- }
- return;
- }
- // Dispatch to each function on the callbacks object.
- for (var fn in callbacks) {
- var f = callbacks[fn];
- if (typeof f == 'function') {
- callbacks[fn](result);
- }
- }
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement