Chrome扩展:从弹出窗口发送数据以取消链接JS文件

chrome extension: sending data from popup to unlink js file

本文关键字:数据 取消 链接 文件 JS 扩展 窗口 Chrome      更新时间:2023-09-26

>我弄乱了Chrome扩展。我得到了我在popup html中做的Litell表单,我试图用用户输入做一些事情。我得到了一个在popup.html中链接的文件,其中包含此功能:

function click(e) {
chrome.tabs.executeScript(null, {
    file: "theJs.js"
});

}

现在在文件theJs.js中,我在正在运行的当前选项卡上编写了要执行的代码(可以在当前选项卡上获取和设置信息)。

所以我的问题是:

如何从popup.html获取信息,并将其发送到theJs.js文件,所以我可以使用当前选项卡上的用户输入?

可能吗?

(对不起我的英语)

使用消息传递

内容.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (request.type == 'apply') {
        applySettings(request.settings);
        sendResponse();
    }
});

弹出窗口.js

chrome.tabs.getSelected(function(tab) {
    chrome.tabs.sendRequest(tab.id, { type: "apply", name: name, settings: settings },
        function(response) {
            showMessage('success', '<strong>' + name + '</strong> applied.');
        }
    );
});
  • 我的扩展:user-bootstrap#popup.html
  • 我的扩展:user-bootstrap#content.js