forked from sp00x/iitc-offle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iitc-offle.user.js
588 lines (501 loc) · 21.2 KB
/
iitc-offle.user.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// ==UserScript==
// @id iitc-plugin-offle
// @name IITC plugin: offle
// @category Misc
// @version 0.9
// @namespace https://github.com/vrabcak/iitc-offle
// @description Offle
// @match https://intel.ingress.com/*
// @grant none
// ==/UserScript==
function wrapper(plugin_info) {
// ensure plugin framework is there, even if iitc is not yet loaded
if (typeof window.plugin !== 'function') {
window.plugin = function () {};
}
// PLUGIN START ////////////////////////////////////////////////////////
// use own namespace for plugin
window.plugin.offle = function () {};
var offle = window.plugin.offle;
offle.portalDb = {};
offle.lastAddedDb = {};
offle.symbol = '•';
offle.symbolWithMission = '◉';
offle.maxVisibleCount = 2000;
// Use portal add event to save it to db
offle.portalAdded = function (data) {
offle.addPortal(
data.portal.options.guid,
data.portal.options.data.title,
data.portal.getLatLng(),
data.portal.options.data.mission
);
};
// Always update portal data if displayed in sidebar (to handle e.g. moved portals)
offle.portalDetailsUpdated = function (data) {
var guid = data.portal.options.guid,
name = data.portal.options.data.title;
if (name) { //update data only with portals with full details
offle.portalDb[guid] = data.portal.getLatLng();
offle.portalDb[guid].name = data.portal.options.data.title;
offle.portalDb[guid].mission = data.portal.options.data.mission;
offle.renderVisiblePortals();
localforage.setItem('portalDb', offle.portalDb);
}
};
offle.addPortal = function (guid, name, latLng, mission) {
var notInDb = guid && !(guid in offle.portalDb);
var old = offle.portalDb[guid];
//console.log("AddPortal: %o %o %o %o %o", guid, name, latLng, mission, old, notInDb);
var newName = name && old && old.name != name;
var newPos = latLng && old && (old.lat != latLng.lat || old.lng != latLng.lng);
var newMission = mission != null && old && old.mission != mission;
var newData = newName || newPos || newMission;
//console.log("AddPortal ", guid," ",name, "::", notInDb, " ", newName, " ", newPos, " ", newMission);
var now = Date.now();
if (notInDb || newData) {
var hadName = old && old.name != null && old.name != old.guid;
//add to last added list only new portals or update already displayed guid with new data
if (notInDb || (((newName && hadName) || newPos) /*&& (guid in offle.lastAddedDb)*/)) {
var la = {
name: notInDb || newName ? name || guid : old.name,
latLng: notInDb || newPos ? latLng : { lat: old.lat, lng: old.lng },
unique: false,
isNew: notInDb
};
if (newName) la.oldName = old.name;
if (newPos) la.oldPos = { lat: old.lat, lng: old.lng };
if (!(window.plugin.uniques && (guid in window.plugin.uniques.uniques))) {
la.unique = true;
}
offle.lastAddedDb[guid] = la;
}
let portal = offle.portalDb[guid] || {};
if (notInDb || newName) portal.name = name || guid;
if (notInDb || newPos) { portal.lat = latLng.lat; portal.lng = latLng.lng; }
if (notInDb || mission) portal.mission = mission;
if (notInDb || !portal.createTs) portal.createTs = now;
portal.modifyTs = now;
if (notInDb) offle.portalDb[guid] = portal;
offle.dirtyDb = true; //mark Db dirty to by stored on mapDataRefreshEnd
offle.renderPortal(guid);
offle.updatePortalCounter();
offle.updateLACounter();
offle.updateLAList();
}
// set last seen timestamp so we can track potentially removed portals later
offle.portalDb[guid].seenTs = now;
if (!offle.portalDb[guid].createTs)
offle.portalDb[guid].createTs = offle.portalDb[guid].modifyTs = now;
offle.dirtyDb = true; // yes(!) - we need to save every scan
};
offle.currentPortalMarkers = {};
offle.renderPortal = function (guid) {
var oldMarker = offle.currentPortalMarkers[guid];
if (oldMarker) // avoid moved portals staying in both locations on the map until refresh
try { oldMarker.remove(); } catch (e) {};
var portalMarker, uniqueInfo,
iconCSSClass = 'offle-marker';
if (window.plugin.uniques) {
uniqueInfo = window.plugin.uniques.uniques[guid];
}
if (uniqueInfo) {
if (uniqueInfo.visited) {
iconCSSClass += ' offle-marker-visited-color';
}
if (uniqueInfo.captured) {
iconCSSClass += ' offle-marker-captured-color';
}
}
portalMarker = L.marker(offle.portalDb[guid], {
icon: L.divIcon({
className: iconCSSClass,
iconAnchor: [15, 23],
iconSize: [30, 30],
html: offle.portalDb[guid].mission ? offle.symbolWithMission : offle.symbol
}),
name: offle.portalDb[guid].name,
title: offle.portalDb[guid].name || ''
});
portalMarker.on('click', function () {
window.renderPortalDetails(guid);
});
portalMarker.addTo(offle.portalLayerGroup);
offle.currentPortalMarkers[guid] = portalMarker;
if (window.plugin.keys) {
var keyCount = window.plugin.keys.keys[guid];
if (keyCount > 0) {
var keyMarker = L.marker(offle.portalDb[guid], {
icon: L.divIcon({
className: 'offle-key',
iconAnchor: [6, 7],
iconSize: [12, 10],
html: keyCount
}),
guid: guid
});
keyMarker.addTo(offle.keyLayerGroup);
}
}
};
offle.clearLayer = function () {
offle.portalLayerGroup.clearLayers();
offle.keyLayerGroup.clearLayers();
offle.currentPortalMarkers = {};
};
offle.saveData = function (force) {
if (offle.dirtyDb || force === true) {
//console.log("Storing new portals to localStorage");
localforage.setItem('portalDb', offle.portalDb);
}
offle.dirtyDb = false;
};
offle.mapDataRefreshStart = function () {
//console.log("offle: starting map refresh..");
};
offle.mapDataRefreshEnd = function () {
//console.log("offle: map refresh ended!");
offle.saveData();
};
offle.setupLayer = function () {
offle.portalLayerGroup = new L.LayerGroup();
window.addLayerGroup('offlePortals', offle.portalLayerGroup, false);
offle.keyLayerGroup = new L.LayerGroup();
window.addLayerGroup('offleKeys', offle.keyLayerGroup, false);
};
offle.setupCSS = function () {
$('<style>')
.prop('type', 'text/css')
.html('.offle-marker {' +
'font-size: 30px;' +
'color: #FF6200;' +
'font-family: monospace;' +
'text-align: center;' +
//'pointer-events: none;' +
'}' +
'.offle-marker-visited-color {' +
'color: #FFCE00;' +
'}' +
'.offle-marker-captured-color {' +
'color: #00BB00;' +
'}' +
'.offle-portal-counter {' +
'display: none; position: absolute; top:0; left: 40vh;' +
'background-color: orange; z-index: 4002; cursor:pointer;}' +
'.pokus {' +
'border-style: solid;' +
'border-width: 3px' +
'}' +
'.offle-key {' +
'font-size: 10px;' +
'color: #FFFFBB;' +
'font-family: monospace;' +
'text-align: center;' +
'text-shadow: 0 0 0.5em black, 0 0 0.5em black, 0 0 0.5em black;' +
'pointer-events: none;' +
'-webkit-text-size-adjust:none;' +
'}'
)
.appendTo('head');
};
offle.updatePortalCounter = function () {
$('#offle-portal-counter').html(Object.keys(offle.portalDb).length);
};
offle.getVisiblePortals = function () {
var keys = Object.keys(offle.portalDb);
var actualBounds = map.getBounds();
var keysInView = keys.filter(function (key) {
var ll,
portal = offle.portalDb[key];
if (portal.lat && portal.lng) {
ll = L.latLng(portal.lat, portal.lng);
return actualBounds.contains(ll);
}
return false;
});
$('#visible-portals-counter').html(keysInView.length);
return keysInView;
};
offle.renderVisiblePortals = function () {
var visiblePortalsKeys = offle.getVisiblePortals();
if (visiblePortalsKeys.length < offle.maxVisibleCount) {
visiblePortalsKeys.forEach(function (key) {
offle.renderPortal(key);
});
}
};
offle.onMapMove = function () {
offle.renderVisiblePortals();
};
offle.clearDb = function () {
if (confirm('Are you sure to permanently delete ALL the stored portals?')) {
localforage.removeItem('portalDb');
offle.portalDb = {};
offle.clearLayer();
offle.updatePortalCounter();
}
};
offle.changeSymbol = function (event) {
offle.symbol = event.target.value;
offle.clearLayer();
offle.renderVisiblePortals();
};
offle.changeMaxVisibleCount = function (event) {
offle.maxVisibleCount = event.target.value;
offle.clearLayer();
offle.renderVisiblePortals();
};
offle.setupHtml = function () {
$('#toolbox').append('<a id="offle-show-info" onclick="window.plugin.offle.showDialog();">Offle</a> ');
offle.lastAddedDialogHtml = '' +
'<div id="offle-last-added-list">' +
'placeholder <br/>' +
'placeholder' +
'</div>' +
'<button onclick="window.plugin.offle.clearLADb()">Clear</div>';
$('body').append('<div class="offle-portal-counter" onclick="window.plugin.offle.showLAWindow();">0</div>');
};
offle.showDialog = function () {
offle.dialogHtml = '<div id="offle-info">' +
'<div>' +
'<div> Offline portals count:' +
'<span id="offle-portal-counter">' +
Object.keys(offle.portalDb).length +
'</span></div>' +
'<div> Visible portals:' +
'<span id="visible-portals-counter">x</span></div>' +
'<div> Unique portals visited: ' +
(window.plugin.uniques ? Object.keys(window.plugin.uniques.uniques).length : 'uniques plugin missing') +
'</div>' +
'<div> Portal marker symbol: <input type="text" value="' +
offle.symbol +
'" size="1" onchange="window.plugin.offle.changeSymbol(event)"> </div>' +
'<div> Maximum visible portals: <input type="number" value="' +
offle.maxVisibleCount +
'" size="5" onchange="window.plugin.offle.changeMaxVisibleCount(event)"> </div>' +
'<div style="border-bottom: 60px;">' +
'<button onclick="window.plugin.offle.showLAWindow();return false;">New portals</button>' +
'<button onClick="window.plugin.offle.export();return false;">Export JSON</button>' +
'<button onClick="window.plugin.offle.exportKML();return false;">Export KML</button>' +
'<button onClick="window.plugin.offle.import();return false;">Import JSON</button>' +
'<input type="file" id="fileInput" style="visibility: hidden">' +
'</div><br/>' +
'<a href="" id="dataDownloadLink" download="" style="display: none" onclick="this.style.display=\'none\'">' +
'click to download </a>' +
'<br/><br/>' +
'<button onclick="window.plugin.offle.clearDb();return false;" style="font-size: 5px;">' +
'Clear all offline portals</button>' +
'</div>';
window.dialog({
html: offle.dialogHtml,
title: 'Offle',
modal: false,
id: 'offle-info'
});
offle.updatePortalCounter();
offle.getVisiblePortals();
};
offle.zoomToPortalAndShow = function (guid) {
var lat = offle.portalDb[guid].lat,
lng = offle.portalDb[guid].lng,
ll = [lat, lng];
map.setView(ll, 15);
window.renderPortalDetails(guid);
};
offle.showLAWindow = function () {
window.dialog({
html: offle.lastAddedDialogHtml,
title: 'Portals added since last session:',
modal: false,
id: 'offle-LA',
height: $(window).height() * 0.45
});
offle.updateLAList();
};
offle.updateLAList = function () { /// update list of last added portals
var guids = Object.keys(offle.lastAddedDb);
var portalListHtml = guids.map(function (guid) {
var portal = offle.lastAddedDb[guid];
var html = '<a onclick="window.plugin.offle.zoomToPortalAndShow(\'' + guid + '\');return false"' +
(portal.unique ? 'style="color: #FF6200;"' : '')
+ 'href="/intel?pll=' + portal.latLng.lat + ',' + portal.latLng.lng + '">'
+ portal.name
+ '</a>';
if (!portal.isNew) {
if (portal.oldName) html += " (renamed: " + portal.oldName + ")";
if (portal.oldPos) html += " (moved: " + (portal.latLng.lat - portal.oldPos.lat) + "," + (portal.latLng.lng - portal.oldPos.lng) + ")";
}
return html;
}).join('<br />');
$('#offle-last-added-list').html(portalListHtml);
};
offle.updateLACounter = function () {
var count = Object.keys(offle.lastAddedDb).length;
if (count > 0) {
$('.offle-portal-counter').css('display', 'block').html('' + count);
}
};
offle.clearLADb = function () {
offle.lastAddedDb = {};
offle.updateLAList();
$('.offle-portal-counter').css('display', 'none');
};
offle.export = function () {
var jsonDb = JSON.stringify(offle.portalDb);
var blobDb = new Blob([jsonDb], {
type: 'application/json'
});
var dataDownlodaLinkEl = document.getElementById('dataDownloadLink');
dataDownlodaLinkEl.href = URL.createObjectURL(blobDb);
dataDownlodaLinkEl.download = 'offle-export.json';
dataDownlodaLinkEl.style.display = 'block';
};
offle.import = function () {
var fileInputEl = document.getElementById('fileInput');
function is_guid(guid) {
var re = /(?:[a-f]|\d){32}\.\d{2}/;
return guid.match(re) !== null;
}
function parseJSONAndImport(string_db) {
var portal_db;
if (string_db !== null) {
try {
portal_db = JSON.parse(string_db);
} catch (err) {
window.alert('Not valid portal database: \n' + err);
return;
}
var guids = Object.keys(portal_db);
var len = guids.length;
var old_len = Object.keys(offle.portalDb).length;
for (var i = 0; i != len; ++i) {
var guid = guids[i];
if (!is_guid(guid)) {
continue;
}
var obj = portal_db[guid];
if (!obj.hasOwnProperty('lat') || !obj.hasOwnProperty('lng')) {
continue;
}
offle.portalDb[guid] = obj;
}
var new_len = Object.keys(offle.portalDb).length;
offle.saveData(true);
window.alert('Portals processed: ' + len + ', portals added:' + (new_len - old_len) + '.');
offle.clearLayer(); // in case something was moved (avoid duplicates)
offle.renderVisiblePortals();
}
}
function handleFile() {
var reader = new FileReader();
if (this.files.length === 0) {
return;
}
console.log(this.files[0].name, this.files[0].type);
reader.onload = function (e) {
parseJSONAndImport(e.target.result);
};
reader.readAsText(this.files[0]);
fileInputEl.removeEventListener('change', handleFile, false);
}
fileInputEl.click();
fileInputEl.addEventListener('change', handleFile, false);
};
offle.exportKML = function () {
var kmlBlob;
var dataDownlodaLinkEl = document.getElementById('dataDownloadLink');
var kml = '<?xml version="1.0" encoding="UTF-8"?>\n' +
'<kml xmlns="http://www.opengis.net/kml/2.2">\n' +
'<Document>\n';
Object.keys(offle.portalDb).forEach(
function (guid) {
var name, escapedName;
var obj = offle.portalDb[guid];
if (!obj.hasOwnProperty('lat') || !obj.hasOwnProperty('lng')) {
return;
}
if (obj.hasOwnProperty('name') && obj.name) {
name = obj.name;
} else {
name = guid;
}
escapedName = name.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
kml += '<Placemark>\n';
kml += '<name>' + escapedName + '</name>\n';
kml += '<Point><coordinates>' + obj.lng + ',' + obj.lat + ',0</coordinates></Point>\n';
kml += '</Placemark>\n';
}
);
kml += '</Document>\n</kml>';
kmlBlob = new Blob([kml], {
type: 'application/vnd.google-earth.kml+xml'
});
dataDownlodaLinkEl.href = URL.createObjectURL(kmlBlob);
dataDownlodaLinkEl.download = 'ingress-portals.kml';
dataDownlodaLinkEl.style.display = 'block';
};
var setup = function () {
var API = 'https://unpkg.com/[email protected]/dist/localforage.js';
$.getScript(API).done(function () {
offle.setupLayer();
offle.setupCSS();
offle.setupHtml();
//convert old localStorage database to new localforage
var db = JSON.parse(localStorage.getItem('portalDb'));
if (db) {
localforage.setItem('portalDb', db)
.then(function () {
console.log('Offle: Db migrated');
localStorage.removeItem('portalDb');
});
}
//load portals from local storage
localforage.getItem('portalDb').then(
function (value) {
if (value) {
offle.portalDb = value;
if (Object.keys(offle.portalDb).length > 0) {
offle.renderVisiblePortals();
} else {
offle.portalDb = {};
}
}
}
);
map.on('movestart', function () {
offle.clearLayer();
});
map.on('moveend', offle.onMapMove);
window.addHook('portalAdded', offle.portalAdded);
window.addHook('mapDataRefreshStart', offle.mapDataRefreshStart);
window.addHook('mapDataRefreshEnd', offle.mapDataRefreshEnd);
window.addHook('portalDetailsUpdated', offle.portalDetailsUpdated);
});
};
// PLUGIN END //////////////////////////////////////////////////////////
setup.info = plugin_info; //add the script info data to the function as a property
if (!window.bootPlugins) {
window.bootPlugins = [];
}
window.bootPlugins.push(setup);
// if IITC has already booted, immediately run the 'setup' function
if (window.iitcLoaded && typeof setup === 'function') {
setup();
}
} // wrapper end
// inject code into site context
var script = document.createElement('script');
var info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) {
info.script = {
version: GM_info.script.version,
name: GM_info.script.name,
description: GM_info.script.description
};
}
script.appendChild(document.createTextNode('(' + wrapper + ')(' + JSON.stringify(info) + ');'));
(document.body || document.head || document.documentElement).appendChild(script);