added v2 of cpeprov

This commit is contained in:
Luca Haid
2025-07-21 22:27:40 +02:00
parent 14757f4de0
commit f7218ab144
13 changed files with 786 additions and 57 deletions

View File

@@ -1,50 +1,51 @@
document.addEventListener('DOMContentLoaded', async () => {
const articleTag = (() => {
const path = window.location.pathname;
const segments = path.split('/').filter(Boolean);
return segments.length > 0 ? segments[0] : 'DefaultTag';
})();
const linkEl = document.getElementById('bookstackLink');
if (!linkEl) return;
const articleTag = window.location.pathname.split('/').filter(Boolean)[0] || 'DefaultTag';
const cacheKey = `bookstack_article_${articleTag}`;
const apiUrl = `https://bookstack.xinon.at/api/search?query=%5Bshowurl%3D${encodeURIComponent(articleTag)}%5D%7Btype%3Apage%7D`;
const linkElement = document.getElementById('bookstackLink');
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 response = await fetch(apiUrl, {
headers: {
'Authorization': 'Token XmGSDWlg3bZhHKXFchNXQ9LpXvCaBuM1:k6XNe6RUU1BIxkv5pxpZ9PSErqZbHJ4i'
}
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;
if (data.data && data.data.length > 0) {
const article = data.data[0];
linkElement.style.display = 'block';
linkElement.querySelector('a').addEventListener('click', (e) => {
e.preventDefault();
showArticleModal(article.url);
});
}
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);
linkElement.style.display = 'none';
linkEl.style.display = 'none';
}
function showArticleModal(url) {
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.querySelector('.bookstack-integration-close-btn').addEventListener('click', () => modal.remove());
modal.addEventListener('click', (e) => {
if (e.target === modal) modal.remove();
});
document.body.appendChild(modal);
}
});
});