Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mrchrisadams/388631 to your computer and use it in GitHub Desktop.
Save mrchrisadams/388631 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Google Maps Carbon
// @namespace http://www.amee.com
// @include http://maps.google.*/
// ==/UserScript==
// Created by James Smith, AMEE - [email protected]
// Public domain, do what you like :)
// Once you've installed the greasemonkey script (click "raw" on the top
// right of the gist to do so), you will need to edit it to set the following
// variables correctly:
server = "stage.amee.com"
username = "your_amee_username_here"
password = "your_amee_password_here"
profile_uid = "your_amee_profile_uid_here"
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
}
function directionsLoaded(event) {
node = event.relatedNode;
if ((node.className == "altroute_info") && node.textContent.indexOf('(') == -1) {
addCO2Value(node);
}
if (node.id == "panel4") {
infonode = document.getElementById("dditd");
if (infonode.textContent.indexOf('(') == -1) {
addCO2Value(infonode);
}
}
}
function addCO2Value(node) {
// Get distance out of content
str = node.textContent;
str = str.replace("\xA0"," ");
var distance = str.split(" ");
// Get co2 data from AMEE
gmAjax({
method: 'GET',
url: 'http://'+server+'/data/transport/car/generic/drill?size=average&fuel=average',
headers: {
'Accept': 'application/json',
'Authorization':'Basic '+Base64.encode(username+":"+password)
},
onload: function(responseDetails) {
response = JSON.parse(responseDetails.responseText);
var itemname = (new Date).getTime();
uid = response['choices']['choices'][0]['value']
gmAjax({
method: 'POST',
url: ('http://'+server+'/profiles/'+profile_uid+'/transport/car/generic') ,
headers: {
'Accept': 'application/json',
'Authorization':'Basic '+Base64.encode(username+":"+password),
'Content-type':'application/x-www-form-urlencoded'
},
data: 'dataItemUid='+uid+'&distance='+distance[0].replace(',','')+'&representation=full&distanceUnit='+distance[1]+'&name='+itemname,
onload: function(responseDetails) {
response = JSON.parse(responseDetails.responseText);
amount = parseFloat(response['profileItems'][0]['amount']['value'])
node.textContent += " ("+amount.toString()+" kg CO2e)";
}
});
}
});
}
element = document.getElementById("opanel4");
element.addEventListener("DOMNodeInserted", directionsLoaded, true);
var ajaxQueue = [];
var processAjaxQueue = function(){
if (ajaxQueue.length > 0) {
for (ajax in ajaxQueue) {
var obj = ajaxQueue[ajax];
GM_xmlhttpRequest(obj);
}
ajaxQueue = [];
}
}
setInterval(function(){
processAjaxQueue();
}, 100);
function gmAjax(obj){
ajaxQueue.push(obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment