IITC_DX_INTEGRATION

Integrate Dx Fielding Simulation

  1. // ==UserScript==
  2. // @id IITC_DX_INTEGRATION
  3. // @name IITC_DX_INTEGRATION
  4. // @version 0.1.7.0
  5. // @namespace IITC_DX_INTEGRATION
  6. // @description Integrate Dx Fielding Simulation
  7. // @include https://www.ingress.com/intel*
  8. // @include http://www.ingress.com/intel*
  9. // @match https://www.ingress.com/intel*
  10. // @match http://www.ingress.com/intel*
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14.  
  15. function wrapper() {
  16. // in case IITC is not available yet, define the base plugin object
  17. if (typeof window.plugin !== "function") {
  18. window.plugin = function() {
  19. };
  20. }
  21. // base context for plugin
  22. window.plugin.dxplugin = function() {
  23. };
  24. var self = window.plugin.dxplugin;
  25. window.plugin.dxplugin.dxMarkers = [];
  26. window.plugin.dxplugin.dxLayer = null;
  27. window.plugin.dxplugin.dxPortals = [];
  28. window.plugin.dxplugin.dxPortalsMarker = [];
  29. // custom dialog wrapper with more flexibility
  30. self.import = function importJSON() {
  31. $('#dxWrapperExport').remove();
  32. if ($('#dxWrapper').is(':visible'))
  33. {
  34. $('#dxWrapper').remove();
  35. return;
  36. }
  37. $('#dxWrapper').remove();
  38. var wrapper = $('<div id="dxWrapper" style="margin: 10px;padding:5px;"></div>');
  39. var textarea = $('<textarea style="width:100%;max-width:100%;height: 200px;" id="dxJSONinput"></textarea>');
  40. var buttonSubmit = $('<input type="button" id="dxJSONinputSubmit" value="Process" style="cursor:pointer;"/>');
  41. var buttonCancel = $('<input type="button" id="dxJSONinputCancel" value="Cancel" style="cursor:pointer;"/>');
  42. var buttonDiscard = $('<input type="button" id="dxJSONinputDiscard" value="Discard" style="cursor:pointer;"/>');
  43. buttonSubmit.click(function() {
  44. if ($('#dxJSONinput').val() !== '')
  45. {
  46. self.processJSON($.parseJSON($('#dxJSONinput').val()));
  47. }
  48. });
  49. buttonDiscard.click(function() {
  50. self.discard();
  51. });
  52. buttonCancel.click(function() {
  53. self.discard();
  54. $('#dxWrapper').remove();
  55. });
  56. wrapper.append(textarea);
  57. wrapper.append(buttonSubmit);
  58. wrapper.append(buttonCancel);
  59. wrapper.append(buttonDiscard);
  60. $("#toolbox").append(wrapper);
  61. };
  62. //Process submit JSON String
  63. self.processJSON = function(jsonObject)
  64. {
  65. if (jsonObject.markers !== undefined && jsonObject.markers.length > 0)
  66. {
  67. self.discard();
  68. //markers
  69. window.plugin.dxplugin.dxMarkers = jsonObject.markers;
  70. //links
  71. if (jsonObject.links !== '')
  72. {
  73. $.each(jsonObject.links, function(i, link) {
  74.  
  75. var nodes = self.__explode('_', link);
  76. var org = self.getPortalData(nodes[0]);
  77. var dst = self.getPortalData(nodes[1]);
  78. var latlngs = [
  79. L.latLng(org.lat, org.lng),
  80. L.latLng(dst.lat, dst.lng)
  81. ];
  82. self.add(L.geodesicPolyline(latlngs, self.getPolyOptions()));
  83. });
  84. }
  85. }
  86. };
  87.  
  88. //REturn the Portal DAta
  89. self.getPortalData = function(id)
  90. {
  91. var portal = null;
  92. $.each(window.plugin.dxplugin.dxMarkers, function(i, marker) {
  93. var val = self.__explode(':', marker);
  94. if (val[0] === id)
  95. {
  96. portal = {
  97. id: val[0],
  98. name: val[1],
  99. lat: parseFloat(val[2]),
  100. lng: parseFloat(val[3]),
  101. value: marker
  102. };
  103. }
  104. });
  105. return portal;
  106. };
  107.  
  108. // this.createLinkEntity(ent);
  109. //Add dxObject
  110. self.add = function(poly)
  111. {
  112. if (window.plugin.dxplugin.dxLayer === null)
  113. {
  114. window.plugin.dxplugin.dxLayer = L.layerGroup();
  115. map.addLayer(window.plugin.dxplugin.dxLayer, true);
  116. }
  117. window.plugin.dxplugin.dxLayer.addLayer(poly);
  118. };
  119.  
  120. //Remove all dxObjects
  121. self.discard = function()
  122. {
  123. if (window.plugin.dxplugin.dxLayer !== null)
  124. {
  125. map.removeLayer(window.plugin.dxplugin.dxLayer);
  126. }
  127. window.plugin.dxplugin.dxLayer = null;
  128. };
  129.  
  130. self.getPolyOptions = function()
  131. {
  132. var color = '#FFA500';
  133. var options = {
  134. opacity: 1,
  135. weight: 1,
  136. clickable: false,
  137. color: color
  138. };
  139. return options;
  140. };
  141.  
  142. //When portaldetailsIs updated
  143. self.portalDetailsUpdated = function(obj)
  144. {
  145. self.createPortalActionLink(obj.portalData.latE6, obj.portalData.lngE6, obj.portalData.title);
  146. };
  147.  
  148. //Create links
  149. self.createPortalActionLink = function(latE6, lngE6, title)
  150. {
  151. $('#dxPortalDetailsLink').remove();
  152. var portalDetailsLink = '<a href="#" onclick="window.plugin.dxplugin.addPortal(' + latE6 + ', ' + lngE6 + ', \'' + title + '\')" title="Add to Dx Portals">Add to Dx</a>';
  153. if (self.getPortal(latE6, lngE6) !== false)
  154. {
  155. portalDetailsLink = '<a href="#" style="color:red !important;" onclick="window.plugin.dxplugin.removePortal(' + latE6 + ', ' + lngE6 + ', \'' + title + '\')" title="Remove from Dx Portals">Remove from Dx</a>';
  156. }
  157. $('#portaldetails .linkdetails').append('<aside id="dxPortalDetailsLink">' + portalDetailsLink + '</aside>');
  158. };
  159.  
  160. //Add Portal
  161. self.addPortal = function(latE6, lngE6, title)
  162. {
  163. var id = latE6 + 'x' + lngE6;
  164. window.plugin.dxplugin.dxPortals[id] = id + ':' + title + ':' + (latE6 / 1E6) + ':' + (lngE6 / 1E6);
  165. var icon = L.icon({
  166. iconUrl: 'https://cdn0.iconfinder.com/data/icons/small-n-flat/24/678111-map-marker-32.png',
  167. iconRetinaUrl: 'https://cdn0.iconfinder.com/data/icons/small-n-flat/24/678111-map-marker-32.png',
  168. iconSize: [32, 32],
  169. iconAnchor: [15, 32]
  170. });
  171. var marker = L.marker([(latE6 / 1E6), lngE6 / 1E6], {icon: icon}).bindPopup(title);
  172. self.add(marker);
  173. window.plugin.dxplugin.dxPortalsMarker[id] = marker;
  174. self.createPortalActionLink(latE6, lngE6, title);
  175. self.updateExport();
  176. self.export(true);
  177. };
  178. //Remove portal
  179. self.removePortal = function(latE6, lngE6, title)
  180. {
  181. var id = latE6 + 'x' + lngE6;
  182. if (window.plugin.dxplugin.dxPortals.hasOwnProperty(id))
  183. {
  184. window.plugin.dxplugin.dxPortals[id] = null;
  185. window.plugin.dxplugin.dxLayer.removeLayer(window.plugin.dxplugin.dxPortalsMarker[id]);
  186. window.plugin.dxplugin.dxPortalsMarker[id] = null;
  187. delete window.plugin.dxplugin.dxPortals[id];
  188. delete window.plugin.dxplugin.dxPortalsMarker[id];
  189. self.createPortalActionLink(latE6, lngE6, title);
  190. self.updateExport();
  191. self.export(true);
  192. }
  193. };
  194.  
  195. //Check if portal was already added
  196. self.getPortal = function(latE6, lngE6)
  197. {
  198. var id = latE6 + 'x' + lngE6;
  199. var ret = false;
  200. if (window.plugin.dxplugin.dxPortals.hasOwnProperty(id))
  201. {
  202. ret = window.plugin.dxplugin.dxPortals[id];
  203. }
  204. return ret;
  205. };
  206.  
  207. //Update Export
  208. self.updateExport = function()
  209. {
  210. var portalDatas = [];
  211. var jsonObject = {};
  212. for (var key in window.plugin.dxplugin.dxPortals) {
  213. if (window.plugin.dxplugin.dxPortals.hasOwnProperty(key))
  214. {
  215. portalDatas.push(window.plugin.dxplugin.dxPortals[key]);
  216. }
  217. }
  218. jsonObject.markers = portalDatas;
  219. $('#dxJSONinputExport').val(JSON.stringify(jsonObject));
  220. };
  221.  
  222. //Export selected portals
  223. self.export = function(update)
  224. {
  225. $('#dxWrapper').remove();
  226. if (update === undefined)
  227. {
  228. if ($('#dxWrapperExport').is(':visible'))
  229. {
  230. $('#dxWrapperExport').remove();
  231. return;
  232. }
  233. }
  234. $('#dxWrapperExport').remove();
  235. var wrapper = $('<div id="dxWrapperExport" style="margin: 10px;padding:5px;"></div>');
  236. var textarea = $('<textarea style="width:100%;max-width:100%;height: 200px;" id="dxJSONinputExport"></textarea>');
  237. var buttonClose = $('<input type="button" id="dxJSONinputCloseExport" value="Close" style="cursor:pointer;"/>');
  238. var buttonRemoveAll = $('<input type="button" id="dxJSONRemoveAllPortals" value="Remove all DxPortals" style="cursor:pointer;"/>');
  239. buttonClose.click(function() {
  240. $('#dxWrapperExport').remove();
  241. return;
  242. });
  243. buttonRemoveAll.click(function() {
  244. for (var id in window.plugin.dxplugin.dxPortals) {
  245. if (window.plugin.dxplugin.dxPortals.hasOwnProperty(id))
  246. {
  247. window.plugin.dxplugin.dxPortals[id] = null;
  248. window.plugin.dxplugin.dxLayer.removeLayer(window.plugin.dxplugin.dxPortalsMarker[id]);
  249. window.plugin.dxplugin.dxPortalsMarker[id] = null;
  250. delete window.plugin.dxplugin.dxPortals[id];
  251. delete window.plugin.dxplugin.dxPortalsMarker[id];
  252. }
  253. }
  254. self.updateExport();
  255. self.export(true);
  256. });
  257. wrapper.append(textarea);
  258. wrapper.append(buttonClose);
  259. wrapper.append(buttonRemoveAll);
  260. wrapper.append('<br /><br />Copy and Import to <a target="_blank" href="http://ingress.dennesabing.com">Dx Field Simulator</a></a>');
  261. $("#toolbox").append(wrapper);
  262. self.updateExport();
  263. };
  264.  
  265. //Self PHP JS Explode
  266. self.__explode = function(delimiter, string, limit)
  267. {
  268. if (arguments.length < 2 || typeof delimiter === 'undefined' || typeof string === 'undefined')
  269. return null;
  270. if (delimiter === '' || delimiter === false || delimiter === null)
  271. return false;
  272. if (typeof delimiter === 'function' || typeof delimiter === 'object' || typeof string === 'function' || typeof string ===
  273. 'object') {
  274. return {
  275. 0: ''
  276. };
  277. }
  278. if (delimiter === true)
  279. delimiter = '1';
  280. delimiter += '';
  281. string += '';
  282. var s = string.split(delimiter);
  283. if (typeof limit === 'undefined')
  284. return s;
  285. if (limit === 0)
  286. limit = 1;
  287. if (limit > 0) {
  288. if (limit >= s.length)
  289. return s;
  290. return s.slice(0, limit - 1)
  291. .concat([s.slice(limit - 1)
  292. .join(delimiter)
  293. ]);
  294. }
  295. if (-limit >= s.length)
  296. return [];
  297. s.splice(s.length + limit);
  298. return s;
  299. };
  300. // setup function called by IITC
  301. self.setup = function init() {
  302. var linkImport = $("<a onclick=\"window.plugin.dxplugin.import();\" title=\"Import Dx JSON String\">Import DxJSON</a>");
  303. var linkExport = $("<a onclick=\"window.plugin.dxplugin.export();\" title=\"Export Dx JSON Portals\">Export DxPortals</a>");
  304. var linkDx = $("<a target=\"_blank\" href=\"http://ingress.dennesabing.com\" title=\"Dx Field Simulator\">DxFieldSim</a>");
  305. $("#toolbox").append(linkImport);
  306. $("#toolbox").append(linkExport);
  307. $("#toolbox").append(linkDx);
  308.  
  309. window.addHook('portalDetailsUpdated', function(e) {
  310. self.portalDetailsUpdated(e);
  311. });
  312.  
  313. delete self.setup;
  314. };
  315.  
  316. // IITC plugin setup
  317. if (window.iitcLoaded && typeof self.setup === "function") {
  318. self.setup();
  319. } else if (window.bootPlugins) {
  320. window.bootPlugins.push(self.setup);
  321. } else {
  322. window.bootPlugins = [self.setup];
  323. }
  324. }
  325. // inject plugin into page
  326. var script = document.createElement("script");
  327. script.appendChild(document.createTextNode("(" + wrapper + ")();"));
  328. (document.body || document.head || document.documentElement).appendChild(script);