moved messages to script variables

This commit is contained in:
Rudis Muiznieks 2023-09-26 00:02:54 +00:00
parent cc4951f515
commit 8604ed72a2
4 changed files with 21 additions and 11 deletions

View file

@ -1,12 +1,22 @@
<?php
class FreshExtension_kagiSummarizer_Controller extends Minz_ActionController {
public function kagiStringsAction() {
$this->view->kagi_strings = json_encode(array(
'loading_summary' => _t('ext.kagiSummarizer.ui.loading_summary'),
'error' => _t('ext.kagiSummarizer.ui.error')
));
$this->view->_layout(false);
$this->view->_path('kagiSummarizer/strings.js');
header('Content-Type: application/javascript; charset=UTF-8');
}
public function summarizeAction() {
$this->view->_layout(false);
$kagi_token = FreshRSS_Context::$user_conf->kagi_token;
if ($kagi_token === null || trim($kagi_token) ==='') {
if ($kagi_token === null || trim($kagi_token) === '') {
echo json_encode(array(
'response' => array(
'output_text' => _t('ext.kagiSummarizer.ui.no_token_configured'),
@ -34,14 +44,10 @@ class FreshExtension_kagiSummarizer_Controller extends Minz_ActionController {
'Authorization: ' . $kagi_token
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$response_body = substr($response, $header_size);
echo json_encode(array(
'response' => json_decode($response_body),
'response' => json_decode($response),
'status' => curl_getinfo($curl, CURLINFO_HTTP_CODE)
));
}

View file

@ -5,6 +5,8 @@ class KagiSummarizerExtension extends Minz_Extension {
$this->registerTranslates();
$this->registerHook('entry_before_display', [$this, 'addSummarizeButton']);
Minz_View::appendScript($this->getFileUrl('script.js', 'js'), false, false, false);
Minz_View::appendScript(_url('kagiSummarizer', 'kagiStrings'), false, true, false);
$this->registerViews();
$this->registerController('kagiSummarizer');
}
@ -30,7 +32,10 @@ class KagiSummarizerExtension extends Minz_Extension {
'params' => array(
'id' => $entry->id()
)));
$entry->_content('<p class="kagi-summary"><a data-loading="' . htmlspecialchars(_t('ext.kagiSummarizer.ui.loading_summary')) . '" data-error="' . htmlspecialchars(_t('ext.kagiSummarizer.ui.error')) . '" class="btn" href="' . $url .'">' . _t('ext.kagiSummarizer.ui.summarize_button') . '</a></p>' . $entry->content());
$entry->_content(
'<p class="kagi-summary"><a class="btn" href="' . $url .'">'
. _t('ext.kagiSummarizer.ui.summarize_button') . '</a></p>'
. $entry->content());
return $entry;
}
}

View file

@ -21,13 +21,11 @@ function configureSummarizeButtons() {
function summarizeButtonClick(button) {
var url = button.href;
var loadingMsg = button.dataset.loading;
var errorMsg = button.dataset.error;
var container = button.parentNode;
container.classList.add('alert');
container.classList.add('alert-warn');
container.innerHTML = loadingMsg;
container.innerHTML = kagi_strings.loading_summary;
var request = new XMLHttpRequest();
request.open('POST', url, true);
@ -62,7 +60,7 @@ function summarizeButtonClick(button) {
badAjax(this.status == 403);
container.classList.remove('alert-warn');
container.classList.add('alert-error');
container.innerHTML = errorMsg;
container.innerHTML = kagi_strings.error;
}
request.setRequestHeader('Content-Type', 'application/json');

View file

@ -0,0 +1 @@
var kagi_strings = <?= htmlspecialchars_decode(urldecode($this->kagi_strings)) ?>