Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle content in iframes #90

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 66 additions & 6 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ let savedRangeOffset;

let selText;

let selDoc;

let clientX;

let clientY;
Expand All @@ -66,6 +68,12 @@ let selStartDelta;

let selStartIncrement;

let observer;

let iframe;

let ownerDocument;

let popX = 0;

let popY = 0;
Expand All @@ -86,12 +94,53 @@ let zwnj = /\u200c/g;
function enableTab() {
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('keydown', onKeyDown);

let iframes = document.getElementsByTagName('iframe');
if (iframes) {
for (let iframe of iframes) {
iframe.contentDocument.addEventListener('mousemove', onMouseMove);
iframe.contentDocument.addEventListener('keydown', onKeyDown);
}

observer = new MutationObserver((mutationsList, observer) => {
for (let mutation of mutationsList) {
if (mutation.addedNodes) {
for (let node of mutation.addedNodes) {
if (node.nodeType === 1 && node.nodeName === 'IFRAME') {
node.addEventListener('load', (event) => {
node.contentDocument
.addEventListener('mousemove', onMouseMove);
node.contentDocument
.addEventListener('keydown', onKeyDown);
});
}
}
}
}
});

observer.observe(document.body, {
childList: true,
subtree: true
});
}

}

function disableTab() {
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('keydown', onKeyDown);

if (observer) {
observer.disconnect();

document.getElementsByTagName('iframe')
.forEach(iframe => {
iframe.contentDocument.removeEventListener('mousemove', onMouseMove);
iframe.contentDocument.removeEventListener('keydown', onKeyDown);
});
}

let popup = document.getElementById('zhongwen-window');
if (popup) {
popup.parentNode.removeChild(popup);
Expand Down Expand Up @@ -381,6 +430,10 @@ function onKeyDown(keyDown) {
}

function onMouseMove(mouseMove) {
ownerDocument = mouseMove.target.ownerDocument;

iframe = ownerDocument.defaultView.frameElement;

if (mouseMove.target.nodeName === 'TEXTAREA' || mouseMove.target.nodeName === 'INPUT'
|| mouseMove.target.nodeName === 'DIV') {

Expand Down Expand Up @@ -415,15 +468,15 @@ function onMouseMove(mouseMove) {
let rangeOffset;

// Handle Chrome and Firefox
if (document.caretRangeFromPoint) {
range = document.caretRangeFromPoint(mouseMove.clientX, mouseMove.clientY);
if (ownerDocument.caretRangeFromPoint) {
range = ownerDocument.caretRangeFromPoint(mouseMove.clientX, mouseMove.clientY);
if (range === null) {
return;
}
rangeNode = range.startContainer;
rangeOffset = range.startOffset;
} else if (document.caretPositionFromPoint) {
range = document.caretPositionFromPoint(mouseMove.clientX, mouseMove.clientY);
} else if (ownerDocument.caretPositionFromPoint) {
range = ownerDocument.caretPositionFromPoint(mouseMove.clientX, mouseMove.clientY);
if (range === null) {
return;
}
Expand Down Expand Up @@ -610,6 +663,11 @@ function getTextFromSingleNode(node, selEndList, maxLength) {
}

function showPopup(html, elem, x, y, looseWidth) {
if (iframe) {
let rect = iframe.getBoundingClientRect();
x += rect.x;
y += rect.y;
}

if (!x || !y) {
x = y = 0;
Expand Down Expand Up @@ -747,12 +805,13 @@ function highlightMatch(doc, rangeStartNode, rangeStartOffset, matchLen, selEndL
range.setStart(rangeStartNode, rangeStartOffset);
range.setEnd(selEnd.node, offset);

let sel = window.getSelection();
let sel = doc.getSelection();
if (!sel.isCollapsed && selText !== sel.toString())
return;
sel.empty();
sel.addRange(range);
selText = sel.toString();
selDoc = doc;
}

function clearHighlight() {
Expand All @@ -761,11 +820,12 @@ function clearHighlight() {
return;
}

let selection = window.getSelection();
let selection = selDoc.getSelection();
if (selection.isCollapsed || selText === selection.toString()) {
selection.empty();
}
selText = null;
selDoc = null;
}

function isVisible() {
Expand Down