Chrome扩展:处理异步的sendMessage。 [英] Chrome Extension: dealing with asynchronous sendMessage.

查看:1990
本文介绍了Chrome扩展:处理异步的sendMessage。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Chrome扩展,提取元数据。在code这解析元数据包含在一个内容脚本。该background.js和content.js通过的sendMessage请求和响应通信。我正在与请求的sendMessage的异步性的问题,我不知道如何解决它(甚至是阅读关于这一问题的讨论一连串之后)。任何意见或方向将是AP preciated。我怀疑我没有得到如何将这些进入回调。

background.js:

 函数onContextClick(资讯,标签){
  如果(资讯[selectionText]){
    VAR X = getMeta(卡);
    //做的东西随x
  }
}功能getMeta(标签){
chrome.tabs.sendMessage(tab.id,{fetchTag:元,名称,作者,内容},函数(响应){
    //alert(response.data);
    //一件事我疲倦把我的做的东西在这里嵌入,但没有任何工作
    返回response.data;
    });
}VAR menu_id = chrome.contextMenus.create({称号:获取元,语境:选择],onclick事件:onContextClick});

content.js:

 函数fetchTag(串){
    变种参数= string.split(,);
    返回$(参数[0] +[+参数[1] += \\+参数[2] +\\])ATTR(参数[3])。
    }chrome.extension.onMessage.addListener(
  功能(要求,发件人,sendResponse){
    如果(request.fetchTag.length大于0)
    sendResponse({数据:fetchTag(request.fetchTag)});
  });


解决方案

您可以使用 关闭。以这种方式

 函数onContextClick(资讯,标签){
  如果(资讯[selectionText]){
    getMeta(选项卡,功能(X){
      的console.log(X);
      //做的东西随x
    });
  }
}功能getMeta(标签,回调){
chrome.tabs.sendMessage(tab.id,{fetchTag:元,名称,作者,内容},函数(响应){
    //alert(response.data);
    //一件事我疲倦把我的做的东西在这里嵌入,但没有任何工作
    回调(response.data);
    });
}

I'm working on a chrome extension that extracts meta data. The code which parses the metadata is contained in a content script. The background.js and content.js communicates via a sendMessage request and response. I'm running into an issue with the asynchronous nature of the sendMessage request and I'm not sure how to fix it (even after reading a litany of the discussions on the issue). Any advice or direction would be appreciated. I suspect I'm not getting how to turn these into callbacks.

background.js:

function onContextClick(info, tab) {    
  if( info["selectionText"] ){  
    var x = getMeta(tab);   
    //do stuff with x       
  }
}

function getMeta (tab) {
chrome.tabs.sendMessage(tab.id, {fetchTag: "meta,name,author,content"}, function(response) {
    //alert(response.data);
    //one thing I tired was to put my "do stuff" embedded here, but that didn't work either         
    return response.data; 
    });
}

var menu_id = chrome.contextMenus.create({"title": "Get Meta", "contexts":["selection"], "onclick": onContextClick});

content.js:

function fetchTag(string) {
    var param = string.split(",");
    return $(param[0] + "["+param[1]+ "=\"" + param[2] + "\"]").attr(param[3]); 
    }

chrome.extension.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.fetchTag.length > 0)        
    sendResponse({data: fetchTag(request.fetchTag)});
  });

解决方案

You can use closures. In this way.

function onContextClick(info, tab) {    
  if( info["selectionText"] ){  
    getMeta(tab, function(x){
      console.log(x);
      //do stuff with x
    });
  }
}

function getMeta (tab, callback) {
chrome.tabs.sendMessage(tab.id, {fetchTag: "meta,name,author,content"}, function(response) {
    //alert(response.data);
    //one thing I tired was to put my "do stuff" embedded here, but that didn't work either
    callback(response.data);
    });
}

这篇关于Chrome扩展:处理异步的sendMessage。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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