Skip to content

Firefox add-on to copy tooltip text or some other text attributes

License

Notifications You must be signed in to change notification settings

maxnikulin/alt-copy

Repository files navigation

AltCopy Firefox Add-on

Copy tooltip text

A helper to copy link title, “alt” image description, full timestamp, etc.

Install AltCopy browser extension from addons.mozilla.org

alt-copy-screenshot.png

Some web pages are too aggressive trying to be friendly to visitors. You may want to copy some text snippet to your notes but it has date formatted as “3 hours ago” that will be completely useless several days, months, or even years later. Full date may be shown in tooltip but it is not easy to copy it. “Inspect” context menu option is a rescue but it requires enough mouse clicks to obtain text from browser developer tools. This extension adds context menu entry to copy text from “title” attribute of HTML element directly.

The extension adds the “Copy alt text” context menu item. Use mouse right click to open context menu for specific element of a web page. Another option is [Menu] or [Shift+F10] keyboard shortcuts that is useful when an element is highlighted in response to the “Find in page…” action ([Ctrl+F], /) or when keyboard or cursor navigation (caret browsing) is enabled ([F7] or the accessibility.browsewithcaret setting). The limitation is that images and some other elements can not be selected from keyboard. Do not be confused by context menu opened in the top left corner (Firefox bug #1426671), the add-on makes some efforts to find the current element.

Other options are tried if there is no “title” attribute:

  • alt attribute mostly used for images,
  • chosen option of <select> element,
  • value of <input> element that is suitable for date, time, color, range, etc. or its placeholder,
  • text of an element when selection is suppressed through CSS,
  • selected text, link target, or image source if nothing “better” is available.

The extension works silently. You can grant the notifications permission in the add-ons manager to enable desktop notification popups when extracting text or clipboard operations fail.

The extension tries to protect you from control characters in the text that may cause inserting of other text than you expect. There is no guarantee that such protection is reliable, so beware malicious sites. It seems modern bash versions have “bracketed paste” enabled by default to mitigate the issue. See Stack Exchange question from the security hub ”How can I protect myself from this kind of clipboard abuse?” or recommendations to web developers in Security Considerations section of Flask framework documentation Copy/Paste to Terminal.

At first I found Copy Element’s text extension, source: https://github.com/utubo/firefox-copy_elements_text It rates text sources to copy in a different way. I borrowed some ideas from it though. Implementation details differ as well.

Do not expect that any add-on can access content of internal browser pages like preferences, extension manager, or others ones listed on the about:about page. Another restriction is privileged pages like https://addons.mozilla.org/.

Firefox 102 ESR may request clipboard permission when invoked on a protected page. The only thing it could offer is to copy link address or image URL. It is not necessary for recent Firefox releases.

The extension used to request access to specific sites when context menu is opened in a subframe. Currently it is not doing so, but such behavior may be restored in future. You may enable access to all sites, see how to manage optional permissions.

Permissions

As usually, add-on requires some permission to do its job. Open about:addons page (Add-ons item in the hamburger menu, [Ctrl+Shift+A]) and choose this add-on to grant or to revoke optional permissions.

See Permission request messages for Firefox extensions for details.

Display notifications to you (notifications)

An optional permission. When granted, it allows the extension to display a message in the case of failure.

Access your data for all websites (<all_urls>)

May be necessary to extract tooltips from frames. Try it if you experienced issues.

The activeTab permission grants access to the top level tab page, not to frames. While manifest v2 add-ons in Firefox have access to frames loaded from other sites (cross-origin frames), there are some corner cases when the permission may be helpful. For security reasons scripts can not be injected into dynamically created frames (Firefox bug #1396399) or when a tab is restored from cache (Firefox bug #1837336).

Even having this permission the add-on may fail inside some special frames (Firefox bug #1411641 comment 41).

This permission is rather dangerous and gives extensions a lot of power.

Input data to the clipboard (clipboardWrite)

This permission is not required in Firefox-113, however in Firefox-102 the add-on may fail to put text to clipboard if you open context menu using keyboard shortcut ([Menu] or [Shift+F10]), in the case of delay between opening menu and selecting the item longer than 5 seconds, when menu is invoked for a subframe, or if you are trying to copy anything from a privileged page.

User action context is propagated to add-on background pages and timeout is counted from selection of menu item in Firefox-113, so the extension may work without the clipboardWrite permission.

This permission allows to put data to clipboard at any moment, not only in response to user action. That is why I am trying to avoid making this permission a required one.

Access to content of the current tab (activeTab)

This permission does not appear in the user interface.

Together with scripting it allows to inject JavaScript code into the current page. It is the means used by the add-on to get attributes of the clicked element.

Almost unavoidable permission for this extension. An alternative is to ask user in response to every invocation from context menu.

Create context menu items (menus)

This permission does not appear in the user interface.

Mandatory permission since it is necessary to get which element should be inspected for alternative text somehow.

Execute JavaScript in the context of web pages (scripting)

This permission does not appear in the user interface.

Together with activeTab it allows to inject JavaScript code into the current page. It is the means used by the add-on to get attributes of the clicked element.

The scripting.executeScript method is a better variant of tabs.executeScript however it requires additional permission.

Complications with Chrome

Due to lack of browser.menus.getTargetElement (contextMenus) API method, it is necessary to inject a content script into every page frame to have active event listener when context menu is invoked. See https://github.com/mdn/webextensions-examples/blob/master/menu-remove-element/menusGetTargetElementPolyfill.js for an example of such approach. It is not implemented in this add-on.

License

Mozilla Public License, v. 2.0, see the LICENSE file.

Copyright (C) 2021-2024 Max Nikulin