Files
luxtools-plugin/js/page-link.js
2026-02-02 21:13:51 +01:00

137 lines
3.8 KiB
JavaScript

/* global window, document */
(function () {
'use strict';
function getSectok() {
try {
if (window.JSINFO && window.JSINFO.sectok) return String(window.JSINFO.sectok);
} catch (e) {}
try {
var inp = document.querySelector('input[name="sectok"], input[name="securitytoken"]');
if (inp && inp.value) return String(inp.value);
} catch (e2) {}
return '';
}
function getPageId() {
try {
if (window.JSINFO && window.JSINFO.id) return String(window.JSINFO.id);
} catch (e) {}
try {
var input = document.querySelector('input[name="id"]');
if (input && input.value) return String(input.value);
} catch (e2) {}
return '';
}
function getBaseUrl() {
try {
if (window.DOKU_BASE) return String(window.DOKU_BASE);
} catch (e) {}
try {
if (window.JSINFO && window.JSINFO.base) return String(window.JSINFO.base);
} catch (e2) {}
return '/';
}
function ensurePageLink() {
var pageId = getPageId();
if (!pageId) return false;
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
var params = new window.URLSearchParams();
params.set('cmd', 'ensure');
params.set('id', pageId);
params.set('sectok', getSectok());
window.fetch(endpoint, {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: params.toString()
}).then(function (res) {
return res.json().catch(function () { return null; }).then(function (body) {
if (!res.ok || !body || body.ok !== true) {
throw new Error('request failed');
}
return body;
});
}).catch(function (err) {
if (window.console && window.console.warn) {
window.console.warn('PageLink creation failed:', err);
}
});
return false;
}
function copyToClipboard(text) {
if (!text) return;
if (window.navigator && window.navigator.clipboard && window.navigator.clipboard.writeText) {
window.navigator.clipboard.writeText(text).catch(function () {});
return;
}
try {
var textarea = document.createElement('textarea');
textarea.value = text;
textarea.setAttribute('readonly', 'readonly');
textarea.style.position = 'absolute';
textarea.style.left = '-9999px';
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
} catch (e) {}
document.body.removeChild(textarea);
} catch (e2) {}
}
function attachStatus() {
var status = document.querySelector('.luxtools-pagelink-status[data-luxtools-pagelink="1"]');
if (!status) return;
var pageTitle = document.querySelector('#dokuwiki__content h1')
|| document.querySelector('.pageId')
|| document.querySelector('h1');
if (pageTitle && pageTitle.appendChild) {
status.classList.add('is-inline');
pageTitle.appendChild(status);
}
var copy = String(status.getAttribute('data-copy') || '') === '1';
if (copy) {
status.setAttribute('role', 'button');
status.setAttribute('tabindex', '0');
status.addEventListener('click', function (e) {
e.preventDefault();
copyToClipboard(String(status.getAttribute('data-uuid') || '').trim());
});
status.addEventListener('keydown', function (e) {
if (!e || (e.key !== 'Enter' && e.key !== ' ')) return;
e.preventDefault();
copyToClipboard(String(status.getAttribute('data-uuid') || '').trim());
});
}
}
window.addBtnActionLuxtoolsPageLink = function ($btn, props, edid) {
$btn.on('click', function () {
ensurePageLink();
return false;
});
return 'luxtools-pagelink';
};
document.addEventListener('DOMContentLoaded', attachStatus, false);
})();