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);
}
});
});

View File

@@ -0,0 +1 @@
document.addEventListener("DOMContentLoaded",(async()=>{const t=document.getElementById("bookstackLink");if(!t)return;const e=window.location.pathname.split("/").filter(Boolean)[0]||"DefaultTag",o=`bookstack_article_${e}`,a=e=>{t.style.display="block",t.querySelector("a").onclick=t=>{t.preventDefault();const o=document.createElement("div");o.className="bookstack-integration-modal",o.innerHTML=`<div class="bookstack-integration-modal-content"><button class="bookstack-integration-close-btn">×</button><iframe src="${e}?iframe=true" class="bookstack-integration-iframe"></iframe></div>`,o.onclick=t=>{(t.target===o||t.target.classList.contains("bookstack-integration-close-btn"))&&o.remove()},document.body.appendChild(o)}};document.addEventListener("keydown",(t=>{t.ctrlKey&&"F8"===t.key&&(t.preventDefault(),localStorage.removeItem(o),window.notify("success",`📗 BookStack cache für '${e}' wurde gelöscht.`))}));try{const n=JSON.parse(localStorage.getItem(o)||"null");if(n&&Date.now()-n.timestamp<(n.url?6048e5:2592e5))return void(n.url&&a(n.url));const c=await fetch(`https://bookstack.xinon.at/api/search?query=%5Bshowurl%3D${encodeURIComponent(e)}%5D%7Btype%3Apage%7D`,{headers:{Authorization:"Token XmGSDWlg3bZhHKXFchNXQ9LpXvCaBuM1:k6XNe6RUU1BIxkv5pxpZ9PSErqZbHJ4i"}}),r=await c.json(),s=r.data?.[0]?.url||null;localStorage.setItem(o,JSON.stringify({url:s,timestamp:Date.now()})),s?a(s):t.style.display="none"}catch(e){console.error("BookStack API error:",e),t.style.display="none"}}));