Skip to content

Commit

Permalink
added firefox support
Browse files Browse the repository at this point in the history
  • Loading branch information
ilih-ilih committed Oct 9, 2022
1 parent b968abd commit a8780e2
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 23 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# Sugoi-web
Chrome extension to translate Japanese web novels with Sugoi Offline Translation Server (https://www.patreon.com/mingshiba) or DeepL API.
Extension for Chromium-based browsers and Firefox to translate Japanese web novels with Sugoi Offline Translation Server (https://www.patreon.com/mingshiba) or DeepL API.

**Installation:**
**Installation for Chromium-based browsers:**
1. Download the *Source code (zip)* archive from Releases
2. Unpack the archive
3. Open extensions page (*chrome://extensions/*)
4. Enable **Developer Mode**
5. Add extension using the "Load Unpacked" button
5. Press "Load Unpacked" button and select folder with *manifest.json*

**Installation for Firefox:**
1. Download the *Source code (zip)* archive from Releases
2. Unpack the archive
3. Delete *manifest.json*
4. Rename *manifest-firefox.json* to *manifest.json*
5. Open extensions page (*about:debugging#/runtime/this-firefox*)
6. Press "Load Temporary Add-on" button and select *manifest.json*

**Using with Sugoi:**
1. Open *{Sugoi folder} / backendServer / Program-Backend / Sugoi-Japanese-Translator / offlineTranslation*
Expand Down
32 changes: 17 additions & 15 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class DeepL
data += '&text=' + encodeURIComponent(lines[i]);
}

/*
const response = await fetch(this.url, {
method: 'POST',
body: data,
Expand All @@ -70,15 +69,6 @@ class DeepL
'Content-Type': 'application/x-www-form-urlencoded',
}
});
*/
const response = await fetch('https://api-free.deepl.com/v2/translate', {
method: 'POST',
body: data,
headers: {
'Authorization': 'DeepL-Auth-Key 3e598917-623e-c1dc-1163-cad97a555430:fx',
'Content-Type': 'application/x-www-form-urlencoded',
},
});

const result = [];

Expand All @@ -101,7 +91,14 @@ class DeepL

function button_toggle(tab_id, enable)
{
chrome.action.enable(tab_id);
if (chrome.action)
{
chrome.action.enable(tab_id);
}
else
{
chrome.browserAction.enable(tab_id);
}
/*
chrome.action.setIcon({
path: enable ? 'images/icon32.png' : 'images/icon32_off.png'
Expand Down Expand Up @@ -227,12 +224,17 @@ async function processExternalMessage(request, sender)
return response;
}

function is_firefox()
{
return typeof browser !== 'undefined';
}

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
processMessage(request, sender).then(sendResponse);
return true;
const promise = processMessage(request, sender).then(sendResponse);
return is_firefox ? promise : true;
});

chrome.runtime.onMessageExternal.addListener((request, sender, sendResponse) => {
processExternalMessage(request, sender).then(sendResponse);
return true;
const promise = processExternalMessage(request, sender).then(sendResponse);
return is_firefox ? promise : true;
});
5 changes: 4 additions & 1 deletion js/classes/ChromeApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ class ChromeApi
config.deepl_texts_per_request = parseInt(config.deepl_texts_per_request);
}

return this.storageSet({config: config});
// firefox workaround, otherwise saved empty object
const data = JSON.parse(JSON.stringify(config));

return this.storageSet({config: data});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/classes/TranslatorSugoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TranslatorSugoi extends Translator
* @type {string}
* @private
*/
_url = 'http://127.0.0.1';
_url = 'http://localhost';

/**
*
Expand Down
65 changes: 65 additions & 0 deletions manifest-firefox.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "Sugoi Web Translation",
"version": "1.0.8",

"manifest_version": 2,

"description": "Sugoi/DeepL Web Translation",
"icons": {
"128": "images/s128.png",
"16": "images/s16.png",
"48": "images/s48.png"
},

"browser_action": {
"default_icon": {
"32": "images/s32.png"
},
"default_title": "Sugoi/DeepL Web Translation",
"default_popup": "popup/action.html"
},

"permissions": [
"storage",
"tabs",
"http://localhost/*",
"https://api-free.deepl.com/v2/*",
"https://api.deepl.com/v2/*"
],
"background": {
"scripts": ["js/background.js"]
},

"content_scripts":[
{
"js": [
"js/vendor/jquery-3.4.1.min.js",
"js/classes/Utilities.js",

"js/classes/Translator.js",
"js/classes/TranslatorSugoi.js",
"js/classes/TranslatorDeepL.js",
"js/classes/TranslatorLine.js",

"js/classes/proxy/Proxy.js",
"js/classes/proxy/ProxySyosetu.js",
"js/classes/proxy/ProxyKakuyomu.js",
"js/classes/proxy/ProxyUnavailable.js",

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

"js/main.js"
],
"matches": [
"*://*.syosetu.com/*",
"*://syosetu.com/*",
"*://*.kakuyomu.jp/*",
"*://kakuyomu.jp/*"
],
"run_at": "document_end",
"all_frames": true
}
]
}
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "Sugoi Web Translation",
"version": "1.0.7",
"version": "1.0.8",

"manifest_version": 3,
"incognito": "split",

"description": "",
"description": "Sugoi/DeepL Web Translation",
"icons": {
"128": "images/s128.png",
"16": "images/s16.png",
Expand All @@ -16,7 +16,7 @@
"default_icon": {
"32": "images/s32.png"
},
"default_title": "Sugoi Web Translation",
"default_title": "Sugoi/DeepL Web Translation",
"default_popup": "popup/action.html"
},

Expand Down

0 comments on commit a8780e2

Please sign in to comment.