Adjust Layout of Translate Website

This script is for adjusting the layout of the Baidu, Youdao, and/or Sogou Translate websites. Only it would be effective when the height of browser is greater than the width of browser.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Adjust Layout of Translate Website
// @name:en      Adjust Layout of Translate Website
// @name:zh-cn   调整翻译页面布局
// @namespace    http://tampermonkey.net/
// @version      0.0.3
// @description        This script is for adjusting the layout of the Baidu, Youdao, and/or Sogou Translate websites. Only it would be effective when the height of browser is greater than the width of browser.
// @description:en     This script is for adjusting the layout of the Baidu, Youdao, and/or Sogou Translate websites. Only it would be effective when the height of browser is greater than the width of browser.
// @description:zh-cn  根据浏览器的窗体情况调整百度|有道|搜狗翻译“待翻译文字”和“翻译后文字”的布局。只有浏览器高度大于宽度时生效。
// @author       [email protected]
// @match        http*://fanyi.baidu.com/*
// @match        http*://fanyi.youdao.com/*
// @match        http*://fanyi.sogou.com/*
// @icon         https://images2.imgbox.com/41/a0/oUqOCDlB_o.png
// @license      MIT
// @grant        none
// @run-at       document-end
// ==/UserScript==


(function() {
    'use strict';

    // Just a delicate difference.
    // You should guarantee the browser's height is greater than its width.
    // Otherwise, it would not be effective.
    // By the way, Google Translate is more humane. It would adjust the layout automatically.

    function adjustBaiduTranslateLayout()
    {
        // console.log('function adjustBaiduTranslateLayout()');
        let divContainer = document.getElementsByClassName("EUENIlUT")[0];
        let divLeft = document.getElementsByClassName("AZLVLJHb")[0];
        let divRight = document.getElementsByClassName("wB5ViVGi")[0];
        // console.log(divContainer, divLeft, divRight);

        if (window.outerWidth < window.outerHeight)
        {
            divContainer.style["flex-direction"] = "column";
            divRight.style.flex = "1 1";
            divRight.style.width = "80%";
            divLeft.style.width = "80%";
        } else {
            divContainer.style["flex-direction"] = "row";
            divRight.style.flex = "1 1";
            divRight.style.width = "50%";
            divLeft.style.width = "50%";
        }
    }

    function adjustYoudaoTranslateLayout()
    {
        let div = document.getElementById("TextTranslate");
        // console.log(div);

        if (window.outerWidth < window.outerHeight)
        {
            div.style['flex-direction'] = "column";
        } else {
            div.style['flex-direction'] = "row";
        }
    }

    function adjustSogouTranslateLayout()
    {
        let div = document.getElementsByClassName("trans-box")[0];
        // console.log(div);

        if (window.outerWidth < window.outerHeight)
        {
            div.style['flex-direction'] = "column";
        } else {
            div.style['flex-direction'] = "row";
        }
    }

    function adjustLayout()
    {
        let url = window.location.href;
        // console.log(url);

        if (url.indexOf('baidu.com') !== -1) {
            adjustBaiduTranslateLayout();
        } else if (url.indexOf('youdao.com') !== -1) {
            adjustYoudaoTranslateLayout();
        } else if (url.indexOf('sogou.com') !== -1) {
            adjustSogouTranslateLayout();
        }
    }

    window.onload=function()
    {
        // console.log("window onload");
        adjustLayout();
    }

    window.addEventListener("resize",function(){
        // console.log("event listerner");
        adjustLayout();
    },false);
})();