基本的谷歌浏览器扩展消息传递不起作用 [英] Basic google chrome extension message passing not working

查看:171
本文介绍了基本的谷歌浏览器扩展消息传递不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让消息传递的chrome扩展工作,但是console.log(response)给了我未定义。我已经看过消息传递和在计算器上的文档,但在我的情况下,它不会工作。



myscript.js:

  chrome.extension.sendRequest({greeting:hello},function(response){
console.log(response);
return true;
});

background.js:

  chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse){
sendResponse({farewell:goodbye});
return true;
});

manifest.json

  {
manifest_version:2,

name:nameasf,
description:asfasaf,
版本:1.0,

权限:[
标签,
http://somedomain.com/
],

content_scripts:[
{
matches:[http://somedomain.com/*],
css:[mystyles。 css],
js:[jquery.js,myscript.js],
all_frames:true
}
],

background:{
scripts:[background.js]
},

browser_action:{
default_icon :icon.png,
default_popup:popup.html
}
}


解决方案

您正在使用过时的 chrome.extension.sendRequest

使用 改为使用chrome.runtime.sendMessage



(注意:您已使用新的 chrome.runtime.onMessage ,因此您不必在侦听器端更改任何内容。)




一些附注: ul>

  • 对于这里提供的代码,您不需要任何权限(特别是不需要强大的选项卡权限)。
  • 使用非持久性背景页面(aka 活动页面 ),因为它们相当资源友好。 $ b

    I've been trying to get message passing for a chrome extension to work but console.log(response) gives me undefined. I've looked at the docs for message passing and on stackoverflow, but in my case it won't work. I am at a loss as to why.

    myscript.js:

    chrome.extension.sendRequest({greeting: "hello"}, function(response) {
        console.log(response);
        return true;
    });
    

    background.js:

    chrome.runtime.onMessage.addListener(
       function(request, sender, sendResponse) {
          sendResponse({farewell: "goodbye"});
          return true;
     });
    

    manifest.json

    {
      "manifest_version": 2,
    
      "name": "nameasf",
      "description": "asfasaf",
      "version": "1.0",
    
      "permissions": [
        "tabs",
        "http://somedomain.com/"
      ],
    
        "content_scripts": [
        {
          "matches": ["http://somedomain.com/*"],
          "css": ["mystyles.css"],
          "js": ["jquery.js", "myscript.js"],
          "all_frames": true
        }
      ],
    
      "background": {
        "scripts": ["background.js"]
      },
    
      "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
      }
    }
    

    解决方案

    You are using the obsolete chrome.extension.sendRequest.
    Use chrome.runtime.sendMessage instead.

    (Note: You are already using the new chrome.runtime.onMessage so you don't have to change anything on the listener side.)


    A couple of sidenotes:

    • For the code presented here you don't need any permission (especially not the powerful tabs permission).
    • And it is a good idea to use non-persistent background pages (a.k.a. event-pages) whenever possible as they are considerably more resource-friendly.

    这篇关于基本的谷歌浏览器扩展消息传递不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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