Skip to content

Commit

Permalink
kakuyomu.jp and cross-extension
Browse files Browse the repository at this point in the history
- added kakuyomu.jp support
- added cross-extension messaging support
  • Loading branch information
ilih-ilih committed Aug 17, 2022
1 parent cb65d8e commit deac853
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 11 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@ Chrome extension to translate Japanese web novels with Sugoi Offline Translation
2. Unpack the archive
3. Copy files with overwrite to *backendServer / Program-Backend / Sugoi-Japanese-Translator / offlineTranslation / fairseq*

**Cross-extension messaging**

Messages should be sent from content scripts.

const sugoiWebExtenstionId = "abcdefghijklmnoabcdefhijklmnoabc";
const can_translate = await chrome.runtime.sendMessage(sugoiWebExtenstionId, { action: 'canTranslate' });
if (can_translate)
{
await chrome.runtime.sendMessage(sugoiWebExtenstionId, { action: 'translate' });
}

**Notes:**
* For now supported only *.syosetu.com.
* For now supported only syosetu.com and kakuyomu.jp.

**Known Problems:**

Expand Down
39 changes: 33 additions & 6 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ function button_toggle(tab_id, enable)

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

let response;
switch (data.action)
switch (request.action)
{
case 'show':
button_toggle(sender.tab.id, true);
Expand All @@ -32,12 +32,39 @@ function processMessage(data, sender, sendResponse)
response = {success: true};
break;
default:
console.error('Unknown request:', data);
console.error('Unknown request:', request);
response = {success: false};
break;
}

sendResponse(response);
}

chrome.runtime.onMessage.addListener(processMessage);
/**
*
* @param {any} request
* @param {MessageSender} sender
* @param {function} sendResponse
*/
async function processExternalMessage(request, sender, sendResponse)
{
let response;
switch (request.action)
{
case 'translate':
await chrome.tabs.sendMessage(sender.tab.id, {action: 'translate'});
response = true;
break;
case 'canTranslate':
response = await chrome.tabs.sendMessage(sender.tab.id, {action: 'canTranslate'});
break;
default:
response = null;
break;
}

sendResponse(response);
}

chrome.runtime.onMessage.addListener(processMessage);
chrome.runtime.onMessageExternal.addListener(processExternalMessage);
2 changes: 2 additions & 0 deletions js/classes/Translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Translator
{
_domains = {
'.syosetu.com': () => new ProxySyosetu(),
'kakuyomu.jp': () => new ProxyKakuyomu(),
'.kakuyomu.jp': () => new ProxyKakuyomu(),
};

/**
Expand Down
51 changes: 50 additions & 1 deletion js/classes/UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class UI
*/
proxy = null;

/**
*
* @type {Promise<void>}
* @private
*/
_translation = null;

/**
*
* @param {string} domain
Expand All @@ -33,6 +40,46 @@ class UI
this.chromeApi = new ChromeApi();
this.chromeApi.onStorageChanged(changes => this.config = changes['config'].newValue);
this.load();

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => this._processMessage(request, sender, sendResponse));
}

/**
*
* @param {any} request
* @param {MessageSender} sender
* @param {function} sendResponse
*/
async _processMessage(request, sender, sendResponse)
{
if (sender.tab)
{
return;
}

let response;
switch (request.action)
{
case 'canTranslate':
response = this.proxy.allowed;
break;
case 'translate':
if (this.proxy.allowed)
{
await (this._translation || this.translate());
response = true;
}
else
{
response = false;
}
break;
default:
response = null;
break;
}

sendResponse(response);
}

get config()
Expand Down Expand Up @@ -106,8 +153,10 @@ class UI
}

this.proxy.loadLines();
await this.translator.run(this.proxy.lines);
this._translation = this.translator.run(this.proxy.lines);
await this._translation;
this.proxy.validate();
this._translation = null;
}

createButtons()
Expand Down
33 changes: 33 additions & 0 deletions js/classes/proxy/ProxyKakuyomu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

class ProxyKakuyomu extends Proxy
{
constructor()
{
super();

this.content_selectors = ['.widget-episodeBody', ];
}

/**
*
* @returns {boolean}
* @override
*/
get allowed()
{
return document.querySelector('.widget-episodeBody') != null;
}

/**
*
* @override
*/
loadLines()
{
this.addLine(document.querySelector('#contentMain-header .widget-episodeTitle'));

const content = document.querySelectorAll('.widget-episodeBody > p');
content.forEach(e => this.addLine(e));
}
}
12 changes: 9 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@
"js": [
"js/vendor/jquery-3.4.1.min.js",

"js/classes/Translator.js",
"js/classes/TranslatorLine.js",
"js/classes/proxy/Proxy.js",
"js/classes/proxy/ProxySyosetu.js",
"js/classes/proxy/ProxyKakuyomu.js",

"js/classes/ChromeApi.js",
"js/classes/Config.js",
"js/classes/Translator.js",
"js/classes/TranslatorLine.js",
"js/classes/UI.js",

"js/main.js"
],
"matches": [ "*://*.syosetu.com/*", "*://syosetu.com/*" ],
"matches": [
"*://*.syosetu.com/*",
"*://syosetu.com/*",
"*://*.kakuyomu.jp/*",
"*://kakuyomu.jp/*"
],
"run_at": "document_end",
"all_frames": true
}
Expand Down

0 comments on commit deac853

Please sign in to comment.