Search code examples
javascriptfirefoxcontextmenufirefox-addon-webextensionstextselection

Firefox WebExtension: selectionText in contextMenus only returns 150 characters


I have build a small WebExtension for Firefox (52.2.1 32-Bit) mainly based on an example provided by the Mozilla Developers Network. It is a ContextMenu which allows the user to copy multiple texts (by selecting the respective text and then choosing one of the buttons in the context menu) and to have them finally combined (in a certain way) in the clipboard for further usage.

The extension worked just fine but now the individually selected and copied texts are suddenly limited to 150 characters, everything selected more is cut off. What could cause this behavior?

So far I could find no documentation or forum which states that selectionText only stores 150 characters.

This is how any selected text is copied to a local variable:

browser.contextMenus.onClicked.addListener(function(info, tab) {
  if (info.menuItemId == "save-title") {
    title = info.selectionText;
  }
});

The rest of the code is mostly identical to the example linked above:

browser.contextMenus.onClicked.addListener(function(info, tab) {
  if (info.menuItemId == "export-to-clipboard") {
    const content = title + "\t" + date + "\t" + author + "\t\t" + abstract;

    const code = "copyToClipboard(" +
        JSON.stringify(content) + ");";

    browser.tabs.executeScript({
        code: "typeof copyToClipboard === 'function';",
    }).then(function(results) {
        // The content script's last expression will be true if the function
        // has been defined. If this is not the case, then we need to run
        // clipboard-helper.js to define function copyToClipboard.
        if (!results || results[0] !== true) {
            return browser.tabs.executeScript(tab.id, {
                file: "clipboard-helper.js",
            });
        }
    }).then(function() {
        return browser.tabs.executeScript(tab.id, {
            code,
        });
    }).catch(function(error) {
        // This could happen if the extension is not allowed to run code in
        // the page, for example if the tab is a privileged page.
        console.error("Failed to copy text: " + error);
    });
    title = "";
    date = "";
    author = "";
    abstract = "";
  }
});

And, in order to include everything here, the clipboard-helper.js:

/* Copy-paste from https://github.com/mdn/webextensions-examples/blob/master/context-menu-copy-link-with-types/clipboard-helper.js */

// This function must be called in a visible page, such as a browserAction popup
// or a content script. Calling it in a background page has no effect!
function copyToClipboard(text) {
    function oncopy(event) {
        document.removeEventListener("copy", oncopy, true);
        // Hide the event from the page to prevent tampering.
        event.stopImmediatePropagation();

        // Overwrite the clipboard content.
        event.preventDefault();
        event.clipboardData.setData("text/plain", text);
    }
    document.addEventListener("copy", oncopy, true);

    // Requires the clipboardWrite permission, or a user gesture:
    document.execCommand("copy");
}

Solution

  • This is fixed in Firefox 56, see: https://bugzilla.mozilla.org/show_bug.cgi?id=1338898