1
0

Compare commits

...

9 Commits

2 changed files with 192 additions and 23 deletions

View File

@ -32,73 +32,240 @@ function bahnParseTrainName(dirty_train_name) {
return dirty_train_name;
}
/**
* Returns HTML a as DOM object
*
* @param href URI as string
*/
function domCreateLink(href) {
var link = document.createElement("a");
link.setAttribute("href", href);
return link;
}
/**
* Returns HTML img as DOM object
*
* @param src Image URI as string
*/
function domCreateImage(src) {
var image = document.createElement("img");
image.setAttribute("src", src);
return image;
}
/**
* Returns HTML br as DOM object
*/
function domCreateLinebreak() {
var linebreak = document.createElement("br");
return linebreak;
}
/**
* Returns HTML span as DOM object
*/
function domCreateSpan() {
var span = document.createElement("span");
return span;
}
/**
* Returns an 'image button' like construct as DOM object
*
* @param href URI as string
* @param image_src Image URI as string
*/
function domCreateButton(href, image_src) {
var link = domCreateLink(href);
link.setAttribute("target", "_blank");
var image = domCreateImage(image_src);
image.setAttribute("style", "height: 2em; vertical-align:middle;")
image.setAttribute("style", "height: 16px; vertical-align:middle;");
link.append(image);
return link;
}
/**
* Returns an 'image buttom' to specifically link to marudor.de
*
* @param path URL path part after https://marudor.de
*/
function domCreateButtonMarudor(path) {
var button = domCreateButton("https://marudor.de" + path, getMediaURL("marudor.svg"));
button.setAttribute("title", "marudor.de");
return button;
}
/**
* Returns an 'image buttom' to specifically link to dbf.finalrewind.org
*
* @param path URL path part after https://dbf.finalrewind.org
*/
function domCreateButtonDbf(path) {
var button = domCreateButton("https://dbf.finalrewind.org" + path, getMediaURL("dbf.png"));
button.setAttribute("title", "dbf.finalrewind.org");
return button;
}
/**
* Returns a 'bahn-insight' element as DOM object
*
* Used to determine if this is an object set by this extension itself
*/
function domCreateBahnInsightField() {
var span = domCreateSpan();
span.setAttribute("class", "bahn-insight");
return span;
}
var connection_result_observer = new MutationObserver((mutations) => {
log("change detected")
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
var target = mutation.target;
if (target.tagName === 'TD') {
log("change is interesting");
var timetable = target.querySelector("td div.detailContainer table.result tbody");
var trains = timetable.querySelectorAll("tr.first td.products");
trains.forEach((train) => {
var train_names = train.querySelectorAll("span a");
train_names.forEach((train_name) => {
train_name_text = bahnParseTrainName(train_name.innerText);
if(typeof train_name_text !== 'undefined') {
var marudor_button = domCreateButtonMarudor("/details/" + train_name_text);
train_name.after(marudor_button);
/*
* COLLECT DATA
*/
var data = [];
var relations = timetable.querySelectorAll("tr.first");
relations.forEach((relation, i) => {
var relationend = relation.nextElementSibling;
data[i] = {};
data[i]["from"] = {}
data[i]["to"] = {}
// Move linebreaks from link inner, after our button
linebreaks = train_name.querySelectorAll("br");
// Depature station name
var relation_from = relation.querySelector("td.station");
data[i]["from"]["station"] = relation_from.innerText;
// Arrival station name
var relation_to = relationend.querySelector("td.station");
data[i]["to"]["station"] = relation_to.innerText;
// Departure time & current estimation
var relation_departure = relation.querySelector("td.time");
var relation_departure_list = relation_departure.firstChild.textContent.trim().split(" ");
data[i]["from"]["time"] = relation_departure_list[1];
data[i]["from"]["time_current"] = null;
var relation_departure_current = relation_departure.querySelector("span.delay, span.delayOnTime");
if(relation_departure_current != null) {
data[i]["from"]["time_current"] = relation_departure_current.innerText.trim();
}
// Arrival time & current estimation
var relation_arrival = relationend.querySelector("td.time");
var relation_arrival_list = relation_arrival.firstChild.textContent.trim().split(" ");
data[i]["to"]["time"] = relation_arrival_list[1];
data[i]["to"]["time_current"] = null;
var relation_arrival_current = relation_arrival.querySelector("span.delay, span.delayOnTime");
if(relation_arrival_current != null) {
data[i]["to"]["time_current"] = relation_arrival_current.innerText.trim();
}
// Departure platform
var relation_platform = relation.querySelector("td.platform");
data[i]["from"]["platform"] = relation_platform.innerText;
// Arrival platform
var relation_to_platform = relationend.querySelector("td.platform");
data[i]["to"]["platform"] = relation_to_platform.innerText;
// Travel products
var relation_products = relation.querySelector("td.products");
data[i]["products"] = [];
// Fetch all products for this travel
var relation_trains = relation_products.querySelectorAll("span a");
relation_trains.forEach((train, j) => {
data[i]["products"][j] = {};
// Prodcut details
data[i]["products"][j]["product"] = null;
data[i]["products"][j]["train_number"] = null;
data[i]["products"][j]["line_name"] = null;
var name_list = train.innerText.trim().replace(/ +/g, ' ').split(" ");
// Product name in format "STB 12 (23561)"
if(name_list.length == 3 && name_list[2].charAt(0) == '(' && name_list[2].charAt(name_list[2].length-1) == ')') {
data[i]["products"][j]["product"] = name_list[0];
data[i]["products"][j]["train_number"] = name_list[0] + " " + name_list[2].substring(1, name_list[2].length-1);
data[i]["products"][j]["line_name"] = name_list[0] + " " + name_list[1];
}
// Product name in format "ICE 234"
else {
data[i]["products"][j]["product"] = name_list[0];
data[i]["products"][j]["train_number"] = name_list[0] + " " + name_list[1];
}
});
// Travel information
var relation_items = relation.querySelectorAll("td");
var relation_info = relation_items[relation_items.length-1];
data[i]["info"] = relation_info.innerText;
});
log("data collected");
console.log(data);
/*
* INJECT CUSTOM UI
*/
var relations = timetable.querySelectorAll("tr.first");
relations.forEach((relation, i) => {
var relationend = relation.nextElementSibling;
var products = relation.querySelectorAll("td.products span a");
products.forEach((product, j) => {
// Field for 'bahn-insight' stuff
var bahn_insight_field = domCreateBahnInsightField();
product.after(bahn_insight_field);
// Button linking to marudor.de
var marudor_button = domCreateButtonMarudor("/details/" + data[i]["products"][j]["train_number"]);
bahn_insight_field.appendChild(marudor_button);
// Move linebreaks from link inner, after our 'bahn-insight' field
var linebreaks = product.querySelectorAll("br");
if(linebreaks.length != 0) {
linebreaks.forEach((linebreak) => {
train_name.removeChild(linebreak);
product.removeChild(linebreak);
});
marudor_button.after(domCreateLinebreak());
}
bahn_insight_field.after(domCreateLinebreak());
}
});
var from = relation.querySelector("td.station");
var bahn_insight_field = domCreateBahnInsightField();
from.appendChild(bahn_insight_field);
var marudor_button = domCreateButtonMarudor("/" + data[i]["from"]["station"]);
bahn_insight_field.appendChild(marudor_button);
var dbf_button = domCreateButtonDbf("/" + data[i]["from"]["station"]);
bahn_insight_field.appendChild(dbf_button);
var to = relationend.querySelector("td.station");
var bahn_insight_field = domCreateBahnInsightField();
to.appendChild(bahn_insight_field);
var marudor_button = domCreateButtonMarudor("/" + data[i]["to"]["station"]);
bahn_insight_field.appendChild(marudor_button);
var dbf_button = domCreateButtonDbf("/" + data[i]["to"]["station"]);
bahn_insight_field.appendChild(dbf_button);
});
var stations = timetable.querySelectorAll("tr td.station");
stations.forEach((station) => {
var station_name = station.innerText;
var marudor_button = domCreateButtonMarudor("/" + station_name);
station.append(marudor_button);
});
log("ui injected");
}
}
@ -113,4 +280,6 @@ if(typeof target !== 'undefined') {
subtree: true,
childList: true
});
log("observation started")
}

BIN
media/dbf.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 652 B