WME MapRaid Helper
当前为
// ==UserScript==
// @name:hu WME MapRaider
// @name:en WME MapRaider
// @description:hu Magyar MapRaid Segéd
// @description:en WME MapRaid Helper
// @copyright 2014, ragacs
// @name WME MapRaider
// @description WME MapRaid Helper
// @version 0.4
// @include https://www.waze.com/editor/*
// @include https://www.waze.com/*/editor/*
// @include https://beta.waze.com/editor/*
// @include https://beta.waze.com/*/editor/*
// @namespace https://greasyfork.org/users/6330
// ==/UserScript==
var wmemr_version = "0.4";
var wmemr_col_num = 20;
var wmemr_col_str = 6;
var wmemr_col_title = 7;
/* bootstrap, will call initialiseMapRaider() */
function bootstrapMapRaider()
{
var bGreasemonkeyServiceDefined = false;
try {
bGreasemonkeyServiceDefined = (typeof Components.interfaces.gmIGreasemonkeyService === "object");
}
catch (err) { /* Ignore */ }
if (typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
unsafeWindow = ( function () {
var dummyElem = document.createElement('p');
dummyElem.setAttribute('onclick', 'return window;');
return dummyElem.onclick();
}) ();
}
/* begin running the code! */
setTimeout(initialiseMapRaider, 999);
}
/* helper function */
function getElementsByClassName(classname, node) {
if(!node) node = document.getElementsByTagName("body")[0];
var a = [];
var re = new RegExp('\\b' + classname + '\\b');
var els = node.getElementsByTagName("*");
for (var i=0,j=els.length; i<j; i++)
if (re.test(els[i].className)) a.push(els[i]);
return a;
}
function getId(node) {
return document.getElementById(node);
}
function processNewPermalinks()
{
var lines = getId("_taMapRaidInput").value.split("\n");
var mapRaiderCounter = getId("_inMapRaidStart").value;
if(lines.length == 1 && lines[0].search(/^CFG:\s*[A-Z]+,\s*[A-Z]+,\s*[A-Z]+$/i) == 0)
{
lines[0]=lines[0].toUpperCase();
var cfg = lines[0].slice(lines[0].indexOf(":") + 1).split(",");
cfg[0] = cfg[0].trim();
cfg[1] = cfg[1].trim();
cfg[2] = cfg[2].trim();
wmemr_col_num = cfg[0].charCodeAt(0) - 65;
if(cfg[0].length > 1) wmemr_col_num = (wmemr_col_num + 1) * 26 + cfg[0].charCodeAt(1) - 65;
wmemr_col_str = cfg[1].charCodeAt(0) - 65;
if(cfg[1].length > 1) wmemr_col_str = (wmemr_col_str + 1) * 26 + cfg[1].charCodeAt(1) - 65;
wmemr_col_title = cfg[2].charCodeAt(0) - 65;
if(cfg[2].length > 1) wmemr_col_title = (wmemr_col_title + 1) * 26 + cfg[2].charCodeAt(1) - 65;
console.log("WME-MapRaider:" + wmemr_col_num);
updateMapRaiderConfigStr();
return;
}
if(lines.length < 2) return;
for(var i=0; i<lines.length; i++)
{
var linestr = "";
var titlestr = "";
var lon = "", lat = "", zoom = "6", segs = "";
var cells = lines[i].split("\t");
if(cells.length < 1) continue;
for(var c=0; c < cells.length; c++)
{
// https://www.waze.com/editor/?lon=19.154683728784565&lat=47.46686402709748&zoom=6&marker=true&segments=102441276
if(cells[c].search(/https:\/\/(www|beta).waze.com\/.*editor/) == 0)
{
lon = cells[c].replace(/.*lon=/,"").replace(/&.*$/,"");
lat = cells[c].replace(/.*lat=/,"").replace(/&.*$/,"");
if(cells[c].search("zoom") > 0)
zoom = cells[c].replace(/.*zoom=/,"").replace(/&.*$/,"");
if(cells[c].search("segments") > 0)
segs = cells[c].replace(/.*segments=/,"").replace(/&.*$/,"");
}
else if(c == wmemr_col_str) linestr = cells[c];
else if(c == wmemr_col_title) titlestr = cells[c];
else if(c == wmemr_col_num) mapRaiderCounter = cells[c];
}
if(lon.length == 0 || lat.length == 0)
continue;
if(linestr.length == 0) linestr = "Bookmark";
else linestr = mapRaiderCounter++ + ". (*)" + linestr;
var item = document.createElement('div');
var redx = document.createElement('img');
redx.setAttribute("src", "http://www.iconsdb.com/icons/download/red/x-mark-3-16.png");
redx.onclick = function() {
var adiv = this.parentNode;
adiv.parentNode.removeChild(adiv);
}
item.innerHTML = linestr;
item.appendChild(redx);
item.setAttribute("lon", lon);
item.setAttribute("lat", lat);
item.setAttribute("zoom", zoom);
item.setAttribute("segs", segs);
item.setAttribute("title", titlestr);
item.onclick = function(event) {
event = event || window.event;
if (event.ctrlKey)
{
this.removeAttribute("style");
return;
}
var tmplatlon=new OpenLayers.LonLat(this.getAttribute("lon"), this.getAttribute("lat"));
tmplatlon.transform(Waze.map.displayProjection, Waze.map.getProjectionObject());
Waze.map.setCenter(tmplatlon, this.getAttribute("zoom"));
var segs = this.getAttribute("segs");
try
{
if(segs.length)
{
var segstrarray = segs.split(",");
var segarray = [];
for(var s=0; s < segstrarray.length; s++)
{
segarray.push(Waze.model.segments.get(segstrarray[s]));
}
Waze.selectionManager.select(segarray);
}
this.style.backgroundColor = "#50bb20";
}
catch(err)
{
this.style.backgroundColor = "#bb5020";
}
};
getId("_divMapRaidPermalinks").appendChild(item);
}
getId("_taMapRaidInput").value = "";
getId("_inMapRaidStart").value = mapRaiderCounter;
}
function clearMapRaiderVisited()
{
var listedlinks=getId("_divMapRaidPermalinks").childNodes;
for(var l = 0; l < listedlinks.length; l++)
{
var color = listedlinks.item(l).style.backgroundColor;
if( color )
{
getId("_divMapRaidPermalinks").removeChild(listedlinks.item(l--));
}
}
}
function clearMapRaiderAll()
{
getId("_divMapRaidPermalinks").innerHTML = '';
}
function updateMapRaiderConfigStr()
{
var line = "Cfg: ";
if(wmemr_col_num > 25) line += String.fromCharCode(~~(wmemr_col_num / 26) + 64);
line += String.fromCharCode(wmemr_col_num % 26 + 65) + ", ";
if(wmemr_col_str > 25) line += String.fromCharCode(~~(wmemr_col_str / 26) + 64);
line += String.fromCharCode(wmemr_col_str % 26 + 65) + ", ";
if(wmemr_col_title > 25) line += String.fromCharCode(~~(wmemr_col_title / 26) + 64);
line += String.fromCharCode(wmemr_col_title % 26 + 65);
getId("_divMapRaidConfig").innerHTML = line;
}
/* =========================================================================== */
function initialiseMapRaider()
{
// global variables
betaMode = location.hostname.match(/beta.waze.com/);
// add new box to left of the map
var addon = document.createElement('section');
addon.id = "mapraider-addon";
if (navigator.userAgent.match(/Chrome/)) {
addon.innerHTML = '<b>'
+ 'WME MapRaider</b> v' + wmemr_version;
} else {
addon.innerHTML = '<b>'
+ 'WME MapRaider</b> v' + wmemr_version;
}
section = document.createElement('p');
section.style.padding = "8px 16px";
//section.style.textIndent = "-16px";
section.id = "nameMapRaider";
section.innerHTML = '<button id="_btnMapRaidClearVisited" title="Tip: Use Ctrl+Click on items to clear visited state">Clear Visited</button>'
+ '<button id="_btnMapRaidClearAll">Clear All</button>'
+ '<div><b>Next No.:</b> <input type="number" value="1" title="Starting number (if index column is not found)" id="_inMapRaidStart"/></div>'
+ '<div><b>Table rows:</b> <textarea rows="500" cols="10" title="Copy here complete rows from the MapRaid table\n'
+ 'or use Cfg:<index column>,<name column>,<description column> to configure. Eg. Cfg: A, E, F" id="_taMapRaidInput"></textarea></div>'
+ '<div id="_divMapRaidConfig" title="Index Column, Name Column, Description Column"></div>'
+ '<div id="_divMapRaidPermalinks"></div>';
addon.appendChild(section);
var userTabs = getId('user-info');
var navTabs = getElementsByClassName('nav-tabs', userTabs)[0];
var tabContent = getElementsByClassName('tab-content', userTabs)[0];
newtab = document.createElement('li');
newtab.innerHTML = '<a href="#sidepanel-mapraider" data-toggle="tab">MapRaider</a>';
navTabs.appendChild(newtab);
addon.id = "sidepanel-mapraider";
addon.className = "tab-pane";
tabContent.appendChild(addon);
updateMapRaiderConfigStr();
getId('_taMapRaidInput').oninput = processNewPermalinks;
getId('_btnMapRaidClearVisited').onclick = clearMapRaiderVisited;
getId('_btnMapRaidClearAll').onclick = clearMapRaiderAll;
}
/* engage! =================================================================== */
bootstrapMapRaider();
/* end ======================================================================= */