This is an attempt to write a Chrome extension for Gmail that can capture the user selection for further processing.
document.addEventListener('selectionchange')
only works for the email body but does not work
for the embed Gmail PDF reader.
By inspecting the Gmail PDF reader, I tried a different way to retrieve user selection.
The current PDF reader in Gmail is in the following structure:
<html>
<body>
...
<div role="document">
<div> // page container, contains one page
<div> // link container, contains all clickable links
<a href="">link1</a>
<a href="">link2</a>
...
</div>
<div> // text container, contains all text
<h2>text1</h2>
<p style="top: a%; left: b%; width: a%; height: d%;">text2</p>
<p style="top: a%; left: b%; width: a%; height: d%;">text3</p>
...
</div>
<div></div> // not sure what it is
<div> // selection continer, contains selections
<div style="top: a%; left: b%; width: a%; height: d%;"></div>
</div>
...
</div>
... // other pages
</body>
</html>
When a user opens a PDF, the Gmail JS insert <div role="document" />
into the page.
The defined document.addEventListener('click')
the evaluate the click-event target.
When a user made a selection, the click-event target is the page container.
The remaining logic compares the style
attribute of the of all children inside
the selection container with all the children inside the text container. If the
area overlaps, the text of the text container child is the selected text.
Please note the selection div
is created after the click-event, so in the logic
it implemented a 100ms setTimeout
.