0

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.

Community
  • 1
  • 1
Jamie Phan
  • 796
  • 1
  • 7
  • 26
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Makyen Dec 17 '16 at 01:42
  • I just checked that asynchronous call, first time heared of it, and I am not able to modify my code with `Promise`, neither I can find any relative documents or post online. – Jamie Phan Dec 17 '16 at 01:52
  • Then please [edit] the question to describe what about the linked duplicate you don't understand. – Makyen Dec 17 '16 at 01:56
  • Ignore the part in the first answer to that question regarding Promises. I am discussing the appropriateness of having the "TL;DR" section there with the person who added it (16 hours ago). See the portion that is "**Embrace the asynchronous nature of JavaScript!**":"**Restructure code**":"**Let functions accept callbacks**". If you really wanted to use Promises, you could do so. But, you would need to have code that follows more along the lines of the example in the "Use promises" section just after "Let functions accept callbacks". – Makyen Dec 17 '16 at 02:42
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – rsanchez Dec 17 '16 at 16:57

0 Answers0