181 lines
5.2 KiB
JavaScript
181 lines
5.2 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 Promise.reject(new Error('missing page id'));
|
|
|
|
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]));
|
|
});
|
|
}
|
|
|
|
return 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;
|
|
});
|
|
});
|
|
}
|
|
|
|
function ensurePageLink() {
|
|
return requestPageLink('ensure', { sectok: getSectok() });
|
|
}
|
|
|
|
function unlinkPageLink() {
|
|
return requestPageLink('unlink', { sectok: getSectok() });
|
|
}
|
|
|
|
function triggerDownload(pageId) {
|
|
try {
|
|
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
|
|
var href = endpoint + '?cmd=download&id=' + encodeURIComponent(pageId);
|
|
|
|
var a = document.createElement('a');
|
|
a.href = href;
|
|
a.download = '.pagelink';
|
|
a.style.display = 'none';
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
document.body.removeChild(a);
|
|
} catch (e) {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
function fetchPageLinkInfo(pageId) {
|
|
if (!pageId) return Promise.reject(new Error('missing page id'));
|
|
|
|
var endpoint = getBaseUrl() + 'lib/plugins/luxtools/pagelink.php';
|
|
var query = endpoint + '?cmd=info&id=' + encodeURIComponent(pageId);
|
|
|
|
return 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;
|
|
});
|
|
});
|
|
}
|
|
|
|
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;
|
|
|
|
fetchPageLinkInfo(pageId).then(function (info) {
|
|
var link = document.createElement('a');
|
|
link.href = '#';
|
|
|
|
if (!info || !info.uuid) {
|
|
link.textContent = 'Link Page';
|
|
link.addEventListener('click', function (e) {
|
|
e.preventDefault();
|
|
ensurePageLink().then(function (res) {
|
|
if (!res || !res.uuid) throw new Error('no uuid');
|
|
triggerDownload(pageId);
|
|
}).catch(function (err) {
|
|
if (window.console && window.console.warn) {
|
|
window.console.warn('PageLink ensure failed:', err);
|
|
}
|
|
});
|
|
});
|
|
} else if (info.linked) {
|
|
link.textContent = 'Unlink Page';
|
|
link.addEventListener('click', function (e) {
|
|
e.preventDefault();
|
|
if (!window.confirm('Unlink page?')) return;
|
|
unlinkPageLink().then(function () {
|
|
window.setTimeout(function () {
|
|
try { window.location.reload(); } catch (e2) {}
|
|
}, 400);
|
|
}).catch(function (err) {
|
|
if (window.console && window.console.warn) {
|
|
window.console.warn('PageLink unlink failed:', err);
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
link.textContent = 'Download Link File';
|
|
link.addEventListener('click', function (e) {
|
|
e.preventDefault();
|
|
triggerDownload(pageId);
|
|
});
|
|
}
|
|
|
|
var first = container.firstChild;
|
|
container.insertBefore(link, first);
|
|
if (first) {
|
|
container.insertBefore(document.createTextNode(' · '), first);
|
|
}
|
|
}).catch(function () {
|
|
// ignore failures
|
|
});
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
attachDocInfoLink();
|
|
}, false);
|
|
})();
|