Skip to content

Commit

Permalink
Improvements by @jobisoft
Browse files Browse the repository at this point in the history
  • Loading branch information
itagagaki committed Jul 26, 2023
1 parent 8e6c9d4 commit 1ce1820
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
95 changes: 57 additions & 38 deletions Thunderbird/background.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,79 @@
function goDeepL(text)
async function goDeepL(text, allowReload = true)
{
messenger.storage.local.get(['target', 'width', 'height', 'source_lang', 'target_lang'], function(result) {
let source_lang = result.source_lang ? result.source_lang : '?';
let target_lang = result.target_lang ? result.target_lang : '?';
let result = await messenger.storage.local.get(['target', 'width', 'height', 'source_lang', 'target_lang']);
let source_lang = result.source_lang ? result.source_lang : '?';
let target_lang = result.target_lang ? result.target_lang : '?';

if (source_lang == '?') {
source_lang = 'null';
}
if (source_lang == '?') {
source_lang = 'null';
}

if (target_lang == '?') {
target_lang = chrome.i18n.getUILanguage();
}
if (target_lang == '?') {
target_lang = chrome.i18n.getUILanguage();
}

const escapedText = text.replace(/\\/g, '\\\\')
.replace(/\//g, '\\/')
.replace(/\|/g, '\\|');
const escapedText = text.replace(/\\/g, '\\\\')
.replace(/\//g, '\\/')
.replace(/\|/g, '\\|');

const url = 'https://www.deepl.com/translator#'+source_lang+'/'+target_lang+'/'+encodeURIComponent(escapedText);
//console.log(url);
const url = 'https://www.deepl.com/translator#' + source_lang + '/' + target_lang + '/' + encodeURIComponent(escapedText);

// Wait for the page load in the opened tab/window to trigger a reload if needed.
let status = await new Promise(async resolve => {
let tabId;
let listener = async details => {
//console.log("Received tabId", details.tabId);
if (details.tabId != tabId) {
return;
}
let releaseGroups = await messenger.cookies.get({ name: "releaseGroups", url: "https://www.deepl.com" });
if (releaseGroups == null) {
messenger.webNavigation.onCompleted.removeListener(listener);
resolve({ tabId, needsReload: false });
return;
}
// We have to delete this cookie and re-request due to the DeepL translator bug.
await messenger.cookies.remove({ name: "releaseGroups", url: "https://www.deepl.com" });
messenger.webNavigation.onCompleted.removeListener(listener);
resolve({ tabId, needsReload: true });
}
messenger.webNavigation.onCompleted.addListener(listener);

switch (result.target) {
case "window":
messenger.windows.create({url: url, type: "normal", width: Number(result.width), height: Number(result.height)});
let window = await messenger.windows.create({ url: url, type: "popup", width: Number(result.width), height: Number(result.height) });
let { tabs } = await browser.windows.get(window.id, { populate: true });
tabId = tabs[0].id;
break;
case "tabs":
default:
messenger.tabs.create({'url': url});
let tab = await messenger.tabs.create({ 'url': url });
tabId = tab.id;
break;
}
});
}
//console.log("Created tabId:", tabId);
})

function onCreated() {
if (messenger.runtime.lastError) {
console.log('DeepL:'+`Error: ${messenger.runtime.lastError}`);
if (status.needsReload && allowReload) {
console.log("Needs Reload");
let { version } = await browser.runtime.getBrowserInfo();
let majorVersion = parseInt(version.split(".").shift());
if (result.target == "window" && majorVersion < 115) {
messenger.tabs.remove(status.tabId);
// Prevent endless loops by not allowing closing and re-opening a second time.
await goDeepL(text, false);
} else {
messenger.tabs.reload(status.tabId);
}
}
}

/**/
}

// Workaround for the DeepL translator bug
function addressDeepLBug(details)
{
if (details.url.match(/^https:\/\/.*\.deepl\.com/)) {
messenger.cookies.get({name: "releaseGroups", url: "https://www.deepl.com"})
.then(releaseGroups => {
if (releaseGroups != null) {
messenger.cookies.remove({name: "releaseGroups", url: "https://www.deepl.com"})
.finally(() => chrome.tabs.reload(details.tabId));
}
});
function onCreated() {
if (messenger.runtime.lastError) {
console.log('DeepL:' + `Error: ${messenger.runtime.lastError}`);
}
}
messenger.webNavigation.onCompleted.addListener(addressDeepLBug);

/**/

messenger.menus.create({
id: "menuDeepL",
Expand Down
2 changes: 1 addition & 1 deletion Thunderbird/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "__MSG_app_name__",
"description": "__MSG_app_desc__",
"version": "1.6.1",
"version": "1.6.2",
"author": "__MSG_app_author__",
"applications": {
"gecko": {
Expand Down

0 comments on commit 1ce1820

Please sign in to comment.