Dịch trang web

Dịch trang web sang ngôn ngữ bạn chọn bằng Google Dịch, có thể thiết lập ngôn ngữ mặc định

// ==UserScript==
// @name        Dịch trang web
// @version     6.1
// @description Dịch trang web sang ngôn ngữ bạn chọn bằng Google Dịch, có thể thiết lập ngôn ngữ mặc định
// @license     MIT
// @author      TieuThanhNhi
// @match       *://*/*
// @grant       none
// @namespace   https://greasyfork.org/users/1195312
// ==/UserScript==

(function() {
  const buttonStyle = "position: fixed; top: 10px; right: 10px; z-index: 1000; padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer;";
  const modalStyle = "display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); z-index: 2000; justify-content: center; align-items: center;";
  const modalContentStyle = "background-color: #fff; padding: 20px; border-radius: 5px;";
  const languageInputStyle = "width: 100%; margin-bottom: 10px; padding: 5px;";
  const translateButtonStyle = "width: 100%; padding: 10px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer;";

  // Tạo và thêm button dịch vào trang web
  var button = document.createElement("button");
  button.textContent = "Dịch trang web";
  button.style.cssText = buttonStyle;
  document.body.appendChild(button);

  // Tạo modal cho người dùng chọn ngôn ngữ
  var modal = document.createElement("div");
  modal.style.cssText = modalStyle;

  var modalContent = document.createElement("div");
  modalContent.style.cssText = modalContentStyle;

  var languageInput = document.createElement("input");
  languageInput.placeholder = "Nhập mã ngôn ngữ (vd: vi)";
  languageInput.style.cssText = languageInputStyle;

  var translateButton = document.createElement("button");
  translateButton.textContent = "Dịch";
  translateButton.style.cssText = translateButtonStyle;

  modalContent.appendChild(languageInput);
  modalContent.appendChild(translateButton);
  modal.appendChild(modalContent);
  document.body.appendChild(modal);

  // Xử lý sự kiện click nút dịch
  button.addEventListener("click", function() {
    modal.style.display = "block";
    languageInput.focus(); // Focus vào ô nhập liệu khi hiển thị modal
  });

  // Xử lý sự kiện click nút dịch trong modal
  translateButton.addEventListener("click", function() {
    var langSelector = languageInput.value.trim().toLowerCase(); // Loại bỏ khoảng trắng và chuyển thành chữ thường
    if (langSelector) {
      var url = window.location.href;
      // Mở Google Dịch với URL trang web và ngôn ngữ được chọn
      window.open("https://translate.google.com/translate?sl=auto&tl=" + langSelector + "&u=" + encodeURIComponent(url), "_blank");
      // Lưu trữ cài đặt ngôn ngữ
      localStorage.setItem("userLanguage", langSelector);
      // Đóng modal sau khi dịch
      modal.style.display = "none";
    }
  });

  // Tự động xác định ngôn ngữ của trang web nếu đã lưu trữ cài đặt của người dùng hoặc sử dụng ngôn ngữ mặc định
  var userLanguage = localStorage.getItem("userLanguage");
  var defaultLanguage = "vi"; // Đặt ngôn ngữ mặc định là tiếng Việt (vi)
  if (userLanguage) {
    defaultLanguage = userLanguage;
  }
  var url = window.location.href;
  window.open("https://translate.google.com/translate?sl=auto&tl=" + defaultLanguage + "&u=" + encodeURIComponent(url), "_blank");
})();