Skip to content

Instantly share code, notes, and snippets.

@PhilippIRL
Last active August 27, 2022 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilippIRL/adedb36f1a89c1097372d854493a893e to your computer and use it in GitHub Desktop.
Save PhilippIRL/adedb36f1a89c1097372d854493a893e to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name bahn.expert Regenbogen-ICE Integration
// @namespace https://bahn.expert/regenbogenice
// @version 0.0.1
// @author PhilippIRL
// @match https://bahn.expert/*
// @match https://beta.bahn.expert/*
// @updateURL https://gist.github.com/PhilippIRL/adedb36f1a89c1097372d854493a893e/raw/bahnexpert-regenbogenice.user.js
// @icon https://www.google.com/s2/favicons?sz=64&domain=regenbogen-ice.de
// @grant none
// ==/UserScript==
(function() {
'use strict';
const REGENBOGEN_ICE_ROOT = 'https://dev.regenbogen-ice.de'
function findReactFiber(node) {
for(const key of Object.keys(node)) {
if(key.startsWith('__reactFiber$')) {
return node[key]
}
}
return null
}
function injectIntoCoachSequence(node) {
const coachContainer = node.childNodes[1]
const coachFiber = findReactFiber(coachContainer)
const groups = coachFiber.memoizedProps.children.map(child => child.props.gruppe)
const regenbogenIceLinks = []
for(const group of groups) {
let regenbogenIceLink = null
if(group.name.startsWith('ICE')) {
regenbogenIceLink = `${REGENBOGEN_ICE_ROOT}/vehicle/ICE/${Number(group.name.substring(3))}`
} else if(group.name.startsWith('ICK')) {
regenbogenIceLink = `${REGENBOGEN_ICE_ROOT}/vehicle/IC/${Number(group.name.substring(3))}`
}
regenbogenIceLinks.push(regenbogenIceLink)
}
if(regenbogenIceLinks.every(e => e)) {
const nameClass = coachContainer?.lastChild?.className
const groupLabels = Array.from(coachContainer.childNodes).filter(elem => elem.className === nameClass)
if(groupLabels.length === regenbogenIceLinks.length) {
for(const [index, link] of Object.entries(regenbogenIceLinks)) {
const linkNode = document.createElement('a')
linkNode.href = link
linkNode.textContent = 'regenbogen-ice.de'
linkNode.target = '_blank'
linkNode.addEventListener('click', e => e.stopPropagation(), {capture: true, passive: false})
groupLabels[index].appendChild(linkNode)
}
}
}
}
function isCoachSequence(node) {
if(node?.tagName === 'DIV' &&
node?.getAttribute('style')?.startsWith('height: ') &&
node.childNodes?.[2]?.textContent === 'Legende') {
return true
}
return false
}
function processNode(node) {
if(node.nodeType !== 3) {
if(isCoachSequence(node)) {
injectIntoCoachSequence(node)
}
for(const child of node.childNodes) {
processNode(child)
}
}
}
const observer = new MutationObserver(changes => {
for(const change of changes) {
for(const node of change.addedNodes) {
processNode(node)
}
}
})
observer.observe(document.body, {childList: true, subtree: true})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment