Skip to content

Commit

Permalink
support google doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ttop32 committed Dec 15, 2023
1 parent bae10b4 commit 466573e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
1 change: 1 addition & 0 deletions doc/description.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ English, Russian, Japanese, Chinese and so on
- 0.1.99
- fix sciencedirect pdf conflict (request by Tamer)
- make bidirectional translate for writing (request by IkiamJ)
- support text select on google document
- 0.1.98
- fix pdf line space
- 0.1.97
Expand Down
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"matches": ["<all_urls>"]
},
{
"matches": ["*://docs.google.com/document/*"],
"matches": ["*://docs.google.com/*"],
"run_at": "document_start",
"js": ["googleDocInject.js"],
"world": "MAIN"
Expand Down
27 changes: 3 additions & 24 deletions src/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var isYoutubeDetected = false;
var delayTime = 700;
var isBlobPdf = false;
var prevWordParam = [];
var isGoogleDoc = false;

//tooltip core======================================================================
$(async function initMouseTooltipTranslator() {
Expand Down Expand Up @@ -80,7 +81,7 @@ function startMouseoverDetector() {

//determineTooltipShowHide based on selection
function startTextSelectDetector() {
enableSelectionEndEvent(); //set mouse drag text selection event
enableSelectionEndEvent(window, isGoogleDoc); //set mouse drag text selection event
addEventHandler("selectionEnd", async function (event) {
// if translate on selection is enabled
if (
Expand Down Expand Up @@ -679,29 +680,7 @@ function checkGoogleDoc() {
return;
}

// todo, select and mouseover for google doc

//get select word by using copy hijack
// run this code on $(".docs-texteventtarget-iframe")
//manifest "match_about_blank": true, is required to inject

// setInterval(() => {
// const el = document.querySelector("[contenteditable=true]");
// var evt = new CustomEvent("copy");
// el?.dispatchEvent(evt);
// var b = document.querySelector("b");
// if (b?.innerText) {
// console.log(b.innerText);
// }
// }, 1000);

// $(".docs-texteventtarget-iframe")
// .contents()
// .find("body")
// .append($("<script>").html(script));

// about:blank
// }
isGoogleDoc = true;
}

// youtube================================
Expand Down
34 changes: 32 additions & 2 deletions src/event/selection.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* Selection related functions
*/

import $ from "jquery";
import { debounce } from "throttle-debounce";

var _win;
var prevNoneSelect = false;
export function enableSelectionEndEvent(_window = window) {
var _isGoogleDoc = false;
export function enableSelectionEndEvent(_window = window, isGoogleDoc = false) {
_win = _window;
_isGoogleDoc = isGoogleDoc;

// Listen selection change every 700 ms. It covers keyboard selection and selection from menu (select all option)
_win.document.addEventListener(
Expand Down Expand Up @@ -49,6 +51,14 @@ function isNoneSelectElement(element) {
}

export function getSelectionText() {
if (!_isGoogleDoc) {
return getWindowSelection();
} else {
return getGoogleDocSelection();
}
}

function getWindowSelection() {
let text = "";
if (_win.getSelection) {
text = _win.getSelection().toString();
Expand All @@ -73,3 +83,23 @@ export const triggerSelectionEnd = (text) => {
evt.selectedText = text;
document.dispatchEvent(evt);
};

//google doc ==========================
function getGoogleDocSelection() {
//get google doc text event iframe
var iframe = $(".docs-texteventtarget-iframe").contents();
var textBox = iframe.find("[contenteditable=true]");

//request copy for hijack copy text element as select text from iframe
var el = textBox.get(0);
var evt = new CustomEvent("copy");
el?.dispatchEvent(evt);

//get select text
var text = "";
iframe.find("span").each(function () {
text += $(this).text() + " ";
});
text = text.trim();
return text;
}
File renamed without changes.

0 comments on commit 466573e

Please sign in to comment.