musdb-tampermonkey-scripts/vocabulary-editing/TransferEvent.user.js
2023-03-24 15:33:27 +01:00

115 lines
3.7 KiB
JavaScript

// ==UserScript==
// @name TransferEvent
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Transfer events to object description
// @author Nathan Eikermann
// @match https://sandkasten.museum-digital.de/musdb/objekt_cha.php*
// @icon https://www.google.com/s2/favicons?sz=64&domain=museum-digital.de
// @grant none
// ==/UserScript==
(function() {
'use strict';
const objectId = document.getElementById("objCounter").textContent;
const eventModuleOld = document.getElementById("eventModuleOld");
function identifyEventId(href) {
console.log("Parsing event ID from " + href)
const urlParams = new URLSearchParams(href.substring(href.indexOf('?')));
console.log(urlParams)
return urlParams.get('ernum');
}
function createButton(title) {
const button = document.createElement('span');
button.textContent = title;
button.style.background = "#000";
button.style.color = "#FFF";
button.style.cursor = "pointer";
button.style.margin = "0 20px"
return button;
}
function createClickFunction(newObjectText, eventId, eventTypeId) {
return function() {
let updated = 0;
window.fetch('/musdb/objekt_update.php', {
method: 'POST', cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: "objektnum=" + encodeURIComponent(objectId) + '&objekt_beschreibung=' + encodeURIComponent(document.getElementById('objekt_beschreibung').textContent + "\n\n" + newObjectText)
}).then(function(response) {
updated++;
if (updated === 2) location.reload();
}).catch((error) => {
console.log(error);
});
window.fetch('/musdb/api_int/object/update_event/' + objectId, {
method: 'POST', cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: eventTypeId + '=0&event_id=' + encodeURIComponent(eventId)
}).then(function(response) {
updated++;
if (updated === 2) location.reload();
}).catch((error) => {
console.log(error);
});
};
}
for (let child of eventModuleOld.children) {
// Get update link to identify event ID
const updateLink = child.getElementsByClassName("iconsEdit")[0];
if (updateLink === undefined) continue;
// Identify event ID
const eventId = identifyEventId(updateLink.href);
console.log(eventId);
const actorLinks = child.getElementsByClassName('actorToolTip');
if (actorLinks.length !== 0) {
const actorLink = actorLinks[0];
const button = createButton('Transfer')
actorLink.parentElement.appendChild(button);
button.addEventListener('click', createClickFunction(actorLink.textContent, eventId, 'persinst_id'), {once: true});
}
const placeLinks = child.getElementsByClassName('placeToolTip');
if (actorLinks.length !== 0) {
const placeLink = placeLinks[0];
const button = createButton('Transfer')
placeLink.parentElement.appendChild(button);
button.addEventListener('click', createClickFunction(placeLink.textContent, eventId, 'place_id'), {once: true});
}
// Offer manipulating actor
}
})();