piroor / copy-selected-tabs-to-clipboard

Provides ability to copy title and URL of selected tabs to the clipboard for Firefox 63 and later.
Other
75 stars 15 forks source link

Rich text copy not working for Google Docs #10

Open gwicke opened 4 years ago

gwicke commented 4 years ago

Short description

Copying the rich text linked title does not work for fully loaded Google Docs tabs, but does work for unloaded ones, and other websites. Other formats (such as separate title & link) work as well.

Steps to reproduce

  1. Start Firefox with clean profile.
  2. Install copy-selected-tabs-to-clipboard
  3. Add a Rich Text (%RT%) option, grant the "use page contents" permission.
  4. Right click on a fully loaded Google Docs tab, and copy as Rich Text.
  5. Paste link into anything accepting rich text content (ex: Google Docs)

Expected result

Tab title is copied as linked, rich text.

Actual result

Clipboard was not updated, previous clipboard content is inserted.

Environment

piroor commented 4 years ago

I've tried this with the latest development build but couldn't reproduce it. Could you try it via about:debugging?: https://piro.sakura.ne.jp/xul/xpi/nightly/copy-selected-tabs-to-clipboard.xpi

Steps I did:

  1. Start Nightly 79.0a1 on Windows 10.
  2. Load the latest development version of this addon via about:debugging.
  3. Prepare two formats and grant the required permission:
    • HTML Link: %RT%<a title="%TITLE_HTML%" href="%URL_HTML%">%TITLE_HTML%</a>
    • Rich text link: %RT%
  4. Go to Google Docs.
  5. Create a blank new document.
  6. Select all tabs and copy as "HTML Link".
  7. Paste the clipboard contents to the document created at 5.
  8. Select all tabs and copy as "Rich text link".
  9. Paste the clipboard contents to the document created at 5.

On both steps 7 and 9 I successfully got inserted links. If you still fail, I think it may be a problem of Firefox or the platform itself...

gwicke commented 4 years ago

Retried on Mozilla/5.0 (X11; Linux x86_64; rv:77.0) Gecko/20100101 Firefox/77.0.

Neither the nightly, nor the released version of the extension are copying the %RT% format for Google docs tabs, while other tabs (and other link formats on Google Docs) are working fine.

I stepped through the copy process in the debugger, and the richText HTML string looks correct to me. The execution hits the browser.tabs.executeScript(permittedTabs[0].id path, and tries to inject the event based copy code into the gdocs page, but the clipboard is not updated.

piroor commented 4 years ago

OK I got it why the difference between my result and your result happens. On my case I select multiple tabs including both Google Docs and other services, and the first tab of them is not Google Docs. On the other hand your case looks to have Google Docs as the first tab.

Due to some technical reasons, this addon executes document.execCommand() and handles it with its own custom event listener, in the first tab of the selection. If you have something another webpage as the first selected tab, the operation succeeds. On the other hand, if the first tab shows Google Docs, document.execCommand() is executed but the event never been handled by the custom event listener. It possibly that such events have been handled and canceled by scripts of the Google Docs service itself.

There is only one workaround: enabling the native clipboard write API of Firefox itself. Go to about:config and turn dom.events.asyncClipboard.dataTransfer to true, then the API becomes available and this addon will use it to copy rich text data without script execution in webpages. (But I've found that there is a mistake and you need to wait a next release including the commit fc80555.)

piroor commented 4 years ago

I've released the version 1.3.0 with the fix.

gwicke commented 4 years ago

Confirmed that rich text copying is now working for gdocs tabs as well with the preference set. Thank you!

One small nit: When the preference is missing, I am getting the HTML string in the clipboard (with <a> tags), rather than the plain text format. Might be nicer to fall back to the user configured plain text format instead.