Merge pull request 'moved messages to script variables' (#1) from cleanup into main

Reviewed-on: #1
This commit is contained in:
Rudis Muiznieks 2023-09-26 00:10:51 +00:00
commit 06b1f4b495
4 changed files with 21 additions and 11 deletions

View File

@ -1,6 +1,16 @@
<?php <?php
class FreshExtension_kagiSummarizer_Controller extends Minz_ActionController { 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() { public function summarizeAction() {
$this->view->_layout(false); $this->view->_layout(false);
@ -34,14 +44,10 @@ class FreshExtension_kagiSummarizer_Controller extends Minz_ActionController {
'Authorization: ' . $kagi_token 'Authorization: ' . $kagi_token
)); ));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl); $response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$response_body = substr($response, $header_size);
echo json_encode(array( echo json_encode(array(
'response' => json_decode($response_body), 'response' => json_decode($response),
'status' => curl_getinfo($curl, CURLINFO_HTTP_CODE) 'status' => curl_getinfo($curl, CURLINFO_HTTP_CODE)
)); ));
} }

View File

@ -5,6 +5,8 @@ class KagiSummarizerExtension extends Minz_Extension {
$this->registerTranslates(); $this->registerTranslates();
$this->registerHook('entry_before_display', [$this, 'addSummarizeButton']); $this->registerHook('entry_before_display', [$this, 'addSummarizeButton']);
Minz_View::appendScript($this->getFileUrl('script.js', 'js'), false, false, false); 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'); $this->registerController('kagiSummarizer');
} }
@ -30,7 +32,10 @@ class KagiSummarizerExtension extends Minz_Extension {
'params' => array( 'params' => array(
'id' => $entry->id() '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; return $entry;
} }
} }

View File

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

View File

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