Changes for page Linkding + SingleFile Archief
Last modified by XWikiGuest on 2026/03/11 21:04
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -78,7 +78,6 @@ 78 78 </style> 79 79 80 80 <div id="ldsf-wrapper"> 81 - <button id="ldsf-test-btn" style="background:#df691a;color:#fff;padding:8px 16px;border:none;border-radius:4px;cursor:pointer;margin-bottom:10px;">TEST KLIK</button> 82 82 <div id="ldsf-status">Laden...</div> 83 83 <div id="ldsf-bookmarks"></div> 84 84 <div id="ldsf-viewer-container"> ... ... @@ -91,104 +91,120 @@ 91 91 </div> 92 92 93 93 <script> 94 -var LDSF_LINKDING_URL = 'https://bookmarks.rhebergen.org/api/bookmarks/'; 95 -var LDSF_TOKEN = '3b7443e0f84e2b2b269adebb96d7475e4a5e653e'; 96 -var LDSF_TAG = 'Nuclear-&-Energy'; 97 -var LDSF_COUNT = 20; 98 -var LDSF_SHARE = 'eT2X9ttBHK5GoEY'; 99 -var LDSF_WEBDAV = 'https://cloud.rhebergen.org/public.php/webdav/'; 100 -var LDSF_AUTH = 'Basic ' + btoa(LDSF_SHARE + ':'); 101 -var ldsf_urlToFile = {}; 102 -var ldsf_archiveMap = {}; 93 +(function() { 94 + var LINKDING_URL = 'https://bookmarks.rhebergen.org/api/bookmarks/'; 95 + var LINKDING_TOKEN = '3b7443e0f84e2b2b269adebb96d7475e4a5e653e'; 96 + var LINKDING_TAG = 'Nuclear-&-Energy'; 97 + var LINKDING_COUNT = 20; 103 103 104 -function ldsfFindArchive(url) { 105 - var v = [url, url.replace(/\/$/, ''), url + '/', url.replace(/\?.*$/, '')]; 106 - for (var i = 0; i < v.length; i++) { if (ldsf_urlToFile[v[i]]) return ldsf_urlToFile[v[i]]; } 107 - return null; 108 -} 99 + var SHARE_TOKEN = 'eT2X9ttBHK5GoEY'; 100 + var WEBDAV_BASE = 'https://cloud.rhebergen.org/public.php/webdav/'; 101 + var INDEX_URL = WEBDAV_BASE + 'index.json'; 102 + var NC_AUTH = 'Basic ' + btoa(SHARE_TOKEN + ':'); 109 109 110 -function ldsfOpen(idx) { 111 - var f = ldsf_archiveMap[idx]; 112 - if (f) ldsfShowArchive(f); 113 -} 104 + var urlToFile = {}; // reverse map: URL -> filename 105 + var archiveMap = {}; // button index -> filename 114 114 115 -function ldsfShowArchive(filename) { 116 - document.getElementById('ldsf-viewer-title').textContent = filename.replace(/\.html?$/i, ''); 117 - document.getElementById('ldsf-viewer-container').style.display = 'block'; 118 - var frame = document.getElementById('ldsf-viewer-frame'); 119 - frame.srcdoc = '<p style="padding:20px;color:#666">Laden...</p>'; 120 - fetch(LDSF_WEBDAV + encodeURIComponent(filename), { headers: { 'Authorization': LDSF_AUTH } }) 121 - .then(function(r) { return r.text(); }) 122 - .then(function(html) { frame.srcdoc = html; }) 123 - .catch(function(err) { frame.srcdoc = '<p style="padding:20px;color:red">Fout: ' + err.message + '</p>'; }); 124 -} 107 + function findArchive(url) { 108 + // Try exact match, with/without trailing slash, and without query string 109 + var variants = [url, url.replace(/\/$/, ''), url + '/', url.replace(/\?.*$/, '')]; 110 + for (var i = 0; i < variants.length; i++) { 111 + if (urlToFile[variants[i]]) return urlToFile[variants[i]]; 112 + } 113 + return null; 114 + } 125 125 126 -function ldsfCloseViewer() { 127 - document.getElementById('ldsf-viewer-container').style.display = 'none'; 128 -} 129 - 130 -function ldsfLoadBookmarks() { 131 - var apiUrl = LDSF_LINKDING_URL + '?limit=' + LDSF_COUNT + '&q=%23' + encodeURIComponent(LDSF_TAG); 132 - console.log('Fetching bookmarks:', apiUrl); 133 - fetch(apiUrl, { headers: { 'Authorization': 'Token ' + LDSF_TOKEN } }) 134 - .then(function(r) { 135 - console.log('Linkding response status:', r.status, r.headers.get('content-type')); 136 - if (!r.ok) return r.text().then(function(t) { throw new Error('HTTP ' + r.status + ': ' + t.substring(0, 200)); }); 137 - return r.json(); 116 + // Load SingleFile index, then load bookmarks 117 + fetch(INDEX_URL, { headers: { 'Authorization': NC_AUTH } }) 118 + .then(function(r) { return r.ok ? r.json() : {}; }) 119 + .then(function(index) { 120 + // Build reverse map: URL -> filename 121 + Object.keys(index).forEach(function(fname) { 122 + urlToFile[index[fname]] = fname; 123 + }); 124 + loadBookmarks(); 138 138 }) 139 - .then(function(data) { 140 - var total = data.count || 0; 141 - var archiveCount = 0; 142 - var container = document.getElementById('ldsf-bookmarks'); 143 - var ul = document.createElement('ul'); 126 + .catch(function() { 127 + loadBookmarks(); // Continue without index 128 + }); 144 144 145 - (data.results || []).forEach(function(bm) { 146 - var title = bm.title || bm.website_title || bm.url; 147 - var fname = ldsfFindArchive(bm.url); 148 - if (fname) archiveCount++; 130 + function loadBookmarks() { 131 + var apiUrl = LINKDING_URL + '?limit=' + LINKDING_COUNT + '&q=%23' + encodeURIComponent(LINKDING_TAG); 132 + fetch(apiUrl, { headers: { 'Authorization': 'Token ' + LINKDING_TOKEN } }) 133 + .then(function(r) { return r.json(); }) 134 + .then(function(data) { 135 + var total = data.count || 0; 136 + var archiveCount = 0; 137 + var container = document.getElementById('ldsf-bookmarks'); 138 + var html = '<ul>'; 149 149 150 - var li = document.createElement('li'); 140 + (data.results || []).forEach(function(bm, idx) { 141 + var title = bm.title || bm.website_title || bm.url; 142 + var fname = findArchive(bm.url); 143 + if (fname) archiveCount++; 151 151 152 - if (fname) { 153 - var btn = document.createElement('button'); 154 - btn.className = 'sf-btn'; 155 - btn.title = 'Archief bekijken'; 156 - btn.textContent = '\uD83D\uDCC4'; 157 - btn.addEventListener('click', (function(f) { 158 - return function() { ldsfShowArchive(f); }; 159 - })(fname)); 160 - li.appendChild(btn); 161 - } 145 + html += '<li>'; 146 + if (fname) { 147 + html += '<button class="sf-btn" data-idx="' + idx + '" title="Archief bekijken">📄</button>'; 148 + archiveMap[idx] = fname; 149 + } 150 + html += '<a href="' + bm.url + '" target="_blank">' + title + '</a>'; 151 + html += '</li>'; 152 + }); 162 162 163 - var a = document.createElement('a'); 164 - a.href = bm.url; 165 - a.target = '_blank'; 166 - a.textContent = title; 167 - li.appendChild(a); 168 - ul.appendChild(li); 154 + html += '</ul>'; 155 + container.innerHTML = html; 156 + document.getElementById('ldsf-status').textContent = total + ' bookmarks, ' + archiveCount + ' met archief'; 157 + 158 + // Bind click handlers directly to buttons 159 + var buttons = container.querySelectorAll('.sf-btn'); 160 + for (var b = 0; b < buttons.length; b++) { 161 + (function(btn) { 162 + var idx = btn.getAttribute('data-idx'); 163 + btn.addEventListener('click', function(ev) { 164 + ev.preventDefault(); 165 + ev.stopPropagation(); 166 + if (archiveMap[idx]) { 167 + ldsfShowArchive(archiveMap[idx], btn); 168 + } 169 + }); 170 + })(buttons[b]); 171 + } 172 + }) 173 + .catch(function(err) { 174 + document.getElementById('ldsf-status').textContent = 'Fout: ' + err.message; 169 169 }); 176 + } 170 170 171 - container.innerHTML = ''; 172 - container.appendChild(ul); 173 - document.getElementById('ldsf-status').textContent = total + ' bookmarks, ' + archiveCount + ' met archief'; 174 - }) 175 - .catch(function(err) { document.getElementById('ldsf-status').textContent = 'Fout: ' + err.message; }); 176 -} 178 + // Show archive in viewer panel 179 + window.ldsfShowArchive = function(filename, btn) { 180 + var container = document.getElementById('ldsf-viewer-container'); 181 + var frame = document.getElementById('ldsf-viewer-frame'); 182 + var titleEl = document.getElementById('ldsf-viewer-title'); 177 177 178 -// Debug test button 179 -document.getElementById('ldsf-test-btn').addEventListener('click', function() { 180 - alert('archiveMap: ' + JSON.stringify(ldsf_archiveMap) + '\nurlToFile: ' + JSON.stringify(ldsf_urlToFile)); 181 -}); 184 + // Toggle active button 185 + document.querySelectorAll('#ldsf-bookmarks .sf-btn').forEach(function(b) { b.classList.remove('active'); }); 186 + btn.classList.add('active'); 182 182 183 -// Load index then bookmarks 184 -fetch(LDSF_WEBDAV + 'index.json', { headers: { 'Authorization': LDSF_AUTH } }) 185 -.then(function(r) { return r.ok ? r.json() : {}; }) 186 -.then(function(idx) { 187 - console.log('SingleFile index loaded:', idx); 188 - Object.keys(idx).forEach(function(f) { ldsf_urlToFile[idx[f]] = f; }); 189 - ldsfLoadBookmarks(); 190 -}) 191 -.catch(function(err) { console.log('SingleFile index FAILED:', err); ldsfLoadBookmarks(); }); 188 + titleEl.textContent = filename.replace(/\.html?$/i, ''); 189 + container.style.display = 'block'; 190 + frame.srcdoc = '<p style="padding:20px;color:#666">Laden...</p>'; 191 + 192 + fetch(WEBDAV_BASE + encodeURIComponent(filename), { headers: { 'Authorization': NC_AUTH } }) 193 + .then(function(r) { return r.text(); }) 194 + .then(function(html) { 195 + frame.srcdoc = html; 196 + }) 197 + .catch(function(err) { 198 + frame.srcdoc = '<p style="padding:20px;color:red">Fout: ' + err.message + '</p>'; 199 + }); 200 + }; 201 + 202 + window.ldsfCloseViewer = function() { 203 + document.getElementById('ldsf-viewer-container').style.display = 'none'; 204 + document.querySelectorAll('#ldsf-bookmarks .sf-btn').forEach(function(b) { b.classList.remove('active'); }); 205 + }; 206 +})(); 192 192 </script> 193 193 {{/html}} 194 194