我设计了一个firefox插件,该插件具有一个附加到javascript文件的manifest.json。这会在选择文本并单击鼠标右键时启动搜索。我需要复制已选择的文本。
console.log("hello hi")
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
function handleRightClick(click_event) {
if (getSelectionText() !== "") {
console.log(JSON.stringify(click_event, null, 4))
click_event.preventDefault()
console.log("selection text: "+getSelectionText())
var base_url1 = "https://www.google.com/search?safe=off&hl=en&source=hp&ei=s0b9W4PmIIaCyAOZwrawAw&q="+getSelectionText().replace(" ","+")
window.open(base_url1, 'ab');
}
}
document.addEventListener('contextmenu', event => handleRightClick(event));
现在如何实现此>> document.execCommand(“ copy”); 我在Firefox中收到失败的错误