后台脚本消息与JavaScript [英] Background script messaging with javascript

查看:199
本文介绍了后台脚本消息与JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Google Chrome浏览器扩展程序进行沟通时使用了以下指南: https:// developer.chrome.com/extensions/messaging



它曾经工作但我遇到过一个错误:


响应tabs.query时出错:TypeError:无法读取属性'id'
未定义


我比较了我的代码和Google Chrome代码,我似乎无法找到为什么我的代码会产生该错误:

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

这里是我寄给它的地方:

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

});

无法修复该错误,无论我尝试保持不变。
不能读取未定义的属性意味着tabs是不确定的,据我所知,但我不明白它为什么在Google示例中起作用,而在这里它不适用。



另一个问题:

如果我试图将它发送给制表符[1],那是否意味着它是在第二个位置的选项卡,或者我解释它错了吗?

解决方案

选项卡

你的查询是 {active:true,currentWindow :true} ,所以通常应该只有1个选项卡(因为最多只有1个当前窗口,并且只有1个活动选项卡)。



所以你需要第一个元素,它是选项卡[0]



选项卡[ 1] 将始终未定义此查询。






<选项卡将为空,过去非常罕见(Chrome浏览器运行在backgrou中nd不打开窗口)。



然而,最近更改的API 不会返回开发工具选项卡。所以如果你正在调试你的扩展,并且开发工具窗口是打开和关注的,那么该数组将是空的。你应该检查。


I was messing around with communicating inside a Google Chrome extension and was using the following guide: https://developer.chrome.com/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 compared 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?

解决方案

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.

这篇关于后台脚本消息与JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆