Files
luxtools-plugin/js/page-link.js

212 lines
6.1 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 requestPageLink(cmd, params) {
var pageId = getPageId();
if (!pageId) return false;
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
var payload = new window.URLSearchParams();
payload.set('cmd', cmd);
payload.set('id', pageId);
if (params && typeof params === 'object') {
Object.keys(params).forEach(function (key) {
payload.set(key, String(params[key]));
});
}
window.fetch(endpoint, {
method: 'POST',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
body: payload.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 request failed:', err);
}
});
return false;
}
function ensurePageLink() {
return requestPageLink('ensure', { sectok: getSectok() });
}
function unlinkPageLink() {
return requestPageLink('unlink', { sectok: getSectok() });
}
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 attachCopyTargets() {
var targets = document.querySelectorAll('[data-luxtools-pagelink-copy="1"]');
if (!targets || !targets.length) return;
targets.forEach(function (el) {
if (!el || !el.getAttribute) return;
el.setAttribute('role', 'button');
el.setAttribute('tabindex', '0');
el.addEventListener('click', function (e) {
e.preventDefault();
copyToClipboard(String(el.getAttribute('data-uuid') || '').trim());
});
el.addEventListener('keydown', function (e) {
if (!e || (e.key !== 'Enter' && e.key !== ' ')) return;
e.preventDefault();
copyToClipboard(String(el.getAttribute('data-uuid') || '').trim());
});
});
}
function attachDocInfoLink() {
var container = document.querySelector('.docInfo');
if (!container || !container.getAttribute) return;
if (container.getAttribute('data-luxtools-pagelink-docinfo') === '1') return;
container.setAttribute('data-luxtools-pagelink-docinfo', '1');
var pageId = getPageId();
if (!pageId) return;
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
var query = endpoint + '?cmd=info&id=' + encodeURIComponent(pageId);
window.fetch(query, {
method: 'GET',
credentials: 'same-origin'
}).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;
});
}).then(function (info) {
if (!info || !info.uuid) return;
var link = document.createElement('a');
link.href = endpoint + '?cmd=download&id=' + encodeURIComponent(pageId);
link.textContent = 'Page ID: ' + String(info.uuid);
var first = container.firstChild;
container.insertBefore(link, first);
if (first) {
container.insertBefore(document.createTextNode(' · '), first);
}
}).catch(function () {
// ignore failures
});
}
window.addBtnActionLuxtoolsPageLink = function ($btn, props, edid) {
$btn.on('click', function () {
var pageId = getPageId();
if (!pageId) return false;
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
var query = endpoint + '?cmd=info&id=' + encodeURIComponent(pageId);
window.fetch(query, {
method: 'GET',
credentials: 'same-origin'
}).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;
});
}).then(function (info) {
if (info && info.uuid) {
if (window.confirm('Unlink page?')) {
unlinkPageLink();
}
return;
}
ensurePageLink();
}).catch(function (err) {
if (window.console && window.console.warn) {
window.console.warn('PageLink info failed:', err);
}
ensurePageLink();
});
return false;
});
return 'luxtools-pagelink';
};
document.addEventListener('DOMContentLoaded', function () {
attachCopyTargets();
attachDocInfoLink();
}, false);
})();