jquery - Background script messaging with javascript - Stack Overflow

I was messing around with municating inside a Google Chrome extension and was using the following guide

I was messing around with municating inside a Google Chrome extension and was using the following guide:

It used to work but I have encountered an error :

Error in response to tabs.query: TypeError: Cannot read property 'id' of undefined

I pared my code and the Google Chrome code and I can't seem to find why my code produces that error:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                chrome.tabs.sendMessage(tabs[1].id, {fen: request.needMove}, function(response) {
                    //console.log(response.farewell);
                });
            });

Here is where I send it to:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log("recv FEN : " + FEN);
   FEN = request.fen;
   setCookie("FEN_SET", "true" , 1);
   setFEN(FEN);

  });

I can't fix that error, whatever I try it stays the same. "Cannot read property of undefined" is implying that 'tabs' is undefined as far as I understood but I don't understand why it works in the Google example and here it doesn't.

Another Q :

If I'm trying to send it to tabs[1] does that mean it's the tab in the second position, or am I interpreting it wrong?

I was messing around with municating inside a Google Chrome extension and was using the following guide: https://developer.chrome./extensions/messaging

It used to work but I have encountered an error :

Error in response to tabs.query: TypeError: Cannot read property 'id' of undefined

I pared my code and the Google Chrome code and I can't seem to find why my code produces that error:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                chrome.tabs.sendMessage(tabs[1].id, {fen: request.needMove}, function(response) {
                    //console.log(response.farewell);
                });
            });

Here is where I send it to:

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log("recv FEN : " + FEN);
   FEN = request.fen;
   setCookie("FEN_SET", "true" , 1);
   setFEN(FEN);

  });

I can't fix that error, whatever I try it stays the same. "Cannot read property of undefined" is implying that 'tabs' is undefined as far as I understood but I don't understand why it works in the Google example and here it doesn't.

Another Q :

If I'm trying to send it to tabs[1] does that mean it's the tab in the second position, or am I interpreting it wrong?

Share Improve this question edited Apr 16, 2015 at 18:41 Sarah Elan 2,4611 gold badge25 silver badges45 bronze badges asked Apr 16, 2015 at 17:02 NotGINotGI 4681 gold badge9 silver badges21 bronze badges 5
  • 2 Without being familiar with the platform, the error means tabs[1] is undefined, debug the script at that point and see what the actual contents of tabs are. – Etheryte Commented Apr 16, 2015 at 17:04
  • says : "tabs: Array[0]" but if you look a the google example , its exactly the same I copied and pasted it to fit my needs – NotGI Commented Apr 16, 2015 at 17:06
  • 1 Make sure you have at least two tabs open in the browser – Dave Commented Apr 16, 2015 at 17:12
  • I have more than that , my extension requires two pages... – NotGI Commented Apr 16, 2015 at 17:18
  • 1 If tabs is Array[0] then chrome.tabs.query({active: true, currentWindow: true},...) is returning an empty array. Try broadening your query so it returns all the tabs you are looking for. – Sarah Elan Commented Apr 16, 2015 at 18:14
Add a ment  | 

1 Answer 1

Reset to default 14

tabs is the list of all tabs (regardless of position) that pass the filter.

Your query is {active: true, currentWindow: true}, so normally it should be just 1 tab (as there is at most 1 current window with exactly 1 active tab).

So you need the first element, which is tabs[0].

tabs[1] will always be undefined with this query.


Cases when tabs will be empty empty used to be exceedingly rare (Chrome running in background with no windows open).

However, with a recent change the API will not return the Dev Tools tab. So if you're debugging your extension and the Dev Tools window is open and focused, the array will be empty. You should check for that.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743778770a4505718.html

相关推荐

  • jquery - Background script messaging with javascript - Stack Overflow

    I was messing around with municating inside a Google Chrome extension and was using the following guide

    7天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信