Firefox WebExtension:上下文菜单中的 selectionText 仅 returns 150 个字符
Firefox WebExtension: selectionText in contextMenus only returns 150 characters
我主要基于 example provided by the Mozilla Developers Network 为 Firefox(52.2.1 32 位)构建了一个小型 WebExtension。它是一个 ContextMenu,它允许用户复制多个文本(通过选择相应的文本,然后选择上下文菜单中的一个按钮)并最终将它们组合(以某种方式)在剪贴板中以供进一步使用。
扩展工作得很好,但现在单独选择和复制的文本突然限制为 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 56 中修复,请参阅:
https://bugzilla.mozilla.org/show_bug.cgi?id=1338898
我主要基于 example provided by the Mozilla Developers Network 为 Firefox(52.2.1 32 位)构建了一个小型 WebExtension。它是一个 ContextMenu,它允许用户复制多个文本(通过选择相应的文本,然后选择上下文菜单中的一个按钮)并最终将它们组合(以某种方式)在剪贴板中以供进一步使用。
扩展工作得很好,但现在单独选择和复制的文本突然限制为 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 56 中修复,请参阅: https://bugzilla.mozilla.org/show_bug.cgi?id=1338898