-
Notifications
You must be signed in to change notification settings - Fork 22
/
IngressMaxFields.user.js
320 lines (285 loc) · 12 KB
/
IngressMaxFields.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
// ==UserScript==
// @id iitc-plugin-ingressmaxfield@stenyg
// @name IITC plugin: Ingress Maxfields
// @category Information
// @version 0.2.0.0
// @namespace http://github.com/jonatkins/ingress-intel-total-conversion
// @updateURL http://github.com/itayo/IITC-Ingress-Maxfields-Exporter/raw/master/IngressMaxFields.user.js
// @downloadURL http://github.com/itayo/IITC-Ingress-Maxfields-Exporter/raw/master/IngressMaxFields.user.js
// @description Exports portals in the format for http://www.ingress-maxfield.com/ and allow direct transfer to site
// @include https://intel.ingress.com/*
// @match https://intel.ingress.com/*
// @grant none
// ==/UserScript==
/*global $:false */
/*global map:false */
/*global L:false */
function wrapper() {
// in case IITC is not available yet, define the base plugin object
if (typeof window.plugin !== "function") {
window.plugin = function() {};
}
// base context for plugin
window.plugin.ingressmaxfield = function() {};
var self = window.plugin.ingressmaxfield;
self.portalInScreen = function portalInScreen(p) {
return map.getBounds().contains(p.getLatLng());
};
// adapted from
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/is-point-in-poly [rev. #0]
self.portalInPolygon = function portalInPolygon(polygon, portal) {
var poly = polygon.getLatLngs();
var pt = portal.getLatLng();
var c = false;
for (var i = -1, l = poly.length, j = l - 1; ++i < l; j = i) {
((poly[i].lat <= pt.lat && pt.lat < poly[j].lat) || (poly[j].lat <= pt.lat && pt.lat < poly[i].lat)) && (pt.lng < (poly[j].lng - poly[i].lng) * (pt.lat - poly[i].lat) / (poly[j].lat - poly[i].lat) + poly[i].lng) && (c = !c);
}
return c;
};
// return if the portal is within the drawtool objects.
// Polygon and circles are available, and circles are implemented
// as round polygons.
self.portalInForm = function(layer) {
if (layer instanceof L.Rectangle) {
return true;
}
if (layer instanceof L.Circle) {
return true;
}
return false;
};
self.portalInGeo = function(layer) {
if (layer instanceof L.GeodesicPolygon) {
return true;
}
if (layer instanceof L.GeodesicCircle) {
return true;
}
return false;
};
self.portalInDrawnItems = function(portal) {
var c = false;
window.plugin.drawTools.drawnItems.eachLayer(function(layer) {
if (!(self.portalInForm(layer) || self.portalInGeo(layer))) {
return false;
}
if (self.portalInPolygon(layer, portal)) {
c = true;
}
});
return c;
};
self.inBounds = function(portal) {
if (window.plugin.drawTools && window.plugin.drawTools.drawnItems.getLayers().length) {
return self.portalInDrawnItems(portal);
} else {
return self.portalInScreen(portal);
}
};
self.genStr = function genStr(title, lat, lng, portalGuid) {
var href = "https://www.ingress.com/intel?ll=" + lat + "," + lng + "&z=17&pll=" + lat + "," + lng;
var str = "";
var keyCount = 0;
str = title;
str = str.replace(/\"/g, "\\\"");
str = str.replace(";", " ");
str = str + ";" + href;
if (typeof window.portals[portalGuid] !== "undefined") {
if (window.plugin.keys) {
keyCount = window.plugin.keys.keys[portalGuid] || 0;
} else if (window.plugin.LiveInventory && typeof window.plugin.LiveInventory.keyMap[portalGuid] !== "undefined") {
keyCount = window.plugin.LiveInventory.keyMap[portalGuid].count;
}
str = str + ";" + keyCount;
}
return str;
};
self.genStrFromPortal = function genStrFromPortal(portal, portalGuid) {
var lat = portal._latlng.lat,
lng = portal._latlng.lng,
title = portal.options.data.title || portal.label || "untitled portal";
return self.genStr(title, lat, lng, portalGuid);
};
self.managePortals = function managePortals(obj, portal, x) {
if (self.inBounds(portal)) {
var str = self.genStrFromPortal(portal, x);
obj.list.push(str);
obj.count += 1;
}
return obj;
};
self.checkPortals = function checkPortals(portals) {
var obj = {
list: [],
count: 0
};
for (var x in portals) {
if (typeof window.portals[x] !== "undefined") {
self.managePortals(obj, window.portals[x], x);
}
}
return obj;
};
// Return markup for the bookmarks to show in the IMF dialog
self.renderPortalBookmarkFolders = function renderPortalBookmarkFolder(folders) {
var data = "<p>With Portal Bookmarks, you can populate the list with a folder you have created instead:</p>";
for (var folder in folders) {
if (folders.hasOwnProperty(folder)) {
data += `<div
style="text-align: center; border: yellow 1px solid; margin-bottom: 0.3em; cursor: pointer;"
onClick='window.plugin.ingressmaxfield.appendBookmarkFolder("${folder}");'>
${folders[folder]}
</div>`;
}
}
return data;
};
// Generate string for given bookmarked portal
self.genStrFromBookmarkPortal = function genStrFromBookmarkPortal(portalId, folder) {
var portalsList = JSON.parse(localStorage["plugin-bookmarks"]);
var portal = portalsList.portals[folder]["bkmrk"][portalId];
var latlng = portal.latlng.split(",");
return self.genStr(portal.label, latlng[0], latlng[1], portal.guid);
};
// Generate string for all the portals
self.genStrFromBookmarkFolder = function genStrFromBookmarkFolder(folder) {
var data = "",
portalsList = JSON.parse(localStorage["plugin-bookmarks"]);
for (var portal in portalsList.portals[folder]["bkmrk"]) {
if (portalsList.portals[folder]["bkmrk"].hasOwnProperty(portal)) {
data += self.genStrFromBookmarkPortal(portal, folder) + "\n";
}
}
return data;
};
// Write a list of all portal bookmarks to the text area
self.appendBookmarkFolder = function appendBookmarkFolder(folder) {
var $form = $("form[name='maxfield'] textarea");
$form.val(self.genStrFromBookmarkFolder(folder));
};
self.showDialog = function showDialog(o, b) {
var data = `
<form name='maxfield' action='https://www.ingress-maxfield.com/submit.php' enctype='multipart/form-data' method='post' target='_blank'>
<div class="row">
<div id='form_area' class="column" style="float:left;width:80%;box-sizing: border-box;padding-right: 10px;">
<textarea class='form_area'
name='portal_list_area'
rows='30'
placeholder='Copy and paste portal list here OR upload portal list file below. Proper formatting guidelines can be found in the instructions. Anything after a # is considered a comment and will be ignored - be sure to remove any # or ; that appear in a portal name. Each portal should start on a new line.'
style="width: 100%; white-space: nowrap;">${o.join("\n")}</textarea>
</div>
<div class="column" style="float:left;width:20%;">
${self.renderPortalBookmarkFolders(b)}
</div>
</div>
<div id='form_part2'>
<div id='file_upload'>
<br/>
<label class='upload_button' hidden>
<span>
<input id='hidden' type='file' name='portal_list'>
</span>
</label>
<input type='hidden' id='path' placeholder='No file selected' disabled>
</div>
<div id='num_agents'>
<table width='100%'>
<tr>
<td width='50%'>
Number of agents:
</td>
<td width='50%'>
<input type='number' class='num_agents' name='num_agents' value='1' min='1' required>
</td>
</tr>
<tr>
<td>
Use Google Maps?
</td>
<td>
<input type='checkbox' name='useGoogle' value='YES' checked>
</td>
</tr>
<tr>
<td>
Color scheme
</td>
<td>
<input type='radio' name='color' value='ENL' checked>ENL</input>
<input type='radio' name='color' value='RES'>RES</input>
</td>
</tr>
</table>
</div>
<div id='submit'>
<table>
<tr>
<td>
Email:
</td>
<td>
<input type='email' name='email' placeholder='(optional)'>
</td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' class='submit' name='submit' value='Submit!'>
</td>
</tr>
</table>
</div>
</div>
</form>
`;
var dia = window.dialog({
title: "www.ingress-maxfield.com: Field your future",
html: data
}).parent();
$(".ui-dialog-buttonpane", dia).remove();
dia.css("width", "600px").css("top", ($(window).height() - dia.height()) / 2).css("left", ($(window).width() - dia.width()) / 2);
return dia;
};
self.gen = function gen() {
var o = self.checkPortals(window.portals);
var bookmarks = self.checkBookmarks();
var dialog = self.showDialog(o.list, bookmarks);
return dialog;
};
// Return a list of portal bookmark folders
self.checkBookmarks = function checkBookmarks() {
if (!window.plugin.bookmarks) {
return null;
}
var portalsList = JSON.parse(localStorage["plugin-bookmarks"]);
var res = {};
for (var folder in portalsList.portals) {
if (portalsList.portals.hasOwnProperty(folder)) {
res[folder] = portalsList.portals[folder].label;
}
}
return res;
};
// setup function called by IITC
self.setup = function init() {
// add controls to toolbox
var link = $("<a onclick=\"window.plugin.ingressmaxfield.gen();\" title=\"Generate a CSV list of portals and locations for use with www.ingress-maxfield.com.\">IMF Export</a>");
$("#toolbox").append(link);
// delete self to ensure init can't be run again
delete self.init;
};
// IITC plugin setup
if (window.iitcLoaded && typeof self.setup === "function") {
self.setup();
} else if (window.bootPlugins) {
window.bootPlugins.push(self.setup);
} else {
window.bootPlugins = [self.setup];
}
}
// inject plugin into page
var script = document.createElement("script");
script.appendChild(document.createTextNode("(" + wrapper + ")();"));
(document.body || document.head || document.documentElement)
.appendChild(script);