1
0

Compare commits

...

6 Commits

2 changed files with 53 additions and 21 deletions

View File

@ -17,7 +17,7 @@ function getMediaURL(path) {
* they will also be cleaned up and returned as undefined, because we can't link to them correctly * they will also be cleaned up and returned as undefined, because we can't link to them correctly
*/ */
function bahnParseTrainName(dirty_train_name) { function bahnParseTrainName(dirty_train_name) {
var name_list = dirty_train_name.split(" "); var name_list = dirty_train_name.trim().replace(/ +/g, ' ').split(" ");
// Train name in format "STB 12 (23561)" // Train 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) == ')') { if(name_list.length == 3 && name_list[2].charAt(0) == '(' && name_list[2].charAt(name_list[2].length-1) == ')') {
@ -32,23 +32,55 @@ function bahnParseTrainName(dirty_train_name) {
return dirty_train_name; return dirty_train_name;
} }
var observer = new MutationObserver((mutations, observer) => { function domCreateLink(href) {
var link = document.createElement("a");
link.setAttribute("href", href);
return link;
}
function domCreateImage(src) {
var image = document.createElement("img");
image.setAttribute("src", src);
return image;
}
function domCreateButton(href, image_src) {
var link = domCreateLink(href);
var image = domCreateImage(image_src);
image.setAttribute("style", "height: 2em; vertical-align:middle;")
link.append(image);
return link;
}
function domCreateButtonMarudor(path) {
var button = domCreateButton("https://marudor.de" + path, getMediaURL("marudor.svg"));
return button;
}
var connection_result_observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => { mutations.forEach((mutation) => {
if (mutation.type === 'childList') { if (mutation.type === 'childList') {
var target = mutation.target; var target = mutation.target;
if (target.tagName === 'TD') { if (target.tagName === 'TD') {
var timetable = target.querySelector("td div.detailContainer table.result tbody"); var timetable = target.querySelector("td div.detailContainer table.result tbody");
var products = timetable.querySelectorAll("tr.first td.products");
products.forEach((product) => { var trains = timetable.querySelectorAll("tr.first td.products");
var train_name = bahnParseTrainName(product.querySelector("span a").innerText); trains.forEach((train) => {
if(typeof train_name !== 'undefined') { var train_names = train.querySelectorAll("span a");
product.innerHTML = product.innerHTML + ' <a href="https://marudor.de/details/' + train_name + '" target="_blank"><img src="' + getMediaURL("marudor.svg") + '" style="height: 2em; vertical-align:middle;"></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 stations = timetable.querySelectorAll("tr td.station"); var stations = timetable.querySelectorAll("tr td.station");
stations.forEach((station) => { stations.forEach((station) => {
var station_name = station.innerText; var station_name = station.innerText;
station.innerHTML = station.innerHTML + ' <a href="https://marudor.de/' + station_name + '" target="_blank"><img src="' + getMediaURL("marudor.svg") + '" style="height: 2em; vertical-align:middle;"></a>'; var marudor_button = domCreateButtonMarudor("/" + station_name);
station.append(marudor_button);
}); });
} }
@ -60,7 +92,7 @@ var observer = new MutationObserver((mutations, observer) => {
var target = document.getElementById('resultsOverview'); var target = document.getElementById('resultsOverview');
if(typeof target !== 'undefined') { if(typeof target !== 'undefined') {
observer.observe(target, { connection_result_observer.observe(target, {
subtree: true, subtree: true,
childList: true childList: true
}); });

View File

@ -2,7 +2,7 @@
"manifest_version": 2, "manifest_version": 2,
"name": "Bahn Insight", "name": "Bahn Insight",
"version": "0.3", "version": "0.4",
"description": "Extends the booking portal of bahn.de with linking to useful information.", "description": "Extends the booking portal of bahn.de with linking to useful information.",
"homepage_url": "https://git.clerie.de/clerie/bahn-insight/", "homepage_url": "https://git.clerie.de/clerie/bahn-insight/",