1
0

Rewrite injection process and pack all added elements into containers

This commit is contained in:
clerie 2020-09-19 21:42:11 +02:00
parent d9fa338813
commit 33cd61e116

View File

@ -62,6 +62,14 @@ function domCreateLinebreak() {
return linebreak; 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 * Returns an 'image button' like construct as DOM object
* *
@ -87,6 +95,17 @@ function domCreateButtonMarudor(path) {
return button; 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) => { var connection_result_observer = new MutationObserver((mutations) => {
log("change detected") log("change detected")
@ -189,34 +208,43 @@ var connection_result_observer = new MutationObserver((mutations) => {
* INJECT CUSTOM UI * INJECT CUSTOM UI
*/ */
var trains = timetable.querySelectorAll("tr.first td.products"); var relations = timetable.querySelectorAll("tr.first");
trains.forEach((train) => { relations.forEach((relation, i) => {
var train_names = train.querySelectorAll("span a"); var relationend = relation.nextElementSibling;
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);
// Move linebreaks from link inner, after our button var products = relation.querySelectorAll("td.products span a");
linebreaks = train_name.querySelectorAll("br"); 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) { if(linebreaks.length != 0) {
linebreaks.forEach((linebreak) => { linebreaks.forEach((linebreak) => {
train_name.removeChild(linebreak); product.removeChild(linebreak);
}); });
bahn_insight_field.after(domCreateLinebreak());
marudor_button.after(domCreateLinebreak());
}
} }
}); });
});
var stations = timetable.querySelectorAll("tr td.station"); var from = relation.querySelector("td.station");
stations.forEach((station) => { var bahn_insight_field = domCreateBahnInsightField();
var station_name = station.innerText; from.appendChild(bahn_insight_field);
var marudor_button = domCreateButtonMarudor("/" + station_name);
station.append(marudor_button); var marudor_button = domCreateButtonMarudor("/" + data[i]["from"]["station"]);
bahn_insight_field.appendChild(marudor_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);
}); });
log("ui injected"); log("ui injected");