2019-02-21 16:43:45 +00:00
|
|
|
import React from "react";
|
|
|
|
import ReactDOM from "react-dom";
|
2019-05-02 14:36:27 +00:00
|
|
|
import browser from "webextension-polyfill";
|
2019-10-20 08:26:48 +00:00
|
|
|
import { initSettings, getSettings, handleSettingsChange } from "src/settings/settings";
|
|
|
|
import { updateLogLevel, overWriteLogLevel } from "src/common/log";
|
2019-02-21 16:43:45 +00:00
|
|
|
import TranslateContainer from "./components/TranslateContainer";
|
|
|
|
|
2019-10-20 08:26:48 +00:00
|
|
|
const init = async () => {
|
|
|
|
await initSettings();
|
|
|
|
document.addEventListener("mouseup", handleMouseUp);
|
|
|
|
document.addEventListener("keydown", handleKeyDown);
|
|
|
|
browser.storage.onChanged.addListener(handleSettingsChange);
|
|
|
|
browser.runtime.onMessage.addListener(handleMessage);
|
|
|
|
overWriteLogLevel();
|
|
|
|
updateLogLevel();
|
|
|
|
disableExtensionByUrlList();
|
|
|
|
};
|
|
|
|
init();
|
|
|
|
|
2019-10-20 09:58:27 +00:00
|
|
|
let prevSelectedText = "";
|
2019-10-20 08:26:48 +00:00
|
|
|
const handleMouseUp = async e => {
|
|
|
|
await waitTime(10);
|
|
|
|
const isLeftClick = e.button === 0;
|
|
|
|
const isInPasswordField = e.target.tagName === "INPUT" && e.target.type === "password";
|
|
|
|
const isInThisElement =
|
|
|
|
document.querySelector("#simple-translate") &&
|
|
|
|
document.querySelector("#simple-translate").contains(e.target);
|
|
|
|
if (!isLeftClick) return;
|
|
|
|
if (isInPasswordField) return;
|
|
|
|
if (isInThisElement) return;
|
|
|
|
removeTranslatecontainer();
|
|
|
|
|
|
|
|
const selectedText = getSelectedText();
|
2019-10-20 09:58:27 +00:00
|
|
|
prevSelectedText = selectedText;
|
2019-10-20 08:26:48 +00:00
|
|
|
if (selectedText.length === 0) return;
|
|
|
|
|
2019-10-20 10:14:48 +00:00
|
|
|
if (getSettings("isDisabledInTextFields")) {
|
|
|
|
if (isInContentEditable()) return;
|
|
|
|
}
|
|
|
|
|
2019-10-20 08:26:48 +00:00
|
|
|
const clickedPosition = { x: e.clientX, y: e.clientY };
|
|
|
|
const selectedPosition = getSelectedPosition();
|
|
|
|
showTranslateContainer(selectedText, selectedPosition, clickedPosition);
|
|
|
|
};
|
|
|
|
|
|
|
|
const waitTime = time => {
|
|
|
|
return new Promise(resolve => setTimeout(() => resolve(), time));
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSelectedText = () => {
|
|
|
|
const element = document.activeElement;
|
|
|
|
const isInTextField = element.tagName === "INPUT" || element.tagName === "TEXTAREA";
|
|
|
|
const selectedText = isInTextField
|
|
|
|
? element.value.substring(element.selectionStart, element.selectionEnd)
|
|
|
|
: window.getSelection().toString();
|
|
|
|
return selectedText;
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSelectedPosition = () => {
|
|
|
|
const element = document.activeElement;
|
|
|
|
const isInTextField = element.tagName === "INPUT" || element.tagName === "TEXTAREA";
|
|
|
|
const selectedRect = isInTextField
|
|
|
|
? element.getBoundingClientRect()
|
|
|
|
: window
|
|
|
|
.getSelection()
|
|
|
|
.getRangeAt(0)
|
|
|
|
.getBoundingClientRect();
|
|
|
|
|
|
|
|
let selectedPosition;
|
|
|
|
const panelReferencePoint = getSettings("panelReferencePoint");
|
|
|
|
switch (panelReferencePoint) {
|
|
|
|
case "topSelectedText":
|
|
|
|
selectedPosition = {
|
|
|
|
x: selectedRect.left + selectedRect.width / 2,
|
|
|
|
y: selectedRect.top
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case "bottomSelectedText":
|
|
|
|
default:
|
|
|
|
selectedPosition = {
|
|
|
|
x: selectedRect.left + selectedRect.width / 2,
|
|
|
|
y: selectedRect.bottom
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return selectedPosition;
|
|
|
|
};
|
|
|
|
|
2019-10-20 10:14:48 +00:00
|
|
|
const isInContentEditable = () => {
|
|
|
|
const element = document.activeElement;
|
|
|
|
if (element.tagName === "INPUT" || element.tagName === "TEXTAREA") return true;
|
|
|
|
if (element.contentEditable === "true") return true;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2019-10-20 08:26:48 +00:00
|
|
|
const handleKeyDown = e => {
|
|
|
|
if (e.key === "Escape") {
|
|
|
|
removeTranslatecontainer();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-02 08:33:29 +00:00
|
|
|
let isEnabled = true;
|
|
|
|
const handleMessage = async request => {
|
|
|
|
const empty = new Promise(resolve => {
|
|
|
|
setTimeout(() => {
|
|
|
|
return resolve("");
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
|
|
|
|
switch (request.message) {
|
2019-10-20 08:26:48 +00:00
|
|
|
case "getTabUrl":
|
|
|
|
if (!isEnabled) return empty;
|
|
|
|
if (window == window.parent) return location.href;
|
|
|
|
else return empty;
|
2019-10-20 09:58:27 +00:00
|
|
|
case "getSelectedText":
|
2019-10-20 08:26:48 +00:00
|
|
|
if (!isEnabled) return empty;
|
2019-10-20 09:58:27 +00:00
|
|
|
if (prevSelectedText.length === 0) return empty;
|
|
|
|
else return prevSelectedText;
|
2019-10-20 08:26:48 +00:00
|
|
|
case "translateSelectedText": {
|
|
|
|
if (!isEnabled) return empty;
|
|
|
|
const selectedText = getSelectedText();
|
|
|
|
if (selectedText.length === 0) return;
|
|
|
|
const selectedPosition = getSelectedPosition();
|
|
|
|
removeTranslatecontainer();
|
|
|
|
showTranslateContainer(selectedText, selectedPosition, null, true);
|
|
|
|
break;
|
|
|
|
}
|
2019-05-02 08:33:29 +00:00
|
|
|
case "getEnabled":
|
|
|
|
return isEnabled;
|
|
|
|
case "enableExtension":
|
2019-10-20 08:26:48 +00:00
|
|
|
isEnabled = true;
|
2019-05-02 08:33:29 +00:00
|
|
|
break;
|
|
|
|
case "disableExtension":
|
2019-10-20 08:26:48 +00:00
|
|
|
removeTranslatecontainer();
|
|
|
|
isEnabled = false;
|
2019-05-02 08:33:29 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return empty;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-10-20 08:26:48 +00:00
|
|
|
const disableExtensionByUrlList = () => {
|
|
|
|
const disableUrls = getSettings("disableUrlList").split("\n");
|
|
|
|
let pageUrl;
|
|
|
|
try {
|
|
|
|
pageUrl = top.location.href;
|
|
|
|
} catch (e) {
|
|
|
|
pageUrl = document.referrer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const matchesPageUrl = urlPattern => {
|
|
|
|
const pattern = urlPattern
|
|
|
|
.trim()
|
|
|
|
.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, match => (match === "*" ? ".*" : "\\" + match));
|
|
|
|
if (pattern === "") return false;
|
|
|
|
return RegExp("^" + pattern + "$").test(pageUrl);
|
|
|
|
};
|
|
|
|
|
|
|
|
const isMatched = disableUrls.some(matchesPageUrl);
|
|
|
|
if (isMatched) isEnabled = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeTranslatecontainer = async () => {
|
2019-05-02 08:33:29 +00:00
|
|
|
const element = document.getElementById("simple-translate");
|
|
|
|
if (!element) return;
|
2019-05-02 19:43:59 +00:00
|
|
|
|
2019-05-02 08:33:29 +00:00
|
|
|
ReactDOM.unmountComponentAtNode(element);
|
|
|
|
element.parentNode.removeChild(element);
|
|
|
|
};
|
|
|
|
|
2019-10-20 08:26:48 +00:00
|
|
|
const showTranslateContainer = (
|
|
|
|
selectedText,
|
|
|
|
selectedPosition,
|
|
|
|
clickedPosition = null,
|
|
|
|
shouldTranslate = false
|
|
|
|
) => {
|
2019-05-02 08:33:29 +00:00
|
|
|
const element = document.getElementById("simple-translate");
|
|
|
|
if (element) return;
|
2019-10-20 08:26:48 +00:00
|
|
|
if (!isEnabled) return;
|
2019-05-02 08:33:29 +00:00
|
|
|
|
|
|
|
document.body.insertAdjacentHTML("beforeend", "<div id='simple-translate'></div>");
|
|
|
|
ReactDOM.render(
|
2019-05-02 19:43:59 +00:00
|
|
|
<TranslateContainer
|
2019-10-20 08:26:48 +00:00
|
|
|
removeContainer={removeTranslatecontainer}
|
|
|
|
selectedText={selectedText}
|
|
|
|
selectedPosition={selectedPosition}
|
|
|
|
clickedPosition={clickedPosition}
|
|
|
|
shouldTranslate={shouldTranslate}
|
2019-05-02 19:43:59 +00:00
|
|
|
/>,
|
2019-05-02 08:33:29 +00:00
|
|
|
document.getElementById("simple-translate")
|
|
|
|
);
|
|
|
|
};
|