chrome.runtime.sendMessage无法在Chrome扩展程序中使用 [英] chrome.runtime.sendMessage not working in Chrome Extension

查看:1401
本文介绍了chrome.runtime.sendMessage无法在Chrome扩展程序中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个新的扩展。我可以在后面使用chrome.runtime.sendMessage函数,但现在我已经尝试了一切,但它仍然无法将消息发送到后台脚本。控制台将获得来自 content-script.js 的日志消息,但不会从 background.js

content-script.js

  console.log(Hello World!s); 
$(document).ready(function(){
console.log(DOM READY!);
$(document.documentElement).keydown(function(e){
console.log(Key has beenressed!);
chrome.runtime.sendMessage({Message:getTextFile},function(response){
if(response.fileData){
alert(Content Of Text File =);
}
else {
console.log(No Response Received);
}
} )

})
});

background.js

  console.log(Atleast reached background.js)
chrome.runtime.onMessage.addListener(
function(request,sender,sendResponse){
console.log(Reached Background.js);
if(request.Message ==getTextFile){
console.log(Entered IF Block);
$ .get(http:// localhost:8000 / rapidusercannedspeechbucket / helloWorld1,function(response){
console.log(response);
sendResponse({fileData:response})$ b $ (b)}
}
else {
console.log(没有收到答复!!!)
}
}
);

manifest.json

  {
manifest_version:2,
name:My Cool Extension,
version:0.1,
content_scripts:[{
all_frames:true,
js:[jquery-2.1.4.min.js,content-script.js],
matches:[http:// * / *,https:// * / *,file:// * / *]
}],
权限:[http:// * / *,https:// * / *,storage],
background:{
scripts:[
jquery-2.1.4.min.js,
background.js
]
}
}

任何帮助表示赞赏:)

谢谢!



解决方案

console.log(Atleast reached background.js)
chrome.runtime.onMessage.addListener(
function(request,sender, sendResponse){
console.log(Reached Background.js);
if(request.Message ==getTextFile){
console.log(Entered IF Block);
$ .get(http:// localhost:63342 / Projects / StackOverflow / ChromeEXT / helloWorld1,function(response){
console.log(response);

//返回当前标签的响应
chrome.tabs.query({active:true,currentWindow:true},function(tabs){
chrome.tabs.sendMessage(tabs [0] .id,{fileData:response},function(response){
;
});
});


})
}
else {
console.log(没有收到回应!!!)
}
}
);

当您需要执行contentcript时:

  console.log(Hello World!s); 
$(document).ready(function(){
console.log(DOM READY!);
$(document.documentElement).keydown(function(e){
console.log(Key has beenressed!);
chrome.runtime.sendMessage({Message:getTextFile},function(response){
;
})

})
});


//接受来自后台的消息
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
alert(Contents Of Text File =+ request.fileData);
});

sendResponse可以作为即时反馈而不是结果的计算。


I am trying to create a new extension. I was able to use the chrome.runtime.sendMessage function a while back but right now, I have tried everything and it still is not able to send the message to the background script. The console is getting populated with the log messages from the content-script.js but not from the background.js

content-script.js

console.log("Hello World!s");
$(document).ready(function() {
    console.log("DOM READY!");
    $(document.documentElement).keydown(function (e) {
        console.log("Key Has Been Pressed!");
        chrome.runtime.sendMessage({Message: "getTextFile"}, function (response) {
                if (response.fileData) {
                    alert("Contents Of Text File = ");
                }
                else {
                    console.log("No Response Received");
                }
            })

    })
});

background.js

console.log("Atleast reached background.js")
        chrome.runtime.onMessage.addListener (
            function (request, sender, sendResponse) {
                console.log("Reached Background.js");
                if (request.Message == "getTextFile") {
                    console.log("Entered IF Block");
                        $.get("http://localhost:8000/quicklyusercannedspeechbucket/helloWorld1", function(response) {
                    console.log(response);
                    sendResponse({fileData: response})
                })
            }
            else {
                console.log("Did not receive the response!!!")
            }
        }
    );

manifest.json

{
  "manifest_version": 2,
  "name": "My Cool Extension",
  "version": "0.1",
  "content_scripts": [ {
    "all_frames": true,
    "js": [ "jquery-2.1.4.min.js", "content-script.js" ],
    "matches": [ "http://*/*", "https://*/*", "file://*/*" ]
  } ],
  "permissions": [ "http://*/*", "https://*/*", "storage" ],
  "background": {
    "scripts": [
      "jquery-2.1.4.min.js",
      "background.js"
    ]
  }
}

Any help is appreciated :)

Thanks!

解决方案

You need to change your code so that in the background.js you must change the behaviour:

console.log("Atleast reached background.js")
chrome.runtime.onMessage.addListener (
    function (request, sender, sendResponse) {
        console.log("Reached Background.js");
        if (request.Message == "getTextFile") {
            console.log("Entered IF Block");
            $.get("http://localhost:63342/Projects/StackOverflow/ChromeEXT/helloWorld1", function(response) {
                console.log(response);

                // to send back your response  to the current tab
                chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                    chrome.tabs.sendMessage(tabs[0].id, {fileData: response}, function(response) {
                        ;
                    });
                });


            })
        }
        else {
            console.log("Did not receive the response!!!")
        }
    }
);

While for the contentscript you need to do:

console.log("Hello World!s");
$(document).ready(function() {
    console.log("DOM READY!");
    $(document.documentElement).keydown(function (e) {
        console.log("Key Has Been Pressed!");
        chrome.runtime.sendMessage({Message: "getTextFile"}, function (response) {
            ;
        })

    })
});


// accept messages from background
chrome.runtime.onMessage.addListener (function (request, sender, sendResponse) {
    alert("Contents Of Text File = " + request.fileData);
});

The sendResponse can be used as an immediate feedback not as a result of computation.

这篇关于chrome.runtime.sendMessage无法在Chrome扩展程序中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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