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;
}
/**
* Returns HTML span as DOM object
*/
function domCreateSpan() {
var span = document.createElement("span");
return span;
}
/**
* Returns an 'image button' like construct as DOM object
*
@ -87,6 +95,17 @@ function domCreateButtonMarudor(path) {
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")
@ -189,34 +208,43 @@ var connection_result_observer = new MutationObserver((mutations) => {
* INJECT CUSTOM UI
*/
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);
var relations = timetable.querySelectorAll("tr.first");
relations.forEach((relation, i) => {
var relationend = relation.nextElementSibling;
// Move linebreaks from link inner, after our button
linebreaks = train_name.querySelectorAll("br");
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 stations = timetable.querySelectorAll("tr td.station");
stations.forEach((station) => {
var station_name = station.innerText;
var marudor_button = domCreateButtonMarudor("/" + station_name);
station.append(marudor_button);
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 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");