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

#7324: Automatically use the focused document to copy to the clipboard #7635

Merged
merged 14 commits into from
Feb 19, 2024

Conversation

fregante
Copy link
Contributor

@fregante fregante commented Feb 16, 2024

What does this PR do?

Demo

The demo shows in order:

  1. Regular in-document copy
  2. copy from sidePanel (the sidePanel is focused)
  3. Copy interactively when no document is focused (the URL bar is focused)
  4. copy via sidePanel (the sidePanel is focused)
Screen.Recording.4.mov

Checklist

  • Add tests
  • New files added to src/tsconfig.strictNullChecks.json (if possible)
  • Designate a primary reviewer: @grahamlangford

Copy link

codecov bot commented Feb 17, 2024

Codecov Report

Attention: 43 lines in your changes are missing coverage. Please review.

Comparison is base (de40517) 72.90% compared to head (60c094a) 72.85%.

Files Patch % Lines
src/background/clipboard.ts 0.00% 19 Missing ⚠️
src/utils/clipboardUtils.ts 70.00% 9 Missing ⚠️
src/utils/promiseUtils.ts 0.00% 6 Missing ⚠️
src/activation/ActivationLink.tsx 50.00% 1 Missing ⚠️
src/bricks/effects/clipboard.ts 75.00% 1 Missing ⚠️
src/components/jsonTree/JsonTree.tsx 50.00% 1 Missing ⚠️
src/components/jsonTree/treeHooks.tsx 50.00% 1 Missing ⚠️
src/contentScript/contentScriptCore.ts 0.00% 1 Missing ⚠️
...ole/pages/brickEditor/referenceTab/BrickDetail.tsx 50.00% 1 Missing ⚠️
...sole/pages/integrations/ZapierIntegrationModal.tsx 0.00% 1 Missing ⚠️
... and 2 more
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.
📢 Have feedback on the report? Share it here.

@@ -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",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer needed somehow

Comment on lines +52 to +55
// 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(
Copy link
Contributor Author

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.

@fregante fregante changed the title #7324: Automatically use the focused document to copy to the clipboard (WIP) #7324: Automatically use the focused document to copy to the clipboard Feb 18, 2024
* 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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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,
Copy link
Contributor Author

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",
);
}
Copy link
Contributor Author

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";
Copy link
Contributor Author

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.


void initMessengerLogging();
void initRuntimeLogging();
void initPerformanceMonitoring();
watchNavigation();
initToaster();
markDocumentAsFocusableByUser();
Copy link
Contributor Author

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

  1. Click "Copy after 1 second" menu item on page
  2. Focus the page editor within a second
  3. 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

interface MessengerMethods {
WRITE_TO_CLIPBOARD: typeof nonInteractivelyWriteToClipboard;
}
}
Copy link
Contributor Author

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

@fregante fregante marked this pull request as ready for review February 18, 2024 08:12

// 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@twschiller twschiller merged commit c06f703 into main Feb 19, 2024
18 checks passed
@twschiller twschiller deleted the F/sidebar/clipboard branch February 19, 2024 01:15
@twschiller twschiller added this to the 1.8.10 milestone Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Copy to Clipboard focus issue in MV3 Side Panel
3 participants