Consider this code:
// background.js
function Action(tab) {
let title;
function domTitle() {
return document.title;
}
chrome.scripting.executeScript({target: {tabId: tab.id}, func: domTitle}, injectionResults => {
for (const frameResult of injectionResults) {
this.title = frameResult.result;
console.log(this); // line 10
console.log(this.title); // line 11
}
});
}
chrome.contextMenus.onClicked.addListener(((info, tab) => {
const action = new Action(tab);
console.log(action); // line 29
console.log(action.title); // line 30
}));
Why line 30 ( the title property title of Action ) is undefined? The property has clearly been set.
How to fix it? So line 30 will hold the value that was set by chrome.scripting.executeScript.