183 lines
5.1 KiB
JavaScript
183 lines
5.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 fetchInfo() {
|
|
return requestPageLink('info', {});
|
|
}
|
|
|
|
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 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 () {
|
|
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', attachStatus, false);
|
|
})();
|