Add hyperlinks to GameData
目前為
// ==UserScript==
// @name Atlantis GameData hyperlinks
// @namespace http://atlantis-pbem.com/data
// @version 0.002
// @description Add hyperlinks to GameData
// @author Anton
// @match http://atlantis-pbem.com/data
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
GM_addStyle(`
.gameDataMenu {
position: fixed;
right: 0;
top: 100px;
width: 350px;
border-top: 1px solid #CCC;
border-left: 1px solid #CCC;
border-bottom: 1px solid #CCC;
border-top-left-radius: 8px;
border-bottom-left-radius: 8px;
padding: 8px;
}
.gameDataLink, pre > a.mainlink {
position: relative;
top: -80px;
}
#skills-ph, #items-ph {
padding-left: 8px;
font-size: 70%;
}
#filtr {
margin-left: 8px;
padding: 1px;
border: 1px solid #CCC;
}
`);
let body = document.querySelector('body');
let container = document.querySelector('pre');
let data = container.innerHTML;
let dataLines = data.split(/\r?\n/);
let skillReportLine = dataLines.indexOf("Skill reports:");
let itemReportLine = dataLines.indexOf("Item reports:");
let objectReportLine = dataLines.indexOf("Object reports:");
let controlPanel = document.createElement('div');
controlPanel.className = 'gameDataMenu';
controlPanel.innerHTML = `
<div><label for="filtr">Search: </label><input id="filtr" type="text"></div>
<a href="#skills">Skills</a>
<div id="skills-ph"></div>
<a href="#items">Items</a>
<div id="items-ph"></div>
<a href="#objects">Objects</a>
`;
body.append(controlPanel);
dataLines[skillReportLine] = '<a name="skills" class="gameDataLink"></a>Skill reports:';
dataLines[itemReportLine] = '<a name="items" class="gameDataLink"></a>Item reports:';
dataLines[objectReportLine] = '<a name="objects" class="gameDataLink"></a>Object reports:';
let skillsArray = [];
let objectsArray = [];
let skillsDescription = {};
let objectsDescription = {};
let skillsDesc = {};
let objectDesc = {};
let addToArray = (arr, key, item) => {
if (arr != '' && key != '') {
if (!arr[key]) arr[key] = [];
if (arr[key].indexOf(item) == -1) arr[key].push(item);
}
};
let analyzePart = (s, run, prevSkill, prevObject) => {
let parts = s.match(/(.*)(\[(.{3,4})\])(.*)/);
if (parts && parts.length > 0) {
let skillAddon = (skillsArray.indexOf(parts[3] + '1') != -1) ? '1' : '';
addToArray(skillsDescription, prevSkill, parts[3] + skillAddon);
addToArray(objectsDescription, prevObject, parts[3]);
return analyzePart(parts[1], run, prevSkill, prevObject) + '<a href="#' + parts[3] + skillAddon + '">' + parts[2] + '</a>' + analyzePart(parts[4], run, prevSkill, prevObject);
}
return s;
};
for (let run = 0; run < 2; run++) {
let prevLine = '';
let prevSkill = '';
let prevObject = '';
for (let i = 0; i < dataLines.length; i++) {
let str = dataLines[i];
// mining [MINI] 3: A unit with this skill may PRODUCE mithril [MITH] at
let searchForName = str.match(/(\S.*)(\[(.{3,4})\].(\d)):(.*)/);
if (searchForName && searchForName.length > 0 && prevLine.trim() == '') {
let firstPart = searchForName[1]; // mining
let linkText = searchForName[2]; // [MINI] 3
let name = searchForName[3]; // MINI
let level = parseInt(searchForName[4]); // 3
let lastPart = searchForName[5]; // A unit with this skill may PRODUCE mithril [MITH] at
let linkName = name + level;
prevSkill = name + '1';
prevObject = '';
if (skillsArray.indexOf(linkName) == -1) {
skillsArray.push(linkName);
lastPart = analyzePart(lastPart, run, prevSkill, prevObject);
dataLines[i] = firstPart + '<a class="mainlink" name="' + linkName + '"></a>' + linkText + ':' + lastPart;
}
} else {
// winged horse [WING], weight 50, walking capacity 20, riding capacity
searchForName = str.match(/(\S.*)(\[(.{3,4})\])[,\.](.*)/);
if (searchForName && searchForName.length > 0 && prevLine.trim() == '') {
let firstPart = searchForName[1]; // winged horse
let linkText = searchForName[2]; // [WING]
let name = searchForName[3]; // WING
let lastPart = searchForName[4]; // weight 50, walking capacity 20, riding capacity
let linkName = name;
prevSkill = '';
prevObject = name;
if (objectsArray.indexOf(linkName) == -1) {
objectsArray.push(linkName);
lastPart = analyzePart(lastPart, run, prevSkill, prevObject);
dataLines[i] = firstPart + '<a class="mainlink" name="' + linkName + '"></a>' + linkText + ',' + lastPart;
}
} else {
if (run == 1) dataLines[i] = analyzePart(str, run, prevSkill, prevObject);
}
}
if (str.trim() == '') {
prevSkill = '';
prevObject = '';
} else if (prevSkill != '' && run == 0) {
if (!skillsDesc[prevSkill]) skillsDesc[prevSkill] = str.trim().toUpperCase();
else skillsDesc[prevSkill] += ' ' + str.trim().toUpperCase();
} else if (prevObject != '' && run == 0) {
if (!objectDesc[prevObject]) objectDesc[prevObject] = str.trim().toUpperCase();
else objectDesc[prevObject] += ' ' + str.trim().toUpperCase();
}
prevLine = str;
}
}
container.innerHTML = dataLines.join("\n");
let skillHasObject = (skil, filterValue) => {
let found = skillsDescription[skil] && !!(skillsDescription[skil].find(o => o.indexOf(filterValue) != -1));
let foundInDesc = skillsDesc[skil] && skillsDesc[skil].indexOf(filterValue) != -1;
return found || foundInDesc;
};
let objectHasSkill = (ob, filterValue) => {
let found = objectsDescription[ob] && !!(objectsDescription[ob].find(o => o.indexOf(filterValue) != -1));
let foundInDesc = objectDesc[ob] && objectDesc[ob].indexOf(filterValue) != -1;
return found || foundInDesc;
};
let updateLists = (filterValue) => {
let skillsPH = document.querySelector('#skills-ph');
skillsPH.innerHTML = skillsArray
.filter((e) => e[e.length - 1] == 1)
.filter((e) => e.indexOf(filterValue) != -1 || skillHasObject(e, filterValue))
.map((e) => e.substr(0, e.length - 1))
.map((e) => '<a href="#' + e + '1">' + e + '</a>')
.join(', ');
let itemsPH = document.querySelector('#items-ph');
itemsPH.innerHTML = objectsArray
.filter((e) => e.indexOf(filterValue) != -1 || objectHasSkill(e, filterValue))
.map((e) => '<a href="#' + e + '">' + e + '</a>')
.join(', ');
}
updateLists('');
let inp = document.querySelector('#filtr');
inp.onkeyup = (e) => {
updateLists(String(inp.value).toUpperCase());
};
console.log(skillsDesc);
console.log(objectDesc);
})();