Warten bis Frame fertig Funktion; Refacoring
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// ==UserScript==
|
||||
// @name academyFIVE::AddSelectBox
|
||||
// @namespace dakp/academyfive
|
||||
// @version 2026.04.003
|
||||
// @version 2026.04.005
|
||||
// @description Füge Selectbox für Kohorten/Planungsgruppen hinzu
|
||||
// @author Dims Akpan
|
||||
// @match https://a5.fhdw-hannover.de/*
|
||||
@@ -15,25 +15,10 @@
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
function getDropdownSelector() {
|
||||
const url = window.location.href;
|
||||
if (url.includes('/course/planning-group/list')) {
|
||||
return 'select[name="cohort-ids[]"]';
|
||||
}
|
||||
if (url.includes('/course/list')) {
|
||||
return 'select[name="planningGroupIds[]"]';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
// ─── Gemeinsame Hilfsfunktionen ───────────────────────────────────────────
|
||||
|
||||
function tryInsertButtons() {
|
||||
const selector = getDropdownSelector();
|
||||
if (!selector) return false;
|
||||
|
||||
// Bootstrap Select wraps the <select> in .btn-group.bootstrap-select —
|
||||
// suche die searchbox direkt über den select-Namen um sicherzustellen,
|
||||
// dass Bootstrap Select bereits initialisiert hat
|
||||
const select = document.querySelector(selector);
|
||||
function insertSelectButtons(selectSelector) {
|
||||
const select = document.querySelector(selectSelector);
|
||||
if (!select) return false;
|
||||
|
||||
const dropdownContainer = select.closest('.btn-group.bootstrap-select');
|
||||
@@ -56,29 +41,66 @@
|
||||
`;
|
||||
|
||||
searchBox.insertAdjacentElement('afterend', actionsBox);
|
||||
console.log('Select/Deselect Buttons erfolgreich eingefügt');
|
||||
console.log('Select/Deselect Buttons eingefügt für:', selectSelector);
|
||||
return true;
|
||||
}
|
||||
|
||||
function waitForElement() {
|
||||
if (tryInsertButtons()) return;
|
||||
function waitAndInsert(tryFn) {
|
||||
if (tryFn()) return;
|
||||
|
||||
// Bootstrap Select läuft nach DOMContentLoaded — beobachte DOM-Mutationen
|
||||
const observer = new MutationObserver(() => {
|
||||
if (tryInsertButtons()) {
|
||||
observer.disconnect();
|
||||
}
|
||||
if (tryFn()) observer.disconnect();
|
||||
});
|
||||
|
||||
observer.observe(document.body, { childList: true, subtree: true });
|
||||
|
||||
// Sicherheitsnetz: nach 10 s aufgeben
|
||||
setTimeout(() => observer.disconnect(), 10000);
|
||||
// Polling für initialen Seitenaufruf (Bootstrap Select initialisiert sich
|
||||
// asynchron nach DOMContentLoaded)
|
||||
let attempts = 0;
|
||||
const poll = setInterval(() => {
|
||||
attempts++;
|
||||
if (tryFn() || attempts >= 50) {
|
||||
clearInterval(poll);
|
||||
if (attempts >= 50) observer.disconnect();
|
||||
}
|
||||
}, 100);
|
||||
}
|
||||
|
||||
// ─── /course/list ─────────────────────────────────────────────────────────
|
||||
|
||||
function courseList_tryInsert() {
|
||||
return insertSelectButtons('select[name="planningGroupIds[]"]');
|
||||
}
|
||||
|
||||
function courseList_init() {
|
||||
waitAndInsert(courseList_tryInsert);
|
||||
}
|
||||
|
||||
// ─── /course/planning-group/list ──────────────────────────────────────────
|
||||
|
||||
function planningGroupList_tryInsert() {
|
||||
return insertSelectButtons('select[name="cohort-ids[]"]');
|
||||
}
|
||||
|
||||
function planningGroupList_init() {
|
||||
waitAndInsert(planningGroupList_tryInsert);
|
||||
}
|
||||
|
||||
// ─── Routing ──────────────────────────────────────────────────────────────
|
||||
|
||||
function init() {
|
||||
const url = window.location.href;
|
||||
|
||||
if (url.includes('/course/planning-group/list')) {
|
||||
planningGroupList_init();
|
||||
} else if (url.includes('/course/list')) {
|
||||
courseList_init();
|
||||
}
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', waitForElement);
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
} else {
|
||||
waitForElement();
|
||||
init();
|
||||
}
|
||||
})();
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user