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

Fix search pages clipboard monitor #190

Closed
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@
"serializeError": "readonly",
"deserializeError": "readonly",
"isObject": "readonly",
"document": "readonly",
"stringReverse": "readonly",
"promiseTimeout": "readonly",
"escapeRegExp": "readonly",
Expand Down
38 changes: 37 additions & 1 deletion ext/js/comm/clipboard-monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/


class ClipboardMonitor extends EventDispatcher {
constructor({japaneseUtil, clipboardReader}) {
super();
Expand All @@ -30,6 +31,23 @@ class ClipboardMonitor extends EventDispatcher {
start() {
this.stop();

// Continuously selecting and focusing on an element makes the search
// Function is unusable manually this code disables the function
// If the mouse is over the content
const content = document.querySelector('.content-body-inner');

let isMouseAbove = false;

content.addEventListener('mouseover', handleMouseover);
content.addEventListener('mouseleave', handleMouseleave);

function handleMouseover() {
isMouseAbove = true;
}

function handleMouseleave() {
isMouseAbove = false;
}
// The token below is used as a unique identifier to ensure that a new clipboard monitor
// hasn't been started during the await call. The check below the await call
// will exit early if the reference has changed.
Expand All @@ -40,7 +58,9 @@ class ClipboardMonitor extends EventDispatcher {

let text = null;
try {
text = await this._clipboardReader.getText(false);
if (isMouseAbove) { text = this._previousText; } else {
text = this.paste();
}
} catch (e) {
// NOP
}
Expand All @@ -66,6 +86,22 @@ class ClipboardMonitor extends EventDispatcher {
intervalCallback();
}

paste() {
const previouslyFocusedElement = document.activeElement;
const ta = document.createElement('textarea');
ta.style.cssText =
'opacity:0; position:fixed; width:1px; height:1px; top:0; left:0;';
document.body.appendChild(ta);

ta.focus();
ta.select();
document.execCommand('paste');
const a = ta.value;
ta.remove();
previouslyFocusedElement.focus();

return a;
}
stop() {
this._timerToken = null;
this._previousText = null;
Expand Down
4 changes: 2 additions & 2 deletions ext/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ <h1>Yomitan Settings</h1>
</div>
</div>
<div class="settings-group">
<div class="settings-item"><div class="settings-item-inner">
<!-- <div class="settings-item"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-invalid-indicator"></div>
<div class="settings-item-label">Enable background clipboard text monitoring</div>
Expand All @@ -1936,7 +1936,7 @@ <h1>Yomitan Settings</h1>
<div class="settings-item-right">
<label class="toggle"><input type="checkbox" class="permissions-toggle" data-permissions-setting="clipboard.enableBackgroundMonitor" data-required-permissions="clipboardRead"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
</div>
</div></div>
</div></div> -->
<div class="settings-item"><div class="settings-item-inner">
<div class="settings-item-left">
<div class="settings-item-invalid-indicator"></div>
Expand Down