私はFirefox用の小さなWebExtension(52.2.1 32ビット)を主にexample provided by the Mozilla Developers Networkに基づいてビルドしました。 ContextMenuは、複数のテキストを(それぞれのテキストを選択し、コンテキストメニューでボタンを選択することによって)コピーし、最終的に(特定の方法で)クリップボードに組み合わせて後で使用できるようにします。Firefox WebExtension:contextMenusのselectionTextは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含める:これは、Firefox 56に固定されている
/* 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)Andrew Swanが彼の答えを掲載しました。 – Syrill