I just try to migrate my terminated colleague’s addon to Webextension from SDK.
However, I do not know how to realize it. just want to do the followings by addon.
1.Use Context menu
2.Get the values from 2 or more fields which have id in a web page
3.Set these values to clipboard
4.Open specific page as new tab
Previously, SDK get the values as follows.
var f1 = document.getElementById(“filed1”);
var f1value = f1.value;
var f2 = document.getElementById(“field2”);
var f2value = f2.value;
I’d like to do:
1.Use Context menu without selecting any fields
2.Get the values from 2 or more specific fields which have id in a web page
3.Set these values to clipboard
4.Open specific page as new tab
I write the followings and just try to add it using FF developer edition, but did not work.
No error occurs, but I do not know how to debug it…
That is the problem … browser.tabs.executeScript is asynchronous and doesn’t return anything. You need to wait for it to resolve and then() use the data.
I changed the code as follows, but clipboard does not have any data although I believe I passed the test text data.
“browser.tabs.executeScript was executed!” is shown in Console output and open new tab. So, no error occurs.
function copydata(event) {
document.removeEventListener("copy", copydata, true);
let filed1_value = document.getElementById(“field1”).value;
event.preventDefault();
event.clipboardData.setData(‘text/plain’, filed1_value);
}
document.addEventListener("copy", copydata, true);
document.execCommand("copy");
background.js
// Create context menu - right click
chrome.contextMenus.create({
id: "xxxxx",
title: "xxxxx",
contexts: ["all"],
});
// Fire when click context menu
chrome.contextMenus.onClicked.addListener((info, tab) => {
// Call content script and get result
browser.tabs.executeScript({ file: "content_script/copy.js"})
.then(
// succesfull copy from filed1_value
)
.catch((error) => {
console.error("Failed to copy data: " + error);
});
// Open Apex page as new tab at next position
// when is this supposed to happen? Does it have anything to do with the copy?
browser.tabs.create({
"url": "https://xxxxxxxxxxxxxxxxxxxxx" ,
index: tab.index + 1
});
});