1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
type MessageSender = chrome.runtime.MessageSender;
let AudioAndCopyHandlerObject;
// Number of milliseconds to wait after requesting a clipboard read
// before clipboard change and paste events are ignored.
const kClipboardReadMaxDelayMs = 1000;
// Number of milliseconds to wait after requesting a clipboard copy
// before clipboard copy events are ignored, used to clear the clipboard
// after reading data in a paste event.
const kClipboardClearMaxDelayMs = 500;
class AudioAndCopyHandler {
private audioElement_: HTMLAudioElement;
private lastClearClipboardDataTime_: Date;
private lastReadClipboardDataTime_: Date;
constructor() {
this.audioElement_ = new Audio('earcons/null_selection.ogg');
/**
* The timestamp at which the last clipboard data clear was requested.
* Used to make sure we don't clear the clipboard on a user's request,
* but only after the clipboard was used to read selected text.
*/
this.lastClearClipboardDataTime_ = new Date(0);
/**
* The timestamp at which clipboard data read was requested by the user
* doing a "read selection" keystroke on a Google Docs app. If a
* clipboard change event comes in within kClipboardReadMaxDelayMs,
* Select-to-Speak will read that text out loud.
*/
this.lastReadClipboardDataTime_ = new Date(0);
document.addEventListener('paste', evt => {
this.onClipboardPaste_(evt);
});
document.addEventListener('copy', evt => {
this.onClipboardCopy_(evt);
});
// Handle messages from the service worker.
chrome.runtime.onMessage.addListener(
(message: any|undefined, _sender: MessageSender,
_sendResponse: (value: any) => void) => {
switch (message['command']) {
case 'playNullSelectionTone':
this.audioElement_.play();
break;
case 'updateLastReadClipboardDataTime':
this.lastReadClipboardDataTime_ = new Date();
break;
case 'clipboardDataChanged':
this.onClipboardDataChanged_();
break;
}
return false;
});
}
private onClipboardDataChanged_(): void {
if (new Date().getTime() - this.lastReadClipboardDataTime_.getTime() <
kClipboardReadMaxDelayMs) {
// The data has changed, and we are ready to read it.
// Get it using a paste.
document.execCommand('paste');
}
}
private onClipboardPaste_(evt: ClipboardEvent): void {
if (new Date().getTime() - this.lastReadClipboardDataTime_.getTime() <
kClipboardReadMaxDelayMs) {
// Read the current clipboard data.
evt.preventDefault();
this.lastReadClipboardDataTime_ = new Date(0);
// Clear the clipboard data by copying nothing (the current document).
// Do this in a timeout to avoid a recursive warning per
// https://crbug.com/363288.
setTimeout(() => this.clearClipboard_(), 0);
// @ts-ignore: TODO(crbug.com/270623046): clipboardData can be null.
let content = evt.clipboardData.getData('text/plain');
chrome.runtime.sendMessage(undefined, {
command: 'paste',
content,
});
}
}
private onClipboardCopy_(evt: ClipboardEvent): void {
if (new Date().getTime() - this.lastClearClipboardDataTime_.getTime() <
kClipboardClearMaxDelayMs) {
// onClipboardPaste has just completed reading the clipboard for speech.
// This is used to clear the clipboard.
// @ts-ignore: TODO(crbug.com/270623046): clipboardData can be null.
evt.clipboardData.setData('text/plain', '');
evt.preventDefault();
this.lastClearClipboardDataTime_ = new Date(0);
}
}
private clearClipboard_(): void {
this.lastClearClipboardDataTime_ = new Date();
document.execCommand('copy');
}
}
document.addEventListener('DOMContentLoaded', () => {
AudioAndCopyHandlerObject = new AudioAndCopyHandler();
});
|