使用executeScript执行popup.html中的函数

发布于 2024-12-27 01:29:47 字数 1144 浏览 6 评论 0原文

所以,我尝试使用 executeScript 来执行 popup.html 的函数。

这就是我想要做的:

function atacar () {
    $(document).ready(function () {
        $("#target_attack").click();
    });
}

function apoiar () {
    $(document).ready(function () {
        $("#target_support").click();
    });
}

function enviar_comando (tipo) {
    chrome.windows.getCurrent(function(win) {
        chrome.tabs.getAllInWindow(win.id, function(tabs) {
            for (i = 0; i < tabs.length; i++) {
                if (tipo == "ataque") chrome.tabs.executeScript(tabs[i].id, { code: "atacar();" });
                else if (tipo == "apoio") chrome.tabs.executeScript(tabs[i].id, { code: "apoiar();" });
            }
        });
    });
}

看看这些行:

                if (tipo == "ataque") chrome.tabs.executeScript(tabs[i].id, { code: "atacar();" });
                else if (tipo == "apoio") chrome.tabs.executeScript(tabs[i].id, { code: "apoiar();" });

我正在尝试执行 popup.html 的一个名为 atacar()apoiar 的函数(),但它不起作用。

帮助? :)

so, I'm trying to use executeScript to execute a function of popup.html.

That's what I'm trying to do:

function atacar () {
    $(document).ready(function () {
        $("#target_attack").click();
    });
}

function apoiar () {
    $(document).ready(function () {
        $("#target_support").click();
    });
}

function enviar_comando (tipo) {
    chrome.windows.getCurrent(function(win) {
        chrome.tabs.getAllInWindow(win.id, function(tabs) {
            for (i = 0; i < tabs.length; i++) {
                if (tipo == "ataque") chrome.tabs.executeScript(tabs[i].id, { code: "atacar();" });
                else if (tipo == "apoio") chrome.tabs.executeScript(tabs[i].id, { code: "apoiar();" });
            }
        });
    });
}

Look at those lines:

                if (tipo == "ataque") chrome.tabs.executeScript(tabs[i].id, { code: "atacar();" });
                else if (tipo == "apoio") chrome.tabs.executeScript(tabs[i].id, { code: "apoiar();" });

I'm trying to execute a function of popup.html that's called atacar() and apoiar(), but it's not working.

Help? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

嘴硬脾气大 2025-01-03 01:29:47

由于 atacar() 和 apoiar() 也在 popup.html 中,因此您无法从executeScript 运行它们。
您可以:

  • 将这些函数也插入到您提供给executeScript的代码字符串中,或​​者
  • 更好的是,将这两个函数移动到 内容脚本 然后你就可以像你一样运行它们。

Since atacar() and apoiar() are also in popup.html , you can't run them from the executeScript.
You can either:

  • Insert these functions also into the code string that you're supplying to executeScript, or
  • better yet, move these 2 functions into a content script and then you can run them like you do.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文