I’m working on a Chrome extension that allows users to highlight a word, right-click to translate it using Google Translate, and then log the translation pair to Chrome’s local storage. I have included Google Translate in both permissions and host_permissions (and have tried each individually), but I keep encountering the following error: Unchecked runtime.lastError: Cannot access contents of the page. Extension manifest must request permission to access the respective host.
I am wondering if visiting an external website in background.js is allowed, or if only local hosts are permitted. I wanted to use the detect-language feature in Google Translate, but given that Chrome storage is limited to around 10MB, it doesn’t seem feasible to include a neural network for detecting and translating multiple languages.
Is there any other way to work around this issue without building my own backend for it?
Thank you in advance for your help!
This is my code after reading Unchecked runtime.lastError: Cannot access contents of url “”. Extension manifest must request permission to access this host. In manifest 3:
I wanted to use the detect-language feature in google translate, this is just a minimum viable(not so much) product
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "translateToEnglish" || info.menuItemId === "translateToChinese") {
const word = info.selectionText;
const targetLang = info.menuItemId === "translateToEnglish" ? "en" : "zh-CN";
const url = `https://translate.google.com/?sl=auto&tl=${targetLang}&text=${word}&op=translate`;
(async () => {
const newTab = await chrome.tabs.create({ url: url });
const tabId = newTab.id;
if (!newTab.url) await onTabUrlUpdated(tabId);
const results = await chrome.scripting.executeScript({
target: { tabId },
func: fetchTranslation
});
if (results && results[0] && results[0].result) {
const translation = results[0].result;
console.log(`Translation to ${targetLang === "en" ? "English" : "Chinese"}:`, translation);
}
chrome.tabs.remove(tabId);
})();
}
});
M X is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.