Add hyperlinks to GameData
目前為
// ==UserScript==
// @name Atlantis GameData hyperlinks
// @namespace http://atlantis-pbem.com/data
// @version 0.001
// @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 analyzePart = (s, run) => {
let parts = s.match(/(.*)(\[(.{3,4})\])(.*)/);
if (parts && parts.length > 0) {
// link to object?
let skillAddon = (skillsArray.indexOf(parts[3] + '1') != -1) ? '1' : '';
return analyzePart(parts[1]) + '<a href="#' + parts[3] + skillAddon + '">' + parts[2] + '</a>' + analyzePart(parts[4]);
}
return s;
};
for (let run = 0; run < 2; run++) {
let prevLine = '';
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;
if (skillsArray.indexOf(linkName) == -1) {
skillsArray.push(linkName);
lastPart = analyzePart(lastPart, run);
str = firstPart + '<a class="mainlink" name="' + linkName + '"></a>' + linkText + ':' + lastPart;
dataLines[i] = str;
}
} 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;
if (objectsArray.indexOf(linkName) == -1) {
objectsArray.push(linkName);
lastPart = analyzePart(lastPart, run);
str = firstPart + '<a class="mainlink" name="' + linkName + '"></a>' + linkText + ',' + lastPart;
dataLines[i] = str;
}
} else {
if (run == 1) dataLines[i] = analyzePart(str, run);
}
}
prevLine = str;
}
}
container.innerHTML = dataLines.join("\n");
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)
.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)
.map((e) => '<a href="#' + e + '">' + e + '</a>')
.join(', ');
}
updateLists('');
let inp = document.querySelector('#filtr');
inp.onkeyup = (e) => {
updateLists(String(inp.value).toUpperCase());
};
})();