2023-09-25 16:45:49 -05:00
|
|
|
if (document.readyState && document.readyState !== 'loading') {
|
|
|
|
configureSummarizeButtons();
|
|
|
|
} else {
|
|
|
|
document.addEventListener('DOMContentLoaded', configureSummarizeButtons, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function configureSummarizeButtons() {
|
|
|
|
document.getElementById('global').addEventListener('click', function(e) {
|
|
|
|
for (var target = e.target; target && target != this; target = target.parentNode) {
|
|
|
|
if (target.matches('.kagi-summary a.btn')) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
if (target.href) {
|
|
|
|
summarizeButtonClick(target);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
2023-09-28 11:02:25 -05:00
|
|
|
function setKagiState(container, statusType, statusMsg, summaryText) {
|
|
|
|
var kstatus = container.querySelector('.kagi-status');
|
|
|
|
var content = container.querySelector('.kagi-content');
|
2024-09-25 14:24:23 -05:00
|
|
|
content.style.cssText = content.dataset.css;
|
2023-09-28 11:02:25 -05:00
|
|
|
|
|
|
|
switch(statusType) {
|
|
|
|
case 0:
|
|
|
|
container.classList.remove('kagi-loading');
|
|
|
|
kstatus.classList.remove('alert-warn');
|
|
|
|
kstatus.classList.remove('alert-error');
|
|
|
|
kstatus.classList.add('hidden');
|
|
|
|
kstatus.innerHTML = '';
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
container.classList.add('kagi-loading');
|
|
|
|
kstatus.classList.remove('alert-error');
|
|
|
|
kstatus.classList.add('alert-warn');
|
|
|
|
kstatus.innerHTML = statusMsg;
|
|
|
|
kstatus.classList.remove('hidden');
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
container.classList.remove('kagi-loading');
|
|
|
|
kstatus.classList.remove('alert-warn');
|
|
|
|
kstatus.classList.add('alert-error');
|
|
|
|
kstatus.innerHTML = statusMsg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (summaryText) {
|
|
|
|
content.innerHTML = summaryText.replace(/(?:\r\n|\r|\n)/g, '<br>');
|
|
|
|
content.classList.remove('hidden');
|
|
|
|
} else {
|
|
|
|
content.classList.add('hidden');
|
|
|
|
content.innerHTML = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 16:45:49 -05:00
|
|
|
function summarizeButtonClick(button) {
|
|
|
|
var container = button.parentNode;
|
2023-09-28 11:02:25 -05:00
|
|
|
if (container.classList.contains('kagi-loading')) {
|
|
|
|
return;
|
|
|
|
}
|
2023-09-25 16:45:49 -05:00
|
|
|
|
2023-09-28 11:02:25 -05:00
|
|
|
setKagiState(container, 1, kagi_strings.loading_summary, null);
|
2023-09-25 16:45:49 -05:00
|
|
|
|
2023-09-28 11:02:25 -05:00
|
|
|
var url = button.href;
|
2023-09-25 16:45:49 -05:00
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open('POST', url, true);
|
|
|
|
request.responseType = 'json';
|
|
|
|
|
|
|
|
request.onload = function(e) {
|
|
|
|
if (this.status != 200) {
|
|
|
|
return request.onerror(e);
|
|
|
|
}
|
|
|
|
|
2023-09-28 11:02:25 -05:00
|
|
|
var xresp = xmlHttpRequestJson(this);
|
|
|
|
if (!xresp) {
|
2023-09-25 16:45:49 -05:00
|
|
|
return request.onerror(e);
|
|
|
|
}
|
|
|
|
|
2023-09-28 11:02:25 -05:00
|
|
|
if (xresp.status !== 200 || !xresp.response || !xresp.response.output_text) {
|
2023-09-25 16:45:49 -05:00
|
|
|
return request.onerror(e);
|
|
|
|
}
|
|
|
|
|
2023-09-28 11:02:25 -05:00
|
|
|
if (xresp.response.error) {
|
|
|
|
setKagiState(container, 2, xresp.response.output_text, null);
|
2023-09-25 16:45:49 -05:00
|
|
|
} else {
|
2023-09-28 11:02:25 -05:00
|
|
|
setKagiState(container, 0, null, xresp.response.output_text);
|
2023-09-25 16:45:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.onerror = function(e) {
|
|
|
|
badAjax(this.status == 403);
|
2023-09-28 11:02:25 -05:00
|
|
|
setKagiState(container, 2, kagi_strings.error, null);
|
2023-09-25 16:45:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
request.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
request.send(JSON.stringify({
|
|
|
|
ajax: true,
|
|
|
|
_csrf: context.csrf
|
|
|
|
}));
|
|
|
|
}
|