gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:50:52 25 4
gpt4 key购买 nike

我已经为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com