javascript - 火狐网络扩展 : selectionText in contextMenus only returns 150 characters

标签 javascript firefox contextmenu firefox-addon-webextensions textselection

我已经为 Firefox(52.2.1 32 位)构建了一个小型 WebExtension,主要基于 example provided by the Mozilla Developers Network 。它是一个上下文菜单,允许用户复制多个文本(通过选择相应的文本,然后选择上下文菜单中的按钮之一)并最终将它们组合(以某种方式)在剪贴板中以供进一步使用。

扩展程序运行得很好,但现在单独选择和复制的文本突然被限制为 150 个字符,所有选择的内容都会被切断。什么可能导致这种行为?

到目前为止,我找不到任何文档或论坛表明 selectionText仅存储 150 个字符。

这是将任何选定的文本复制到局部变量的方式:

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

其余代码与上面链接的示例基本相同:

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 = "";
  }
});

并且,为了包含此处的所有内容,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");
}

最佳答案

此问题已在 Firefox 56 中修复,请参阅: https://bugzilla.mozilla.org/show_bug.cgi?id=1338898

关于javascript - 火狐网络扩展 : selectionText in contextMenus only returns 150 characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45550560/

相关文章:

Android:如何设置生成的上下文菜单标题?

Excel 2010 单元格内的自定义右键菜单

javascript - 为什么dbs调用后数组变量没有保存-node js

javascript - cryptoJS 解密/解密消息 AES CFB 模式

javascript - 使用 jQuery 遍历 DOM

firefox - 在我的 firefox 扩展中,onUninstalled 事件似乎没有触发

javascript - 为什么在 JavaScript 中使用 getRangeAt() 会从选择中删除一些内容?

css - 在 Firefox 和 IE 上工作的图像模糊效果

android - 获取在 RecycleView 中选中的 ContextMenu 的位置

javascript - 当复选框= true时