I am still quite new to makeing Google Chrome extensions, but I encounter this problem:
I want to make a function with chrome.tabs.executeScript
that I can reuse the code, and here is what I made:
function get(code){
chrome.tabs.executeScript({
code: '(function(){return ' + code + ' })();'
}, function(results) {
console.log("This log is inside the exec function: " + results[0]);
return results[0];
});
}
Then I call the function by:
console.log("This log is called externally: " + get("document.getElementsByClassName('test-class')[0].innerText"))
And, the HTML in the main page of the extension:
<div class="test-class">This is testing text.</div>
But end up the result in the console panel, I got the following message:
This log is called externally: undefined
This log is inside the exec function: This is testing text.
Which is not good, as I want it working and get the result by calling get()
, but not inside the chrome.tabs.executeScript
itself.
I got this link from suggested duplicate, and I read though it, the question example for that is a jQuery Ajax call, which used Promise (returning the whole ajax call), but for chrome.tabs.executeScript
, I really cannot find any relevant documents online with using Promise
.
Whereas I tried something like this but it doesn't work.:
function get(code){
return chrome.tabs.executeScript({
code: '(function(){return ' + code + ' })();'
});
}
var result = get("document.getElementsByClassName('pNickname')[0].innerText")
result.then(function(value){
console.log(value)
})
Many Thanks.