51 lines
2.3 KiB
JavaScript
51 lines
2.3 KiB
JavaScript
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';
|
||
}
|
||
}); |