Skip to content

Commit

Permalink
Cleaned up errors and multiple displays
Browse files Browse the repository at this point in the history
  • Loading branch information
ianbbqzy committed Aug 22, 2023
1 parent 0713962 commit bdcf80f
Show file tree
Hide file tree
Showing 7 changed files with 387 additions and 205 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@

# Change Log
# Change Log

## 2023-08-21

Added comic-text-detector to help with capturing the multiple text bubbles in one go.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Veebee Browser Extension

Uses Google Vision, DeepL, GPT, and [manga-ocr](https://github.com/kha-white/manga-ocr) to help you with translating and/or learning new languages while browing the internet in a Chrome Browser. One great use case is for reading manga/manhwa/manhua but you can use it on almost any content on the web.
Uses Google Vision, DeepL, GPT, [comic-text-detector](https://github.com/dmMaze/comic-text-detector), and [manga-ocr](https://github.com/kha-white/manga-ocr) to help you with translating and/or learning new languages while browing the internet in a Chrome Browser. One great use case is for reading manga/manhwa/manhua but you can use it on almost any content on the web.

![](assets/example.png)

Expand Down
48 changes: 36 additions & 12 deletions background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ chrome.storage.sync.get((config) => {
chrome.storage.sync.set({api: 'deepl'})
}

if (!config.capture_mode) {
chrome.storage.sync.set({capture_mode: 'multiple'})
}

if (!config.pronunciation) {
chrome.storage.sync.set({pronunciation: 'off'})
}

if (!config.source_lang) {
chrome.storage.sync.set({source_lang: 'Japanese'})
}
Expand All @@ -32,15 +40,28 @@ chrome.storage.sync.get((config) => {
// for screenshot capture.
// It injects the content script into the active tab.
chrome.action.onClicked.addListener((tab) => {
pingContentScript(tab, 'initCrop');
// continue with the translation process.
chrome.storage.sync.get((config) => {
if (config.capture_mode === 'screen') {
pingContentScript(tab, 'screenCapture');
} else {
pingContentScript(tab, 'initCrop');
}
})
})

// take-screenshot is received when keyboard shortcut is triggered, as defined in manifest.json
// This is another entry point for screenshot capture.
chrome.commands.onCommand.addListener((command) => {
if (command === 'take-screenshot') {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
pingContentScript(tabs[0], 'initCrop');
chrome.storage.sync.get((config) => {
if (config.capture_mode === 'screen') {
pingContentScript(tabs[0], 'screenCapture');
} else {
pingContentScript(tabs[0], 'initCrop');
}
})
})
}
})
Expand Down Expand Up @@ -95,44 +116,47 @@ chrome.contextMenus.onClicked.addListener((info, tab) => {
if (!config.idToken) {
pingContentScript(tab, {"translation": "Please login first. Right click on the extension icon and click on options.", pronunciation: undefined});
} else {
callTranslateWithText(info.selectionText, config.source_lang, config.target_lang, config.api, config.idToken)
callTranslateWithText(info.selectionText, config.source_lang, config.target_lang, config.api, config.idToken, config.pronunciation)
.then(response => {
pingContentScript(tab, response)
if (response.error) {
pingContentScript(tab, {"error": `Translation: ${response.error}`, pronunciation: undefined});
} else {
pingContentScript(tab, response)
}
})
.catch(error => {
console.error(`error: ${error.message}`);
pingContentScript(tab, {"translation": `Translation: ${error.message}`, pronunciation: undefined});
pingContentScript(tab, {"error": `Translation: ${error.message}`, pronunciation: undefined});
});
}
})
}
});

// Modify the callTranslateWithText function
async function callTranslateWithText(text, source_lang, target_lang, api, idToken) {
async function callTranslateWithText(text, source_lang, target_lang, api, idToken, pronunciation) {
const url = "__BACKEND_URL__";
const headers = new Headers();
headers.append('Authorization', `Bearer ${idToken}`);
headers.append('Content-Type', `application/json`);

try {
const resp = await fetch(url + '/translate-text?api=' + api + '&source_lang=' + source_lang + '&target_lang=' + target_lang, {
const resp = await fetch(url + '/translate-text?api=' + api + '&source_lang=' + source_lang + '&target_lang=' + target_lang + (pronunciation === "on" ? "&pronunciation=true" : ""), {
method: 'POST',
headers: headers,
body: JSON.stringify({
'text': text
})
}).then(res => res.json())
if (resp.error) {
return {"translation": `Translation: ${resp.error}`, pronunciation: undefined};
}
if (resp.translation) {
return {"error": `Translation: ${resp.error}`};
} else if (resp.translation) {
return {"translation": resp.translation, "pronunciation": resp.pronunciation};
} else {
return {"translation": `Error: translation is not valid: ${resp}`, "pronunciation": resp.pronunciation};
return {"error": `Error: translation is not valid: ${resp}`};
}
} catch (err) {
return {"translation": `Translation: ${err.message}`, pronunciation: undefined};
return {"error": `Translation: ${err.message}`};
}
}

Expand Down
Loading

0 comments on commit bdcf80f

Please sign in to comment.