Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ilih-ilih committed Jun 14, 2022
0 parents commit 9349f9e
Show file tree
Hide file tree
Showing 27 changed files with 12,923 additions and 0 deletions.
45 changes: 45 additions & 0 deletions !compile/action.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Sugoi Web Translation</title>
<meta content="text/html; charset=utf-8">
<script src="../js/vendor/vue.js"></script>
</head>
<body style="width: 400px;">
<div id="app">
<div>
<label>
Enabled: <input type="checkbox" v-model="enabled">
</label>
</div>
<div>
<label>
Always Translate: <input type="checkbox" v-model="alwaysTranslate">
</label>
</div>
<div>
<label>
Server Port:
<input name="port" type="number" v-model="port" min="1">
</label>
</div>
<div>
<label>
Concurrent Requests:
<input name="requests" type="number" v-model="requests" min="1">
</label>
</div>
</div>

<textarea id="output" style="width: 100%; height: 500px;">

</textarea>

<script>
const html = document.getElementById('app').innerHTML;
const compiled = Vue.compile('<div>' + html + '</div>');
console.log(compiled.render);
document.getElementById('output').value = compiled.render.toString();
</script>
</body>
</html>
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
!build
.hg
3 changes: 3 additions & 0 deletions .hgignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
!build
.git
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Sugoi-web
Chrome extension: translates pages using Sugoi Offline Translation Server

To install:
1. Download the "sugoi-web" archive from Releases
2. Unpack the archive
3. Open "chrome://extensions/"
4. Enable "Developer Mode"
5. Add extension using the "Load Unpacked" button

Recommended to update server to support long lines:
1. Download the "server" archive from Releases
2. Unpack the archive
3. Copy files with overwrite to "backendServer / Program-Backend / Sugoi-Japanese-Translator / offlineTranslation / fairseq"
Binary file added images/s128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/s16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/s32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/s48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';

function button_toggle(tab_id, enable)
{
chrome.action.enable(tab_id);
/*
chrome.action.setIcon({
path: enable ? 'images/icon32.png' : 'images/icon32_off.png'
});
*/
}

/**
*
* @param {any} data
* @param {MessageSender} sender
* @param {function} sendResponse
*/
function processMessage(data, sender, sendResponse)
{
console.log('message:', data, sender);

let response;
switch (data.action)
{
case 'show':
button_toggle(sender.tab.id, true);
response = {success: true};
break;
case 'hide':
button_toggle(sender.tab.id, false);
response = {success: true};
break;
default:
console.error('Unknown request:', data);
response = {success: false};
break;
}

sendResponse(response);
}

chrome.runtime.onMessage.addListener(processMessage);
103 changes: 103 additions & 0 deletions js/classes/ChromeApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
'use strict';

class ChromeApi
{
/**
*
* @type {[function]}
* @private
*/
_changedListeners = [];

constructor()
{
chrome.storage.onChanged.addListener((changes, namespace) => this._processStorageChanged(changes, namespace));
}

/**
*
* @param {object} changes
* @param {string} namespace
* @private
*/
_processStorageChanged(changes, namespace)
{
this._changedListeners.forEach(x => x(changes, namespace));
}

/**
*
* @param {string} key
* @returns {Promise<object>}
*/
async storageGet(key)
{
return new Promise(resolve => {
chrome.storage.local.get([key], response => {
resolve(response[key] ?? null);
});
});
}

/**
*
* @param {object} data
* @returns {Promise<any>}
*/
async storageSet(data)
{
return new Promise(resolve => {
chrome.storage.local.set(data, response => {
resolve(response);
});
});
}

/**
*
* @returns {Promise<Config>}
*/
async getConfig()
{
return this.storageGet('config');
}

/**
*
* @param {Config} config
* @returns {Promise<any>}
*/
async setConfig(config)
{
return this.storageSet({config: config});
}

/**
*
* @param {function} listener
*/
onStorageChanged(listener)
{
this._changedListeners.push(listener);
}

/**
*
* @param {any} request
* @returns {Promise<any>}
*/
async send(request)
{
return chrome.runtime.sendMessage(request);
}

/**
*
* @param {object} query
* @returns {Promise<object>}
*/
async tabsFind(query)
{
return chrome.tabs.query(query);
}
}
26 changes: 26 additions & 0 deletions js/classes/Config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Config
{
/**
*
* @type {boolean}
*/
enabled = true;

/**
*
* @type {boolean}
*/
alwaysTranslate = false;

/**
*
* @type {string}
*/
port = '14366';

/**
*
* @type {number}
*/
requests = 10;
}
141 changes: 141 additions & 0 deletions js/classes/Translator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
'use strict';

class Translator
{
_domains = {
'.syosetu.com': () => new ProxySyosetu(),
};

/**
*
* @type {string}
* @private
*/
_url = 'http://127.0.0.1';

/**
*
* @type {string}
*/
port = '14366';

/**
*
* @type {number}
*/
requests = 10;

/**
*
* @type {string}
*/
get url()
{
return this._url + ':' + this.port;
}

/**
*
* @param domain
* @returns {null|Proxy}
*/
getProxy(domain)
{
for (let d in this._domains)
{
if ((domain === d) || domain.endsWith(d))
{
return this._domains[d]();
}
}

return null;
}

/**
*
* @returns {Promise<boolean>}
*/
async serverAvailable()
{
try
{
const response = await this._request('test');
return true;
}
catch (e)
{
return false;
}
}

/**
*
* @param {[TranslatorLine]} lines
* @returns {Promise<void>}
*/
async run(lines)
{
/**
*
* @type {Promise<void>[]}
*/
const promises = [];
let i = 0;
for (i = 0; i < lines.length; i++)
{
if (i === this.requests)
{
break;
}

promises.push(this._translate(lines[i]));
}

const next = () => {
const p = this._translate(lines[i]);
i += 1;

return i < lines.length ? p.then(next) : p;
};

return await Promise.race(promises).then(next);
}

/**
*
* @param {TranslatorLine} line
* @returns {Promise<void>}
* @private
*/
async _translate(line)
{
line.translation = line.needTranslate
? await this._request(line.original)
: line.original;
}

/**
*
* @param {string} jpn
* @returns {Promise<string>}
* @private
*/
async _request(jpn)
{
const data = {
message: 'translate sentences',
content: jpn
};

const response = await fetch(this.url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json'
}
});

return await response.json();
}
}
Loading

0 comments on commit 9349f9e

Please sign in to comment.