英文:
how to create chrome extension for google translate?
问题
我想创建一个Chrome扩展程序,用于将页面翻译成我的语言。
我的解决方案没有起作用:
function google_translate_api(){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
document.body.appendChild(script);
}
function tElement(){
var gt = document.createElement('div');
gt.setAttribute("id", "google_translate_element");
document.body.appendChild(gt);
}
function googleTranslateElementInit() {
new google.translate.TranslateElement(
{pageLanguage: 'en'},
'google_translate_element'
);
}
chrome.action.onClicked.addListener(async (tab) => {
if (tab.url || tab.url) {
const prevState = await chrome.action.getBadgeText({ tabId: tab.id });
const nextState = prevState === 'ON' ? 'OFF' : 'ON'
await chrome.action.setBadgeText({
tabId: tab.id,
text: nextState,
});
}
});
function changeHtmlLangFr(){
document.documentElement.setAttribute('lang','fr')
tElement()
google_translate_api()
}
function changeHtmlLangEn(){
document.documentElement.setAttribute('lang','en')
tElement()
google_translate_api()
}
if (nextState === "ON")
{
await chrome.scripting.executeScript({
target : {tabId : tab.id},
func : changeHtmlLangFr,
});
}
else if (nextState === "OFF")
{
await chrome.scripting.executeScript({
target : {tabId : tab.id},
func : changeHtmlLangEn,
});
}
});
我想通过运行这个扩展程序将整个页面翻译成法语。在manifest.json文件中有一个suggested_key来运行这个扩展程序。
有没有办法制作这个扩展程序?请帮助我。
英文:
I want create chrome extension for translate page to my language.
My solution didn't work:
function google_translate_api(){
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
document.body.appendChild(script);
}
function tElement(){
var gt = document.createElement('div');
gt.setAttribute("id", "google_translate_element");
document.body.appendChild(gt);
}
function googleTranslateElementInit() {
new google.translate.TranslateElement(
{pageLanguage: 'en'},
'google_translate_element'
);
}
chrome.action.onClicked.addListener(async (tab) => {
if (tab.url || tab.url) {
const prevState = await chrome.action.getBadgeText({ tabId: tab.id });
const nextState = prevState === 'ON' ? 'OFF' : 'ON'
await chrome.action.setBadgeText({
tabId: tab.id,
text: nextState,
});
chrome.runtime.onInstalled.addListener(() => {
chrome.action.setBadgeText({
text: "OFF",
});
});
function changeHtmlLangFr(){
document.documentElement.setAttribute('lang','fr')
tElement()
google_translate_api()
}
function changeHtmlLangEn(){
document.documentElement.setAttribute('lang','en')
tElement()
google_translate_api()
}
if (nextState === "ON")
{
await chrome.scripting.executeScript({
target : {tabId : tab.id},
func : changeHtmlLangFr,
});
}
else if (nextState === "OFF")
{
await chrome.scripting.executeScript({
target : {tabId : tab.id},
func : changeHtmlLangEn,
});
}
}
});
I want the entire page to be translated into French by running this extension. and in the manifest.json file there is a suggested_key that to run this extension.
Is there a solution to make this extension? Help me please.
答案1
得分: 1
以下是您提供的内容的中文翻译:
这不是一个完整的答案,但我希望它有所帮助。
manifest.json
{
"manifest_version": 3,
"name": "googleTranslate",
"version": "1.0",
"action": {
"default_popup": "popup.html"
},
"permissions": [
"scripting"
],
"host_permissions": [
"<all_urls>"
]
}
popup.html
<html>
<body>
<button id="submit">提交</button>
<script src="popup.js"></script>
</body>
</html>
popup.js
function googleTranslate() {
function RegistergoogleTranslateElementInit() {
const script = document.createElement("script");
script.innerText =
"function googleTranslateElementInit() {" +
" console.log('googleTranslateElementInit');" +
" new google.translate.TranslateElement(" +
" { pageLanguage: 'en', includedLanguages: 'es,fr,it' }," +
" 'google_translate_element'" +
" );" +
"}";
document.body.appendChild(script);
}
function tElement() {
var gt = document.createElement("div");
gt.setAttribute("id", "google_translate_element");
document.body.insertBefore(gt, document.body.children[0]);
}
function google_translate_api() {
const script = document.createElement("script");
script.type = "text/javascript";
script.src = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
document.body.appendChild(script);
}
RegistergoogleTranslateElementInit();
tElement();
google_translate_api();
}
document.getElementById("submit").onclick = async () => {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript(
{
target: { tabId: tabs[0].id },
world: "MAIN",
func: googleTranslate
}
);
}
英文:
This is not a complete answer, but I hope it helps.
manifest.json
{
"manifest_version": 3,
"name": "googleTranslate",
"version": "1.0",
"action": {
"default_popup": "popup.html"
},
"permissions": [
"scripting"
],
"host_permissions": [
"<all_urls>"
]
}
popup.html
<html>
<body>
<button id="submit">submit</button>
<script src="popup.js"></script>
</body>
</html>
popup.js
function googleTranslate() {
function RegistergoogleTranslateElementInit() {
const script = document.createElement("script");
script.innerText =
"function googleTranslateElementInit() {" +
" console.log('googleTranslateElementInit');" +
" new google.translate.TranslateElement(" +
" { pageLanguage: 'en', includedLanguages: 'es,fr,it' }," +
" 'google_translate_element'" +
" );" +
"}";
document.body.appendChild(script);
}
function tElement() {
var gt = document.createElement("div");
gt.setAttribute("id", "google_translate_element");
document.body.insertBefore(gt, document.body.children[0]);
}
function google_translate_api() {
const script = document.createElement("script");
script.type = "text/javascript";
script.src = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
document.body.appendChild(script);
}
RegistergoogleTranslateElementInit();
tElement();
google_translate_api();
}
document.getElementById("submit").onclick = async () => {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
chrome.scripting.executeScript(
{
target: { tabId: tabs[0].id },
world: "MAIN",
func: googleTranslate
}
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论