Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save duhow/b191eced68deea7d9be376ab9b2b4c16 to your computer and use it in GitHub Desktop.
Save duhow/b191eced68deea7d9be376ab9b2b4c16 to your computer and use it in GitHub Desktop.
IITC plugin: Ingress KML Exporter
// ==UserScript==
// @id iitc-plugin-CSV@pad
// @name IITC plugin: Ingress KML Exporter
// @author Dron007 - enhanced by duhowpi
// @category Keys
// @version 1.0.20161122.02
// @namespace https://github.com/jonatkins/ingress-intel-total-conversion
// @description Exports portals currently in view for use with Google Map (KML Format Customized with more data).
// @include https://www.ingress.com/intel*
// @include http://www.ingress.com/intel*
// @match https://www.ingress.com/intel*
// @match http://www.ingress.com/intel*
// @grant none
// ==/UserScript==
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.ingressKMLexporter = function() {};
var self = window.plugin.ingressKMLexporter;
// custom dialog wrapper with more flexibility
self.TEAM_STYLES = ["NEUTRAL", "RES", "ENL"];
self.TEAM_COLORS = ["B00066FF", "B0D09205", "B002BF02"];
self.portalInScreen = function portalInScreen( p ) {
return map.getBounds().contains(p.getLatLng());
};
self.genStyle = function genStyle(team, o) {
var teamStyleName = self.TEAM_STYLES[team];
var teamStyleColor = self.TEAM_COLORS[team];
o.push('<Style id="' + teamStyleName + '-inactive">');
o.push(' <IconStyle>');
o.push(' <Icon><href>http://www.gstatic.com/mapspro/images/stock/959-wht-circle-blank.png</href></Icon>');
o.push(' <color>' + teamStyleColor + '</color>');
o.push(' </IconStyle>');
o.push(' <LabelStyle><scale>0</scale></LabelStyle>');
o.push(' <BalloonStyle><text><![CDATA[$[description]]]></text></BalloonStyle>');
o.push('</Style>');
o.push('<Style id="' + teamStyleName + '-active">');
o.push(' <IconStyle>');
o.push(' <Icon><href>http://www.gstatic.com/mapspro/images/stock/959-wht-circle-blank.png</href></Icon>');
o.push(' <color>' + teamStyleColor + '</color>');
o.push(' </IconStyle>');
o.push(' <LabelStyle><scale>1</scale></LabelStyle>');
o.push(' <BalloonStyle><text><![CDATA[$[description]]]></text></BalloonStyle>');
o.push('</Style>');
o.push('<StyleMap id="' + teamStyleName + '">');
o.push(' <Pair><key>normal</key><styleUrl>#' + teamStyleName + '-inactive</styleUrl></Pair>');
o.push(' <Pair><key>highlight</key><styleUrl>#' + teamStyleName + '-active</styleUrl></Pair>');
o.push('</StyleMap>');
};
self.gen = function gen() {
var o = [];
var inBounds = function( portal ) {
return self.portalInScreen( portal );
};
// var string = "Portal selection based on screen boundaries.";
o.push('<?xml version="1.0" encoding="UTF-8"?>');
o.push('<kml xmlns="http://www.opengis.net/kml/2.2">');
o.push(' <Document>');
o.push(' <name>Import from IITC</name>');
for (var x in window.portals) {
var p = window.portals[x];
if (inBounds(p)) {
// console.log(p);
var title = p.options.data.title;
var level = p.options.data.level;
var resCount = p.options.data.resCount;
var health = p.options.data.health;
var imgLink = p.options.data.image;
title = title.replace(/["«»]/g, '&quot;');//'""');
title = title.replace(/'/g, '&apos;');
title = title.replace(/</g, '&lt;');
title = title.replace(/>/g, '&gt;');
title = title.replace(/&/g, '&amp;');
var teamStyle = self.TEAM_STYLES[p.options.team];
o.push(' <Placemark>');
o.push(' <styleUrl>' + teamStyle + '</styleUrl>');
o.push(' <Icon>' + imgLink + '</Icon>');
o.push(' <Level>' + level + '</Level>');
o.push(' <Resonators>' + resCount + '</Resonators>');
o.push(' <Health>' + health + '</Health>');
o.push(' <name>' + title + '</name>');
o.push(' <Point>');
o.push(' <coordinates>' + p._latlng.lng + "," + p._latlng.lat + ',0.0</coordinates>');
o.push(' <lat>' + p._latlng.lat + '</lat>');
o.push(' <lng>' + p._latlng.lng + '</lng>');
o.push(' </Point>');
o.push(' </Placemark>');
}
}
self.genStyle(0, o);
self.genStyle(1, o);
self.genStyle(2, o);
o.push(' </Document>');
o.push('</kml>');
var xmlput = o.join("\n");
var xmlfile = 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent( xmlput );
function pad (str, max) {
str = str.toString();
return str.length < max ? pad("0" + str, max) : str;
}
var dt = new Date();
var dtf = pad(dt.getMonth()+1, 2) + pad(dt.getDate(), 2) + '_' + pad(dt.getHours(), 2) + pad(dt.getMinutes(), 2) + pad(dt.getSeconds(), 2);
var link = document.createElement("a");
link.download = 'kml_' + dtf + ".kml";
link.href = xmlfile;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
return true;
};
// setup function called by IITC
self.setup = function init() {
// add controls to toolbox
var link = $("<a onclick=\"window.plugin.ingressKMLexporter.gen();\" title=\"Generate KML list of portals and locations.\">KML Export</a>");
$("#toolbox").append(link);
// delete setup to ensure init can't be run again
delete self.setup;
};
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment