-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunready.js
103 lines (80 loc) · 3.07 KB
/
unready.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// ==UserScript==
// @name Waze Map Events Unready - Address
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description add location to events, easy to look for the ones you can help
// @author Delfim Machado - [email protected]
// @match https://*.waze.com/events/unready
// @grant none
// ==/UserScript==
'use strict';
// Google Maps API key
// get yours here: https://developers.google.com/maps/documentation/javascript/get-api-key
var key = null;
// add country to event
var setData = function(i, r) {
var e = document.getElementsByClassName("mte-unready")[i];
if (/\[/.test(e.innerText) === false) {
var a = e.getElementsByClassName("mte-unready__address")[0];
a.innerText = a.innerText + " [" + r + "] ";
a.style.color = "red";
}
};
var getCache = function(k) {
var s = localStorage;
return s.getItem("UNREADY_" + k);
};
var setCache = function(k, v) {
var s = localStorage;
return s.setItem("UNREADY_" + k, v);
};
// get country from Google API
var getAddress = function(lat, lon, i) {
var r = getCache(lat + "_" + lon);
if (r) {
//console.log("FROM CACHE");
setData(i, r);
} else {
var client = new XMLHttpRequest();
client.open('GET', 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + lat + ',' + lon + '&key=' + key, true);
client.onreadystatechange = function() {
if (client.readyState === 4 && client.status >= 200 && client.status < 300) {
var jsonData = JSON.parse(client.responseText);
var r = jsonData.results[0].formatted_address;
setCache(lat + "_" + lon, r);
setData(i, r);
}
};
client.send(null);
}
};
// loop thgough all events
var loopUnready = function() {
var events = document.getElementsByClassName("mte-unready");
var l = events.length;
for (var i = 0; i < l; i++) {
var e = events[i];
var lat = e.href.match(/lat=([\-\d\.]+)/)[1];
var lon = e.href.match(/lon=([\-\d\.]+)/)[1];
getAddress(lat, lon, i);
}
};
var init = function() {
key = getCache("GoogleMapsAPIKey");
var header = document.getElementsByClassName('events-header')[0];
header.innerHTML = header.innerHTML + " [<a id='undeady_load'>SHOW ADDRESSES</a>]";
header.innerHTML = header.innerHTML + " : Add your <a href='https://developers.google.com/maps/documentation/javascript/get-api-key'>Google Maps API</a> key here: <input style='width:350px' id='unready_key' value='" + key + "'></input> [<a id='unready_save'>SAVE</a>]";
document.getElementById("undeady_load").addEventListener('click', function() {
loopUnready();
}, false);
document.getElementById("unready_save").addEventListener('click', function() {
setCache('GoogleMapsAPIKey', document.getElementById("unready_key").value);
}, false);
};
init();
/*
Goodies:
for (var i = 0; i < localStorage.length; i++){
console.log(localStorage.key(i) + " _ " +localStorage.getItem(localStorage.key(i)));
}
*/