-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Update manifest - Switch to newer APIs - Patch chrome stub with new APIs for testing - Move clipboard and audio functionality into offscreen page (requires Chrome 109) - rikaikun now remembers on state between startups; required due to ephemeral nature of new scripts. BREAKING CHANGE: MV3 with offscreen pages requires at least Chrome 109 which is the new minimum version. Fixes #187 Fixes #65
- Loading branch information
Showing
17 changed files
with
921 additions
and
515 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function copyToClipboard(text: string) { | ||
const textEl = document.querySelector('#text'); | ||
if (textEl === null) { | ||
throw new TypeError('Textarea for clipboard use not defined.'); | ||
} | ||
if (!(textEl instanceof HTMLTextAreaElement)) { | ||
throw new TypeError('#text element in offscreen doc not text area.'); | ||
} | ||
// `document.execCommand('copy')` works against the user's selection in a web | ||
// page. As such, we must insert the string we want to copy to the web page | ||
// and to select that content in the page before calling `execCommand()`. | ||
textEl.value = text; | ||
textEl.select(); | ||
document.execCommand('copy'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export async function setupOffscreenDocument() { | ||
// A simple try catch is easier to understand though perhaps less forward compatible. | ||
// See https://groups.google.com/a/chromium.org/g/chromium-extensions/c/D5Jg2ukyvUc. | ||
try { | ||
await chrome.offscreen.createDocument({ | ||
url: 'offscreen.html', | ||
reasons: [ | ||
chrome.offscreen.Reason.AUDIO_PLAYBACK, | ||
chrome.offscreen.Reason.CLIPBOARD, | ||
], | ||
justification: | ||
'Copying word definitions to clipboard. Playing audio using TTS of the selected word.', | ||
}); | ||
} catch (error) { | ||
let message; | ||
if (error instanceof Error) { | ||
message = error.message; | ||
} else { | ||
message = String(error); | ||
} | ||
if (!message.startsWith('Only a single offscreen')) { | ||
throw error; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<html> | ||
<head> | ||
<script type="module" src="offscreen.js"></script> | ||
</head> | ||
<body> | ||
<textarea id="text"></textarea> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { copyToClipboard } from './clipboard'; | ||
import { tts } from './texttospeech'; | ||
|
||
chrome.runtime.onMessage.addListener(handleMessages); | ||
|
||
let timeoutId = 0; | ||
function handleMessages(message: { | ||
target: string; | ||
type: string; | ||
text: string; | ||
}): void { | ||
if (message.target !== 'offscreen') { | ||
return; | ||
} | ||
clearTimeout(timeoutId); | ||
try { | ||
switch (message.type) { | ||
case 'copyToClipboardOffscreen': | ||
// Error if we received the wrong kind of data. | ||
if (typeof message.text !== 'string') { | ||
throw new TypeError( | ||
`Value provided must be a 'string', got '${typeof message.text}'.` | ||
); | ||
} | ||
copyToClipboard(message.text); | ||
break; | ||
case 'playTtsOffscreen': | ||
tts.play(message.text); | ||
break; | ||
} | ||
} finally { | ||
timeoutId = window.setTimeout(window.close, 30000); | ||
} | ||
} |
Oops, something went wrong.