google translate element

Google Translate Elementを自動的にページに挿入する。ページを移動したり新しいタブを開いたりすることなくページ全体を翻訳する。ソースコードを編集して変数"includedlanguages"と"ignorelanguages"を適切に設定するとより便利に使える。

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name     google translate element
// @version  1
// @grant    none
// @include  http://*
// @include  https://*
// @include  file://*
// @author   zupffwhy
// @homepageURL		https://addons.mozilla.org/firefox/user/12942033/
// @description:ja	Google Translate Elementを自動的にページに挿入する。ページを移動したり新しいタブを開いたりすることなくページ全体を翻訳する。ソースコードを編集して変数"includedlanguages"と"ignorelanguages"を適切に設定するとより便利に使える。
// @description:en	Automatically inserts Google Translate Element into the page. Translate the entire page without leaving page or opening a new tab. You can use it more conveniently by editing the source code and setting the variables "includedlanguages" and "ignorelanguages" appropriately.
// @icon	data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAADm0lEQVRYha3XS2idRRTA8V9yU7H4IKLWF6IIPkAXanxg1YW4UxGk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fq1JB5ARSIIeHbIeHTZhouRgrc/wLUIERgHdTdUqdEHEzqBFgt7YZ+Rue+uX2+AOXjxJ1gsTuv5R9x/5j3UwKzJf6P3CECdGwR8qK86PwHULYqSMjoGJwAAAAASUVORK5CYII=
// @namespace https://greasyfork.org/users/417745
// @description Google Translate Elementを自動的にページに挿入する。ページを移動したり新しいタブを開いたりすることなくページ全体を翻訳する。ソースコードを編集して変数"includedlanguages"と"ignorelanguages"を適切に設定するとより便利に使える。
// ==/UserScript==

// Specify a list of languages to translate to.
// If the string is empty, all languages, supported by Google Translate Element are displayed.
// Describe language codes separated by commas. Must not contain whitespace.
// The following URL is not a description of Google Translate Element, but many languages in Google Translate Element will be the same language code.
// https://cloud.google.com/translate/docs/languages
// let includedlanguages = "en,ja,zh-CN"; // Specify only English, Japanese, German, and Chinese (mandarin) as the target languages.
let includedlanguages = "";

// Specify a language (your native language) that is not automatically translated.
// let ignorelanguages = ["ja", "en"]; // Do not translate pages written in Japanese or English.
// let ignorelanguages = navigator.languages; // See https://developer.mozilla.org/docs/Web/API/NavigatorLanguage/languages
let ignorelanguages = [navigator.language]; // See https://developer.mozilla.org/docs/Web/API/NavigatorLanguage/language

// References
// How TO - Google Translate https://www.w3schools.com/howto/howto_google_translate.asp
// List of ISO 639-1 codes https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
// ISO 3166-1 https://en.wikipedia.org/wiki/ISO_3166-1


// Code
function modify_page(pagelanguage, includedlanguages) {
    if (document.querySelector("#google_translate_element")) {
    	return false;
    }
    let url = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
    let loc = new URL(window.location);
    if (loc.protocol != "http:" && loc.protocol != "https:") {
			url = "https:" + url;
    }
    let s = `function googleTranslateElementInit() {new google.translate.TranslateElement({pageLanguage: "${pagelanguage}", includedLanguages: "${includedlanguages}", layout: google.translate.TranslateElement.InlineLayout.SIMPLE, multilanguagePage: true,}, "google_translate_element" );}`;

    let elem = document.createElement("script");
    elem.setAttribute("id", "google_translate_element");
    elem.appendChild(document.createTextNode(s));
    document.body.appendChild(elem);
    elem = document.createElement("script");
    elem.setAttribute("src", url);
    document.body.appendChild(elem);
}

chrome.i18n.detectLanguage(document.body.innerText).then(langs => {
	//if (ignorelanguages.length === 0) {
  //  ignorelanguages = navigator.languages;
  //}
  if (langs.languages.length === 0 || ignorelanguages.indexOf(langs.languages[0].language) < 0) {
		modify_page("", includedlanguages);
  }
});