如何在 Chrome 扩展中的 popup.js 和 background.js 之间进行通信? [英] How to communicate between popup.js and background.js in chrome extension?
问题描述
来自 popup.js 的消息被两次发布到 background.js,但我从 background.js 中完全没有得到任何信息.
The message from popup.js is being posted twice to background.js, but I get absolutely nothing from background.js.
背景.js
function login(username,password){
console.log(username);
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000/login/", true);
xhr.setRequestHeader('Content-type','application/json; charset=utf-8');
data = {"username":username,"password":password};
console.log(JSON.stringify(data));
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// JSON.parse does not evaluate the attacker's scripts.
var resp = JSON.parse(xhr.responseText);
console.log(resp);
var lStorage = localStorage;
localStorage.setItem("username",resp["username"]);
localStorage.setItem("apiKey",resp["apiKey"]);
localStorage.setItem("password",resp["password"]);
console.log(localStorage.getItem("username"));
}
};
}
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse){
console.log("hello");
if(request.msg == "login") {
//alert(request.password);
login(request.username,request.password);}
}
);
chrome.extension.onConnect.addListener(function(port) {
console.log("Connected .....");
port.onMessage.addListener(function(msg) {
console.log("message recieved "+ msg);
port.postMessage("Hi Popup.js");
});
});
popup.js:
function login(){
alert(document.login.username.value);
chrome.extension.sendRequest({ msg: "login",username:document.login.username.value,password:document.login.password.value});
document.getElementById('openBackgroundWindow').visbility = "hidden";
}
$(document).ready(function (){
checkUserAuth();
console.log("Inside");
$("#openBackgroundWindow").click(login);
});
function checkUserAuth(){
console.log(localStorage.getItem("apiKey"));
if(localStorage.getItem("apiKey") != null)
{
$("#openBackgroundWindow").visbility ="hidden";
}
}
var port = chrome.extension.connect({name: "Sample Communication"});
port.postMessage("Hi BackGround");
port.onMessage.addListener(function(msg) {
console.log("message recieved"+ msg);
});
推荐答案
方法 - A :
使用长连接您可以针对任何活动从 background.js 到扩展页面的 popup.js 进行通信(这里我包含了 popup.html 和 only started
来自 popup.js 的示例通信作为示例)
Method - A :
Using Long Lived Connections you can communicate from background.js to popup.js of extension page for any activities( Here i have included popup.html and only initiated
sample communication from popup.js as a sample)
background.js
chrome.extension.onConnect.addListener(function(port) {
console.log("Connected .....");
port.onMessage.addListener(function(msg) {
console.log("message recieved" + msg);
port.postMessage("Hi Popup.js");
});
})
popup.js
var port = chrome.extension.connect({
name: "Sample Communication"
});
port.postMessage("Hi BackGround");
port.onMessage.addListener(function(msg) {
console.log("message recieved" + msg);
});
方法 - B :
直接操作 DOM* 如果你的最终结果是修改 DOM,你可以用这个来实现
Method - B :
Direct Manipulation of DOM* if your end result is modification of DOM, you can achieve with this
popup.html
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
<div id="x" value="m">Some thing</div>
</body>
</html>
background.js
var views = chrome.extension.getViews({
type: "popup"
});
for (var i = 0; i < views.length; i++) {
views[i].document.getElementById('x').innerHTML = "My Custom Value";
}
方法 - C :
使用长寿命连接您可以为任何活动从 background.js 到扩展页面的 popup.js 进行通信(这里我没有包括 popup.html 和从 background.js 发起的示例通信;
Using Long Lived Connections you can communicate from background.js to popup.js of extension page for any activities( Here i have not included popup.html and initiated sample communication from background.js;
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
var port = chrome.extension.connect({
name: "Sample Communication"
});
port.postMessage("Hi BackGround");
port.onMessage.addListener(function(msg) {
console.log("message recieved" + msg);
});
});
我已经更改了您的代码,并在消除了一些内容并使其成为一个简单版本后使其运行.在此骨架上添加用于 AJAX 请求和 HTML DOM 的代码(确保在头部部分添加 <script>
标记并将 chrome.extension.onConnect.addListener
出 (xhr.readyState == 4)
代码;)
I have changed your code and made it running after eliminating some stuff and making it a simple version. Add your code for AJAX requests and HTML DOM on this skeleton( Make sure you add <script>
tag in head section and put chrome.extension.onConnect.addListener
out of (xhr.readyState == 4)
code;)
popup.html
<html >
<head>
<script src="popup.js"></script>
</head>
<body></body>
</html>
manifest.json
{
"name": "Demo",
"version": "1.0",
"manifest_version": 2,
"description": "This is a demo",
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
},
"permissions": ["<all_urls>",
"storage",
"tabs"
]
}
background.js
chrome.extension.onConnect.addListener(function(port) {
console.log("Connected .....");
port.onMessage.addListener(function(msg) {
console.log("message recieved " + msg);
port.postMessage("Hi Popup.js");
});
});
popup.js
var port = chrome.extension.connect({
name: "Sample Communication"
});
port.postMessage("Hi BackGround");
port.onMessage.addListener(function(msg) {
console.log("message recieved" + msg);
});
这篇关于如何在 Chrome 扩展中的 popup.js 和 background.js 之间进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!