use translate api

This commit is contained in:
cutls 2022-12-05 23:43:15 +09:00
parent c67b4b7037
commit 30d4dae0c4

View File

@ -600,28 +600,21 @@ function staCopy(id) {
} }
//翻訳 //翻訳
function trans(tar, to, elem) { function trans(tar, to, elem) {
var html = elem.parents('.cvo').find('.toot').html() var id = elem.parents('.cvo').attr('toot-id')
if (html.match(/^<p>(.+)<\/p>$/)) { alert(id)
html = html.match(/^<p>(.+)<\/p>$/)[1]
}
html = html.replace(/<br\s?\/?>/g, '\n')
html = html.replace(/<p>/g, '\n')
html = html.replace(/<\/p>/g, '\n')
html = $.strip_tags(html)
if (~tar.indexOf('zh')) {
tar = 'zh'
}
$('#toot-this .additional').text('Loading...(Powered by Google Translate)') $('#toot-this .additional').text('Loading...(Powered by Google Translate)')
var exec = var domain = localStorage.getItem('domain_' + acct_id)
'https://script.google.com/macros/s/AKfycbxhwW5tjjop9Irg-y1zr_WsXlCKEzwWG6KuoOt_vVRDfEbRv0c/exec?format=json&text=' + if (localStorage.getItem('mode_' + domain) == 'misskey') {
encodeURIComponent(html) + return false
'&source=' + }
tar + var at = localStorage.getItem('acct_' + acct_id + '_at')
'&target=' + var exec = `https://${domain}/api/v1/statuses/${id}/translate`
to
console.log('Try to translate from ' + tar + ' to ' + to + ' at ' + exec)
fetch(exec, { fetch(exec, {
method: 'GET' method: 'POST',
headers: {
'content-type': 'application/json',
Authorization: 'Bearer ' + at
}
}) })
.then(function (response) { .then(function (response) {
if (!response.ok) { if (!response.ok) {
@ -637,7 +630,8 @@ function trans(tar, to, elem) {
console.error(error) console.error(error)
}) })
.then(function (text) { .then(function (text) {
elem.parents('.cvo').find('.toot').append('<span class="gray translate">' + text.text + '</span>') console.log(text)
elem.parents('.cvo').find('.toot').append('<span class="gray translate">' + text.content + '</span>')
}) })
} }
//ブラウザで開く //ブラウザで開く