Chrome 扩展消息传递 - 包含代码
我有一个名为 file.js 的内容脚本,它创建变量“found”。我想将变量“found”的值发送到
file.js 中 popup.html 中的文本框(我知道变量找到正在工作,使用警报进行了测试)。
chrome.extension.sendRequest({foundlinks: found}, function(response) {
console.log(response);
});
在 Popup.html 中:
function pulllinks(){
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
document.getElementById("box").value = request.foundlinks;
sendResponse({});
});
以及在 popup.html 中的表单上:
<input type = "button" onclick="pulllinks();" name = "box" id="box" value = "Grab Links From Page" />
但是,它没有执行任何操作。有什么想法吗?
谢谢!
I have a contentscript called file.js that creates variable "found". I want to send the values of varibale "found" to a textbox in popup.html
In file.js (I know varible found is working, tested using an alert).
chrome.extension.sendRequest({foundlinks: found}, function(response) {
console.log(response);
});
In Popup.html:
function pulllinks(){
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
document.getElementById("box").value = request.foundlinks;
sendResponse({});
});
And on the form in popup.html:
<input type = "button" onclick="pulllinks();" name = "box" id="box" value = "Grab Links From Page" />
However, its not doing anything. Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要以相反的方向发送请求 - 从弹出窗口到内容脚本:
popup.html:
file.js:
You need to be sending requests in the opposite direction - from popup to content script:
popup.html:
file.js:
首先,
onRequest
的监听器应该从一开始就存在,因此将其移到pulllinks
函数之外。其次,如何确保您的sendRequest
在弹出窗口打开时被触发?First of all, your listener for
onRequest
should exist from the start, so move it outside of thepulllinks
function. Secondly, how are you making sure that yoursendRequest
is being fired while the popup is open?