我已經爲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