Skip to content

Commit

Permalink
translate only english text.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdnsk committed Jan 5, 2018
1 parent 578cdad commit 5ef3517
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/entry/content-capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,42 @@ document.body.addEventListener('mousedown', e => {
});

document.body.addEventListener('dblclick', e => {
if (options['double-click']) {
// If there's a key constraint.
if (options['double-click-ctrl'] || options['double-click-alt'] || options['double-click-alt']) {
// Do nothing if dblclick handling is disabled.
if (! options['double-click']) {
return;
}

// If there are no conditions that have passed a check.
if (! (isAltClick(e) || isCtrlClick(e) || isCmdClick(e))) {
return;
}
// If there's a key constraint.
if (options['double-click-ctrl'] || options['double-click-alt'] || options['double-click-alt']) {

// If there are no conditions that have passed a check.
if (! (isAltClick(e) || isCtrlClick(e) || isCmdClick(e))) {
return;
}
}

const selection = window.getSelection();
const text = selection.toString().trim();

openPopup();
// Do nothing if there are no english letters.
if (! containsAnyEnglishLetter(text)) {
return;
}

openPopup(text, selection);
});

function closePopup () {
sendMessage({ id: PROXY_CONTENT_CLOSE_POPUP });
}

function openPopup () {
const selection = window.getSelection();

function openPopup (text, selection) {
if (selection.rangeCount > 0) {
const rect = selection.getRangeAt(0).getBoundingClientRect();

const message = {
id: PROXY_CONTENT_OPEN_POPUP,
text: selection.toString(),
text,
context: '',
rect: {
top: rect.top,
Expand Down Expand Up @@ -79,6 +88,10 @@ function isCmdClick (e) {
return options['double-click-meta'] && e.metaKey;
}

function containsAnyEnglishLetter (text) {
return /.*[a-zA-Z].*/.test(text);
}

function sendMessage (message) {
message['frameIndex'] = -1;

Expand Down

0 comments on commit 5ef3517

Please sign in to comment.