Files
thetool/public/plugins/bookstack/bookstackIntegration.js
2025-07-21 22:27:40 +02:00

51 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
document.addEventListener('DOMContentLoaded', async () => {
const linkEl = document.getElementById('bookstackLink');
if (!linkEl) return;
const articleTag = window.location.pathname.split('/').filter(Boolean)[0] || 'DefaultTag';
const cacheKey = `bookstack_article_${articleTag}`;
const setupLinkAction = url => {
linkEl.style.display = 'block';
linkEl.querySelector('a').onclick = e => {
e.preventDefault();
const modal = document.createElement('div');
modal.className = 'bookstack-integration-modal';
modal.innerHTML = `<div class="bookstack-integration-modal-content"><button class="bookstack-integration-close-btn">×</button><iframe src="${url}?iframe=true" class="bookstack-integration-iframe"></iframe></div>`;
modal.onclick = ev => {
if (ev.target === modal || ev.target.classList.contains('bookstack-integration-close-btn')) {
modal.remove();
}
};
document.body.appendChild(modal);
};
};
document.addEventListener('keydown', e => {
if (e.ctrlKey && e.key === 'F8') {
e.preventDefault();
localStorage.removeItem(cacheKey);
window.notify('success', `📗 BookStack cache für '${articleTag}' wurde gelöscht.`);
}
});
try {
const cachedItem = JSON.parse(localStorage.getItem(cacheKey) || 'null');
if (cachedItem && (Date.now() - cachedItem.timestamp < (cachedItem.url ? 604800000 : 259200000))) {
if (cachedItem.url) setupLinkAction(cachedItem.url);
return;
}
const response = await fetch(`https://bookstack.xinon.at/api/search?query=%5Bshowurl%3D${encodeURIComponent(articleTag)}%5D%7Btype%3Apage%7D`, {
headers: { 'Authorization': 'Token XmGSDWlg3bZhHKXFchNXQ9LpXvCaBuM1:k6XNe6RUU1BIxkv5pxpZ9PSErqZbHJ4i' }
});
const data = await response.json();
const articleUrl = data.data?.[0]?.url || null;
localStorage.setItem(cacheKey, JSON.stringify({ url: articleUrl, timestamp: Date.now() }));
articleUrl ? setupLinkAction(articleUrl) : (linkEl.style.display = 'none');
} catch (error) {
console.error('BookStack API error:', error);
linkEl.style.display = 'none';
}
});