我已经为Firefox(52.2.1 32位)构建了一个小型WebExtension,主要基于example provided by the Mozilla Developers Network。它是一个ContextMenu,它允许用户复制多个文本(通过选择相应的文本,然后选择上下文菜单中的一个按钮),并使它们最终在剪贴板中组合(以某种方式)以供进一步使用。Firefox WebExtension:selectionText in contextMenus只返回150个字符
扩展工作得很好,但现在单独选择和复制的文本突然被限制为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 = "";
}
});
而且,为了包括这里的一切,剪贴板-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之间的变化? – Makyen
@Makyen我自己并没有和webextension一起工作,所以跟踪起来有点困难,但是,我想是的。使用Firefox的ESR可能会混淆它更改的版本... 但我认为你可以很容易地跟踪它,当你[看看线程](https://bugzilla.mozilla.org/show_bug.cgi ?id = 1338898)安德鲁斯旺在他的回答中张贴。 – Syrill