Chrome 扩展消息传递 - 包含代码

发布于 2024-11-04 13:59:13 字数 692 浏览 0 评论 0原文

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2024-11-11 13:59:13

您需要以相反的方向发送请求 - 从弹出窗口到内容脚本:

popup.html:

function pulllinks(){
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendRequest(tab.id, {cmd: "findLinks"}, function(response) {
            document.getElementById("box").value = response.foundlinks;
        });
    });

}

file.js:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "findLinks") {
        //calculate "found" value and send it back
        sendResponse({foundlinks: found});

    }
});

You need to be sending requests in the opposite direction - from popup to content script:

popup.html:

function pulllinks(){
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.sendRequest(tab.id, {cmd: "findLinks"}, function(response) {
            document.getElementById("box").value = response.foundlinks;
        });
    });

}

file.js:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "findLinks") {
        //calculate "found" value and send it back
        sendResponse({foundlinks: found});

    }
});
看春风乍起 2024-11-11 13:59:13

首先,onRequest 的监听器应该从一开始就存在,因此将其移到 pulllinks 函数之外。其次,如何确保您的 sendRequest 在弹出窗口打开时被触发?

First of all, your listener for onRequest should exist from the start, so move it outside of the pulllinks function. Secondly, how are you making sure that your sendRequest is being fired while the popup is open?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文