forked from EisFrei/IngressPortalHistoryFlags
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathportalHistoryFlags.user.js
245 lines (214 loc) · 8.49 KB
/
portalHistoryFlags.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
// ==UserScript==
// @id portalHistoryFlags
// @name IITC Plugin: Portal History Flags
// @category Layer
// @version 0.0.8
// @namespace https://github.com/brockhaus/IngressPortalHistoryFlags
// @downloadURL https://github.com/brockhaus/IngressPortalHistoryFlags/raw/main/portalHistoryFlags.user.js
// @homepageURL https://github.com/brockhaus/IngressPortalHistoryFlags
// @description Shows Visited/Captured/Scouted status above portal markers
// @author EisFrei
// @include https://intel.ingress.com/*
// @match https://intel.ingress.com/*
// @grant none
// ==/UserScript==
function wrapper(plugin_info) {
// Make sure that window.plugin exists. IITC defines it as a no-op function,
// and other plugins assume the same.
if (typeof window.plugin !== "function") window.plugin = function () {};
const KEY_SETTINGS = "plugin-portal-history-flags";
window.plugin.PortalHistoryFlags = function () {};
const thisPlugin = window.plugin.PortalHistoryFlags;
// Name of the IITC build for first-party plugins
plugin_info.buildName = "PortalHistoryFlags";
// Datetime-derived version of the plugin
plugin_info.dateTimeVersion = "202102052258";
// ID/name of the plugin
plugin_info.pluginId = "portalhistoryflags";
function svgToIcon(str, x) {
const url = ("data:image/svg+xml," + encodeURIComponent(str)).replace(/#/g, '%23');
return new L.Icon({
iconUrl: url,
iconSize: [10, 10],
iconAnchor: [x, 20],
})
}
thisPlugin.removePortalFromMap = function (data) {
if (!data.portal._historyLayer) {
return;
}
thisPlugin.layerGroup.removeLayer(data.portal._historyLayer);
}
thisPlugin.addToPortalMap = function (data) {
if (data.portal.options.ent.length === 3 && data.portal.options.ent[2].length >= 19 && data.portal.options.ent[2][18] > 0) {
data.portal.options.data.agentVisited = (data.portal.options.ent[2][18] & 0b1) === 1;
data.portal.options.data.agentCaptured = (data.portal.options.ent[2][18] & 0b10) === 2;
data.portal.options.data.agentScouted = (data.portal.options.ent[2][18] & 0b100) === 4;
}
var tileParams = window.getCurrentZoomTileParameters();
if (tileParams.level === 0) {
drawPortalFlags(data.portal);
} else {
thisPlugin.removePortalFromMap(data);
}
}
thisPlugin.toggleDisplayMode = function () {
// round robin switch
let clickAnchor = document.getElementById(plugin_info.pluginId + '_mode');
if (thisPlugin.settings.highLighter) {
thisPlugin.settings.highLighter = false;
thisPlugin.settings.drawMissing = false;
thisPlugin.settings.drawScouter = true;
}
else if (!thisPlugin.settings.drawMissing && thisPlugin.settings.drawScouter) {
thisPlugin.settings.drawMissing = true;
thisPlugin.settings.drawScouter = true;
}
else if (thisPlugin.settings.drawMissing && thisPlugin.settings.drawScouter){
thisPlugin.settings.drawMissing = true;
thisPlugin.settings.drawScouter = false;
}
else {
thisPlugin.settings.highLighter = true;
thisPlugin.settings.drawMissing = false;
thisPlugin.settings.drawScouter = true;
}
clickAnchor.innerHTML = 'History Mode ' + getCurrentMode();
localStorage[KEY_SETTINGS] = JSON.stringify(thisPlugin.settings);
drawAllFlags();
}
function drawPortalFlags(portal) {
/*if (portal._historyLayer) {
portal._historyLayer.addTo(thisPlugin.layerGroup);
return;
}*/
const drawMissing = thisPlugin.settings.drawMissing;
const drawScouter = thisPlugin.settings.drawScouter;
const highLighter = thisPlugin.settings.highLighter;
portal._historyLayer = new L.LayerGroup();
if (!highLighter) {
if (drawMissing && !portal.options.data.agentVisited || !drawMissing && portal.options.data.agentVisited) {
L.marker(portal._latlng, {
icon: thisPlugin.iconVisited,
interactive: false,
keyboard: false,
}).addTo(portal._historyLayer);
}
if (drawMissing && !portal.options.data.agentCaptured || !drawMissing && portal.options.data.agentCaptured) {
L.marker(portal._latlng, {
icon: thisPlugin.iconCaptured,
interactive: false,
keyboard: false,
}).addTo(portal._historyLayer);
}
if (drawMissing && !portal.options.data.agentScouted || !drawMissing && portal.options.data.agentScouted) {
if (drawScouter) {
L.marker(portal._latlng, {
icon: thisPlugin.iconScouted,
interactive: false,
keyboard: false,
}).addTo(portal._historyLayer);
}
}
}
portal._historyLayer.addTo(thisPlugin.layerGroup);
}
thisPlugin.highlighter = {
highlight: function(data) {
if (!thisPlugin.isHighlightActive) return;
let portalLoaded = data.portal.options.ent.length === 3 && data.portal.options.ent[2].length > 4;
if (data.portal.options.ent.length === 3 && data.portal.options.ent[2].length >= 19 && data.portal.options.ent[2][18] > 0) {
data.portal.options.data.agentVisited = (data.portal.options.ent[2][18] & 0b1) === 1;
data.portal.options.data.agentCaptured = (data.portal.options.ent[2][18] & 0b10) === 2;
data.portal.options.data.agentScouted = (data.portal.options.ent[2][18] & 0b100) === 4;
}
var style = {};
if (portalLoaded) {
if (data.portal.options.data.agentCaptured) {
// captured (and, implied, visited too) - no highlights
} else if (data.portal.options.data.agentVisited) {
style.fillColor = 'yellow';
style.fillOpacity = 0.8;
} else {
// we have an 'uniqueInfo' entry for the portal, but it's not set visited or captured?
// could be used to flag a portal you don't plan to visit, so use a less opaque red
style.fillColor = 'red';
style.fillOpacity = 0.8;
}
}
data.portal.setStyle(style);
},
setSelected: function(active) {
thisPlugin.isHighlightActive = active;
}
}
function drawAllFlags() {
thisPlugin.layerGroup.clearLayers();
for (let id in window.portals) {
drawPortalFlags(window.portals[id]);
}
}
function getCurrentMode() {
let mode = '[N]';
if (thisPlugin.settings.highLighter) mode = '[H]';
else if (!thisPlugin.settings.drawScouter) mode = '[M-S]';
else if (thisPlugin.settings.drawMissing) mode = '[M]';
return mode;
}
function setup() {
let drawScouter = true;
let highLighter = false;
try {
thisPlugin.settings = JSON.parse(localStorage[KEY_SETTINGS]);
drawScouter = thisPlugin.settings.drawScouter;
highLighter = thisPlugin.settings.highLighter;
} catch (e) {
thisPlugin.settings = {
drawMissing: false,
drawScouter: drawScouter,
highLighter: highLighter,
};
localStorage[KEY_SETTINGS] = JSON.stringify(thisPlugin.settings);
}
thisPlugin.iconVisited = svgToIcon('<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><circle fill="#9538ff" cx="50" cy="50" r="50"/></svg>', 15);
thisPlugin.iconCaptured = svgToIcon('<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><circle fill="#ff0000" cx="50" cy="50" r="50"/></svg>', 5);
thisPlugin.iconScouted = svgToIcon('<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"><circle fill="#ff9c00" cx="50" cy="50" r="50"/></svg>', -5);
thisPlugin.layerGroup = new L.LayerGroup();
window.addLayerGroup('Portal History', thisPlugin.layerGroup, false);
window.addHook('portalAdded', thisPlugin.addToPortalMap);
window.addHook('portalRemoved', thisPlugin.removePortalFromMap);
window.addPortalHighlighter('Portal History', thisPlugin.highlighter);
let mode = getCurrentMode();
$('#toolbox').append('<a id="' + plugin_info.pluginId + '_mode" onclick="window.plugin.PortalHistoryFlags.toggleDisplayMode()">History Mode ' + mode + '</a>');
}
setup.info = plugin_info; //add the script info data to the function as a property
// if IITC has already booted, immediately run the 'setup' function
if (window.iitcLoaded) {
setup();
} else {
if (!window.bootPlugins) {
window.bootPlugins = [];
}
window.bootPlugins.push(setup);
}
}
(function () {
const plugin_info = {};
if (typeof GM_info !== 'undefined' && GM_info && GM_info.script) {
plugin_info.script = {
version: GM_info.script.version,
name: GM_info.script.name,
description: GM_info.script.description
};
}
// Greasemonkey. It will be quite hard to debug
if (typeof unsafeWindow != 'undefined' || typeof GM_info == 'undefined' || GM_info.scriptHandler != 'Tampermonkey') {
// inject code into site context
const script = document.createElement('script');
script.appendChild(document.createTextNode('(' + wrapper + ')(' + JSON.stringify(plugin_info) + ');'));
(document.body || document.head || document.documentElement).appendChild(script);
} else {
// Tampermonkey, run code directly
wrapper(plugin_info);
}
})();