gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:18:58 26 4
gpt4 key购买 nike

我有一个名为 file.js 的内容脚本,它创建变量“found”。我想将变量“found”的值发送到 popup.html 中的文本框

在 file.js 中(我知道发现的变量正在工作,使用警报进行了测试)。

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"  /> 

但是,它没有做任何事情。有任何想法吗?

谢谢!

最佳答案

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

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;
});
});

}

文件.js:

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

}
});

关于javascript - Chrome 扩展消息传递 - 包含代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5859582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com