-
Notifications
You must be signed in to change notification settings - Fork 25
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
#7324: Automatically use the focused document to copy to the clipboard #7635
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #7635 +/- ##
==========================================
- Coverage 72.90% 72.85% -0.05%
==========================================
Files 1233 1234 +1
Lines 38356 38404 +48
Branches 7196 7209 +13
==========================================
+ Hits 27963 27981 +18
- Misses 10393 10423 +30 ☔ View full report in Codecov by Sentry. |
because it's also used to message other targets
@@ -74,8 +74,6 @@ const knipConfig = { | |||
"@types/gapi.client.oauth2-v2", | |||
// Used by Code Editor so format on save matches pre-commit behavior | |||
"prettier", | |||
// Used by eslint-local-rules/noCrossBoundaryImports.js | |||
"multimatch", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed somehow
// TODO: After the MV3 migration, just use chrome.offscreen instead | ||
// https://developer.chrome.com/blog/Offscreen-Documents-in-Manifest-v3 | ||
// https://github.com/GoogleChrome/chrome-extensions-samples/tree/73265836c40426c004ac699a6e19b9d56590cdca/functional-samples/cookbook.offscreen-clipboard-write | ||
export default async function writeToClipboardInFocusedContext( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future API alert 🙌
The current code already works in MV3, but presumably that API will avoid the need to track focus and other guesswork.
* return a uniquely-idenfying Target usable by the Messenger. | ||
* It returns `undefined` for tab-less HTTP senders; they must be either in tabs or chrome-extension:// pages. | ||
*/ | ||
function extractTargetFromSender( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be extracted
// https://developer.chrome.com/blog/Offscreen-Documents-in-Manifest-v3 | ||
// https://github.com/GoogleChrome/chrome-extensions-samples/tree/73265836c40426c004ac699a6e19b9d56590cdca/functional-samples/cookbook.offscreen-clipboard-write | ||
export default async function writeToClipboardInFocusedContext( | ||
item: ClipboardText, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the base64-to-blob parsing/validation is done a bit too early, this currently only supports copying text. Image copying will still use the modal if necessary
throw new BusinessError( | ||
"Only PNG images are supported by the browser clipboard API", | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every browser only supports image/text
, image/html
and image/png
at the moment.
@@ -23,7 +23,7 @@ import { Form, InputGroup } from "react-bootstrap"; | |||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | |||
import { faCopy } from "@fortawesome/free-solid-svg-icons"; | |||
import AsyncButton from "@/components/AsyncButton"; | |||
import { writeTextToClipboard } from "@/utils/clipboardUtils"; | |||
import { writeToClipboard } from "@/utils/clipboardUtils"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no need for a dedicated method. Content detection can be done in one place. Before this change, text was being passed around as string or as ClipboardItem indiscriminately. This change just makes the conversion at the last moment so that text can be serialized easily.
src/pageEditor/pageEditor.tsx
Outdated
|
||
void initMessengerLogging(); | ||
void initRuntimeLogging(); | ||
void initPerformanceMonitoring(); | ||
watchNavigation(); | ||
initToaster(); | ||
markDocumentAsFocusableByUser(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dropped this because, as one comment suggested, the dev tools don't support it.
Repro
- Click "Copy after 1 second" menu item on page
- Focus the page editor within a second
- See modal dialog appear on page after failure
Log
This is the unified background <-> page editor
communication log. Notice how it responds with false
after a failure.
![Screenshot 2](https://private-user-images.githubusercontent.com/1402241/305693812-7f779de8-14e6-403c-a4c1-adc817e8139e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyMTkxNjYsIm5iZiI6MTczOTIxODg2NiwicGF0aCI6Ii8xNDAyMjQxLzMwNTY5MzgxMi03Zjc3OWRlOC0xNGU2LTQwM2MtYTRjMS1hZGM4MTdlODEzOWUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxMCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTBUMjAyMTA2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9YjM3NDVkMGY5MDkzMzJkZjQwMTJiZmE5MWZlZGQxYjc2ZWIwZjVhOTI5YzU1NDU3ZDRhNjc0M2NjMGEyYmQzMyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.Li4zAiP65I5ATG9gRB3MHtmfhMW_gLbQCvg90Yxn6qs)
interface MessengerMethods { | ||
WRITE_TO_CLIPBOARD: typeof nonInteractivelyWriteToClipboard; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explained in the code below
|
||
// Just like chrome.storage.session, this must be "global" | ||
// eslint-disable-next-line local-rules/persistBackgroundData -- MV2-only | ||
const storage = new Map<ManualStorageKey, JsonValue>(); | ||
|
||
// eslint-disable-next-line local-rules/persistBackgroundData -- Static | ||
const hasSession = "session" in chrome.storage; | ||
function validateContext(): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
What does this PR do?
Demo
The demo shows in order:
copy
copy
fromsidePanel
(thesidePanel
is focused)copy
viasidePanel
(thesidePanel
is focused)Screen.Recording.4.mov
Checklist
src/tsconfig.strictNullChecks.json
(if possible)