OcttKB Cross-Repo Sync (HTML to Raw)

This commit is contained in:
2023-11-05 23:29:21 +00:00
parent 3d736a0e51
commit dfc37bcd14
24 changed files with 133 additions and 39 deletions

View File

@@ -1,11 +1,11 @@
created: 20230124112022156
creator: Octt
modified: 20231103220122034
modified: 20231105185849353
modifier: Octt
tags: $:/tags/ViewTemplate
title: $:/Styles/View/9-99-Hacks
\define AiLinks(Enabled:1)
\define AiLinks()
\whitespace trim
<<script script="
try {
@@ -26,7 +26,7 @@ if ('$(Enabled)$' == 1) {
};
} catch(e) { ErrAtLine(e); };
">>
">><<script off>>
\end
<$set name=Enabled value={{$:/OcttKB/Config/AiLinks.ui!!Enabled}}>

View File

@@ -0,0 +1,65 @@
created: 20231105210128175
creator: Octt
list-after: $:/core/ui/ViewTemplate/tags
modified: 20231105232834847
modifier: Octt
tags: $:/tags/ViewTemplate
title: $:/Styles/View/i18n
\define OcttKB-i18n()
<<script script="try{
var BodySplitter = '</p>>';
var TidTitle = this.variables.thisTiddler.value;
var TidFrame = this.parentDomNode.parentElement;
/* var [TidTitleEl, TidBodyEl] = this.parentDomNode.parentElement.querySelectorAll('.tc-tiddler-title, .tc-tiddler-body'); */
var TidLangKnown = $tw.wiki.getTiddler(TidTitle).fields['i18n-main'];
this.parentDomNode.innerHTML += '[' + (TidLangKnown ? TidLangKnown : '?') + '] ';
var input = document.createElement('input');
input.type = 'text';
input.value = (window['OcttKB-i18n-current'] ? window['OcttKB-i18n-current'] : 'it');
input.onchange = function(){ window['OcttKB-i18n-current'] = input.value };
this.parentDomNode.appendChild(input);
var btn = document.createElement('button');
btn.innerHTML = 'Translate [Beta]';
btn.onclick = function(){
/* var TidLangGuessed; */
var TidBodyEl = TidFrame.querySelector('.tc-tiddler-body');
var TidBody = TidBodyEl.innerHTML;
var TidBodyParags = TidBody.trim().replaceAll('>', '>>').split(BodySplitter);
var TidBodyTranslated = '';
/* TODO: should probably fix the splitting to make it do longer segments, otherwise translations are more likely broken, and also find a good way to avoid the occasional translating of HTML elements; we should ideally do like the GTranslate embeddable script and parse the doctree to translate only the innerHTML strings */
for (var i=0; i<TidBodyParags.length; i++) {
if (TidBodyParags[i].trim()) {
/* TODO: make this not lock up the main thread */
var req = new XMLHttpRequest();
req.open('GET', `https://lingva.garudalinux.org/api/v1/${TidLangKnown ? TidLangKnown : 'auto'}/${window['OcttKB-i18n-current']}/${encodeURIComponent(TidBodyParags[i].trim())}`, false);
req.send();
var jsonResponse = JSON.parse(req.responseText);
TidBodyTranslated += jsonResponse.translation + BodySplitter;
/* TidLangGuessed = jsonResponse.info.detectedSource; */
}
}
var TransBodyEl = document.createElement('div');
TransBodyEl.innerHTML += TidBodyTranslated.replaceAll('>>', '>');
TidFrame.appendChild(TransBodyEl);
TidBodyEl.style.display = 'none';
btn.style.display = 'none';
var close = document.createElement('button');
close.innerHTML = '❌ Close';
close.onclick = function(){
TidFrame.removeChild(TransBodyEl);
TidBodyEl.style.display = 'revert';
btn.style.display = 'revert';
this.parentElement.removeChild(close);
};
this.parentElement.appendChild(close);
};
this.parentDomNode.appendChild(btn);
} catch(e) { ErrAtLine(e); };
">><<script off>>
\end
<div class="OcttKB-i18n">
<<OcttKB-i18n>>
</div>