如何创建用于谷歌翻译的Chrome扩展?

huangapple go评论142阅读模式
英文:

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

以下是您提供的内容的中文翻译:

这不是一个完整的答案,但我希望它有所帮助。

如何创建用于谷歌翻译的Chrome扩展?

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.

如何创建用于谷歌翻译的Chrome扩展?

manifest.json

{
  &quot;manifest_version&quot;: 3,
  &quot;name&quot;: &quot;googleTranslate&quot;,
  &quot;version&quot;: &quot;1.0&quot;,
  &quot;action&quot;: {
    &quot;default_popup&quot;: &quot;popup.html&quot;
  },
  &quot;permissions&quot;: [
    &quot;scripting&quot;
  ],
  &quot;host_permissions&quot;: [
    &quot;&lt;all_urls&gt;&quot;
  ]
}

popup.html

&lt;html&gt;
&lt;body&gt;
  &lt;button id=&quot;submit&quot;&gt;submit&lt;/button&gt;
  &lt;script src=&quot;popup.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

popup.js

function googleTranslate() {
  function RegistergoogleTranslateElementInit() {
    const script = document.createElement(&quot;script&quot;);
    script.innerText =
      &quot;function googleTranslateElementInit() {&quot; +
      &quot;  console.log(&#39;googleTranslateElementInit&#39;);&quot; +
      &quot;  new google.translate.TranslateElement(&quot; +
      &quot;    { pageLanguage: &#39;en&#39;, includedLanguages: &#39;es,fr,it&#39; },&quot; +
      &quot;    &#39;google_translate_element&#39;&quot; +
      &quot;  );&quot; +
      &quot;}&quot;;
    document.body.appendChild(script);
  }

  function tElement() {
    var gt = document.createElement(&quot;div&quot;);
    gt.setAttribute(&quot;id&quot;, &quot;google_translate_element&quot;);
    document.body.insertBefore(gt, document.body.children[0]);
  }

  function google_translate_api() {
    const script = document.createElement(&quot;script&quot;);
    script.type = &quot;text/javascript&quot;;
    script.src = &quot;//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit&quot;;
    document.body.appendChild(script);
  }

  RegistergoogleTranslateElementInit();
  tElement();
  google_translate_api();
}

document.getElementById(&quot;submit&quot;).onclick = async () =&gt; {
  const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
  chrome.scripting.executeScript(
    {
      target: { tabId: tabs[0].id },
      world: &quot;MAIN&quot;,
      func: googleTranslate
    }
  );
}

huangapple
  • 本文由 发表于 2023年6月8日 15:57:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定