Skip to content

Commit

Permalink
fix touch recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
ttop32 committed Dec 20, 2023
1 parent 4c04d9d commit 1a743c7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 50 deletions.
58 changes: 22 additions & 36 deletions src/contentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,15 @@ async function processWord(word, actionType, range) {
var isTtsOn = keyDownList[setting["TTSWhen"]];
applyReleaseKeydownList();

// skip if mouse target is tooltip
//hide tooltip, if activated word exist and current word is none
//do nothing, if no new word or no word change, or if isTooltipOn isTtsOn both are down
//if isTooltipOn is off, hide tooltip
if (checkMouseTargetIsTooltip()) {
// skip if mouse target is tooltip or no text, if no new word
// hide tooltip, if no text
// if tooltip is off, hide tooltip
if (checkMouseTargetIsTooltip() || activatedWord == word) {
return;
} else if (!word && activatedWord) {
} else if (!word) {
activatedWord = word;
hideTooltip();
return;
} else if (activatedWord == word || !word) {
return;
} else if (!isTooltipOn) {
hideTooltip();
}
Expand Down Expand Up @@ -368,6 +365,8 @@ function getWritingText() {
function loadEventListener() {
//use mouse position for tooltip position
addEventHandler("mousemove", handleMousemove);
addEventHandler("touchstart", handleTouchstart);

//detect activation hold key pressed
addEventHandler("keydown", handleKeydown);
addEventHandler("keyup", handleKeyup);
Expand All @@ -390,6 +389,10 @@ function handleMousemove(e) {
checkMouseTargetIsYoutubeSubtitle();
}

function handleTouchstart(e) {
mouseMoved = true;
}

function handleKeydown(e) {
//if user pressed ctrl+f ctrl+a, hide tooltip
if (/KeyA|KeyF/.test(e.code) && e.ctrlKey) {
Expand Down Expand Up @@ -633,17 +636,13 @@ async function detectPDF() {
setting["detectPDF"] == "true" &&
document?.body?.children?.[0]?.type == "application/pdf"
) {
checkPdfError();
addPdfListener();
openPdfIframe(window.location.href);
}
}
function checkPdfError() {
function addPdfListener() {
//if pdf not working message come, try open using blob url
window.addEventListener("message", function (event) {
if (event?.data?.type == "documenterror") {
openPdfIframeBlob();
}
});
util.addFrameListener("pdfErrorLoadDocument", openPdfIframeBlob);
}

async function openPdfIframeBlob() {
Expand Down Expand Up @@ -721,37 +720,24 @@ async function checkYoutube() {
isYoutubeOn = true;
await util.injectScript("youtube.js");
initYoutubePlayer();
addCaptionButtonListener();
addCaptionOnOffListener();
}

async function addCaptionButtonListener() {
await waitUntil(() => $(".ytp-subtitles-button").get(0));

$(".ytp-subtitles-button").on("click", (e) => {
handleCaptionOnOff();
});
$(document).on("keydown", (e) => {
if (e.code == "KeyC") {
handleCaptionOnOff();
}
function addCaptionOnOffListener() {
util.addFrameListener("youtubeCaptionOnOff", ({ captionOnStatusByUser }) => {
setting["captionOnStatusByUser"] = captionOnStatusByUser;
setting.save();
});
}

function handleCaptionOnOff() {
var captionOnStatusByUser = $(".ytp-subtitles-button").attr("aria-pressed");
setting["captionOnStatusByUser"] = captionOnStatusByUser;
setting.save();
}

function pausePlayer() {
util.postMessage({ type: "pausePlayer" });
util.postFrame({ type: "pausePlayer" });
}
function playPlayer() {
util.postMessage({ type: "playPlayer" });
util.postFrame({ type: "playPlayer" });
}
function initYoutubePlayer() {
if (isYoutubeOn) {
util.postMessage({
util.postFrame({
type: "initYoutubePlayer",
targetLang: setting["translateTarget"],
subSetting: setting["enableYoutube"],
Expand Down
8 changes: 4 additions & 4 deletions src/ocr/ocrIframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ async function loadScheduler(lang, mode) {
return schedulerList[lang + "_" + mode];
}

function response(data) {
util.postMessage(data);
}

function initTesseract(request) {
loadScheduler(request.lang, "auto");
loadScheduler(request.lang, "bbox");
Expand All @@ -157,3 +153,7 @@ function initTesseract(request) {
windowPostMessageProxy: request.windowPostMessageProxy,
});
}

function response(data) {
util.postFrame(data);
}
3 changes: 1 addition & 2 deletions src/ocr/ocrView.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export async function checkImage(img, setting, keyDownList) {
// if mouse target is not image, skip
// if already ocr processed,skip
if (
(setting["keyDownOCR"] != "always" &&
!keyDownList[setting["keyDownOCR"]]) ||
!keyDownList[setting["keyDownOCR"]] ||
!checkIsImage(img) ||
ocrHistory[img.src]
) {
Expand Down
14 changes: 9 additions & 5 deletions src/pdf/pdfInject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import delay from "delay";
import $ from "jquery";

import * as util from "/src/util";

// <link rel="stylesheet" href="../../tippy.css" />
// <script src="../../contentScript.js"></script>
// <script src="../../pdfInject.js"></script>
Expand Down Expand Up @@ -69,7 +71,7 @@ function checkPdfError() {
PDFViewerApplication.initializedPromise.then(function () {
PDFViewerApplication.eventBus.on("documenterror", function (event) {
console.log(event);
window.top.postMessage({ type: "documenterror" }, "*");
util.postFrame({ type: "pdfErrorLoadDocument" });
});
});
});
Expand All @@ -85,6 +87,8 @@ function waitUntilPdfLoad() {
async function addSpaceBetweenPdfText() {
var prevY;
var prevLine;
var newLineScale = 1.5;
var spaceScale = 1.0;

// remove all br
$("br").remove();
Expand All @@ -106,13 +110,13 @@ async function addSpaceBetweenPdfText() {
//skip if already has space
if (prevLine && !/[\n ]$/.test(prevLine.textContent)) {
if (
prevY < lineY - lineFontSize * 2 ||
lineY + lineFontSize * 2 < prevY
prevY < lineY - lineFontSize * newLineScale ||
lineY + lineFontSize * newLineScale < prevY
) {
prevLine.textContent += "\n";
} else if (
prevY < lineY - lineFontSize ||
lineY + lineFontSize < prevY
prevY < lineY - lineFontSize * spaceScale ||
lineY + lineFontSize * spaceScale < prevY
) {
prevLine.textContent += " ";
}
Expand Down
43 changes: 41 additions & 2 deletions src/subtitle/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
// restart playersubtitle for apply

import { XMLHttpRequestInterceptor } from "@mswjs/interceptors/XMLHttpRequest";
import { waitUntil } from "async-wait-until";
import { waitUntil, WAIT_FOREVER } from "async-wait-until";
import { debounce } from "throttle-debounce";
import $ from "jquery";
import delay from "delay";

var interceptor = new XMLHttpRequestInterceptor();
var interceptorLoaded = false;
var captionButtonListenerLoaded = false;
var targetLang = "";
var subSetting = "";
var pausedByExtension = false;
Expand Down Expand Up @@ -43,6 +46,7 @@ async function initPlayer(data) {
addPlayerStartListener();
addUrlListener();
activateCaption();
addCaptionButtonListener();

//if not embed, load interceptor directly else load when start video // embed has interceptor conflict
if (!isEmbed(window.location.href)) {
Expand Down Expand Up @@ -81,6 +85,39 @@ function addUrlListener() {
});
}

async function addCaptionButtonListener() {
if (captionButtonListenerLoaded == true) {
return;
}
captionButtonListenerLoaded = true;

await waitUntil(() => $(".ytp-subtitles-button").get(0));

$(".ytp-subtitles-button").on("click", (e) => {
handleCaptionOnOff();
});
$(document).on("keydown", (e) => {
if (e.code == "KeyC") {
handleCaptionOnOff();
}
});
}

async function handleCaptionOnOff() {
await delay(200);
captionOnStatusByUser = $(".ytp-subtitles-button").attr("aria-pressed");
postFrame({ type: "youtubeCaptionOnOff", captionOnStatusByUser });
}

function postFrame(data) {
if (self == top) {
window.postMessage(data, "*");
} else {
window.postMessage(data, "*");
window.parent.postMessage(data, "*");
}
}

//intercept sub request ===================================================================
// check any subtitle request and concat dual sub
function loadInterceptor() {
Expand Down Expand Up @@ -142,7 +179,9 @@ function handlePlayerAll(callbackFn) {
}

async function waitVideoPlayerLoad() {
await waitUntil(() => getVideoPlayer());
await waitUntil(() => getVideoPlayer(), {
timeout: WAIT_FOREVER,
});
}

async function pausePlayer() {
Expand Down
12 changes: 11 additions & 1 deletion src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,8 @@ export function getEbookIframe() {

// browser Listener handler========================

export function postMessage(data) {
//from body to parent or iframe message
export function postFrame(data) {
if (self == top) {
window.postMessage(data, "*");
} else {
Expand All @@ -924,6 +925,15 @@ export function postMessage(data) {
}
}

export function addFrameListener(type, handler) {
window.addEventListener("message", function (event) {
if (event?.data?.type == type) {
handler(event?.data);
}
});
}

//fron content script to background
export async function sendMessage(message) {
try {
return await browser.runtime.sendMessage(message);
Expand Down

0 comments on commit 1a743c7

Please sign in to comment.