IITC_DX_INTEGRATION

Integrate Dx Fielding Simulation

当前为 2015-01-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @id IITC_DX_INTEGRATION
  3. // @name IITC_DX_INTEGRATION
  4. // @version 0.1.2.1
  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. // @locale en-US
  13. // ==/UserScript==
  14.  
  15.  
  16. function wrapper() {
  17. // in case IITC is not available yet, define the base plugin object
  18. if (typeof window.plugin !== "function") {
  19. window.plugin = function() {
  20. };
  21. }
  22. // base context for plugin
  23. window.plugin.dxplugin = function() {
  24. };
  25. var self = window.plugin.dxplugin;
  26. window.plugin.dxplugin.dxMarkers = [];
  27. window.plugin.dxplugin.dxLayer = null;
  28. window.plugin.dxplugin.dxPortals = [];
  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. self.createPortalActionLink(latE6, lngE6, title);
  166. self.updateExport();
  167. self.export(true);
  168. };
  169. //Remove portal
  170. self.removePortal = function(latE6, lngE6, title)
  171. {
  172. var id = latE6 + 'x' + lngE6;
  173. if (window.plugin.dxplugin.dxPortals.hasOwnProperty(id))
  174. {
  175. window.plugin.dxplugin.dxPortals[id] = null;
  176. delete window.plugin.dxplugin.dxPortals[id];
  177. self.createPortalActionLink(latE6, lngE6, title);
  178. self.updateExport();
  179. self.export(true);
  180. }
  181. };
  182.  
  183. //Check if portal was already added
  184. self.getPortal = function(latE6, lngE6)
  185. {
  186. var id = latE6 + 'x' + lngE6;
  187. var ret = false;
  188. if (window.plugin.dxplugin.dxPortals.hasOwnProperty(id))
  189. {
  190. ret = window.plugin.dxplugin.dxPortals[id];
  191. }
  192. return ret;
  193. };
  194.  
  195. //Update Export
  196. self.updateExport = function()
  197. {
  198. var portalDatas = [];
  199. var jsonObject = {};
  200. for (var key in window.plugin.dxplugin.dxPortals) {
  201. if (window.plugin.dxplugin.dxPortals.hasOwnProperty(key))
  202. {
  203. portalDatas.push(window.plugin.dxplugin.dxPortals[key]);
  204. }
  205. }
  206. jsonObject.markers = portalDatas;
  207. $('#dxJSONinputExport').val(JSON.stringify(jsonObject));
  208. };
  209.  
  210. //Export selected portals
  211. self.export = function(update)
  212. {
  213. $('#dxWrapper').remove();
  214. if (update === undefined)
  215. {
  216. if ($('#dxWrapperExport').is(':visible'))
  217. {
  218. $('#dxWrapperExport').remove();
  219. return;
  220. }
  221. }
  222. $('#dxWrapperExport').remove();
  223. var wrapper = $('<div id="dxWrapperExport" style="margin: 10px;padding:5px;"></div>');
  224. var textarea = $('<textarea style="width:100%;max-width:100%;height: 200px;" id="dxJSONinputExport"></textarea>');
  225. var buttonClose = $('<input type="button" id="dxJSONinputCloseExport" value="Close" style="cursor:pointer;"/>');
  226. buttonClose.click(function() {
  227. $('#dxWrapperExport').remove();
  228. return;
  229. });
  230. wrapper.append(textarea);
  231. wrapper.append(buttonClose);
  232. wrapper.append('<br /><br />Copy and Import to <a href="http://ingress.dennesabing.com">Dx Field Simulator</a></a>');
  233. $("#toolbox").append(wrapper);
  234. self.updateExport();
  235. };
  236.  
  237. //Self PHP JS Explode
  238. self.__explode = function(delimiter, string, limit)
  239. {
  240. if (arguments.length < 2 || typeof delimiter === 'undefined' || typeof string === 'undefined')
  241. return null;
  242. if (delimiter === '' || delimiter === false || delimiter === null)
  243. return false;
  244. if (typeof delimiter === 'function' || typeof delimiter === 'object' || typeof string === 'function' || typeof string ===
  245. 'object') {
  246. return {
  247. 0: ''
  248. };
  249. }
  250. if (delimiter === true)
  251. delimiter = '1';
  252. delimiter += '';
  253. string += '';
  254. var s = string.split(delimiter);
  255. if (typeof limit === 'undefined')
  256. return s;
  257. if (limit === 0)
  258. limit = 1;
  259. if (limit > 0) {
  260. if (limit >= s.length)
  261. return s;
  262. return s.slice(0, limit - 1)
  263. .concat([s.slice(limit - 1)
  264. .join(delimiter)
  265. ]);
  266. }
  267. if (-limit >= s.length)
  268. return [];
  269. s.splice(s.length + limit);
  270. return s;
  271. };
  272. // setup function called by IITC
  273. self.setup = function init() {
  274. var linkImport = $("<a onclick=\"window.plugin.dxplugin.import();\" title=\"Import Dx JSON String\">Import DxJSON</a>");
  275. var linkExport = $("<a onclick=\"window.plugin.dxplugin.export();\" title=\"Export Dx JSON Portals\">Export DxPortals</a>");
  276. var linkDx = $("<a target=\"_blank\" href=\"http://ingress.dennesabing.com\" title=\"Dx Field Simulator\">DxFieldSim</a>");
  277. $("#toolbox").append(linkImport);
  278. $("#toolbox").append(linkExport);
  279. $("#toolbox").append(linkDx);
  280.  
  281. window.addHook('portalDetailsUpdated', function(e) {
  282. self.portalDetailsUpdated(e);
  283. });
  284.  
  285. delete self.setup;
  286. };
  287.  
  288. // IITC plugin setup
  289. if (window.iitcLoaded && typeof self.setup === "function") {
  290. self.setup();
  291. } else if (window.bootPlugins) {
  292. window.bootPlugins.push(self.setup);
  293. } else {
  294. window.bootPlugins = [self.setup];
  295. }
  296. }
  297. // inject plugin into page
  298. var script = document.createElement("script");
  299. script.appendChild(document.createTextNode("(" + wrapper + ")();"));
  300. (document.body || document.head || document.documentElement).appendChild(script);