无法使用 google chrome 扩展触发 chrome.browserAction.onClicked.addListener [英] Unable to trigger chrome.browserAction.onClicked.addListener with google chrome extensions

查看:86
本文介绍了无法使用 google chrome 扩展触发 chrome.browserAction.onClicked.addListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点卡在这里,想知道是否有人能指出我可能错的地方.

I'm a bit stuck here and was wondering if anyone can point out where I might be wrong.

我只是想在单击应用程序图标时将主体颜色更改为红色.

I am simply trying to make the body color change to red on click of the app icon.

manifest.json

manifest.json

{
    "name": "Bagde",
    "description": "",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "browser_action": {
        "default_title": "Test",
        "default_popup": "popup.html"
    }
}

popup.html

<html>

    <head>
        <script src="popup.js"></script>
    </head>

    <body>
        <p>Some Content ..</p>
    </body>

</html>

popup.js

document.addEventListener("DOMContentLoaded", function () {
    //Get Reference to Functions
    backGround = chrome.extension.getBackgroundPage();
    //Call Function
    backGround.updateIcon();
  });

背景.js

var i = 1;

function updateIcon() {
    i = 1;
    chrome.browserAction.setBadgeText({
        text: 'Test'
    });
    chrome.browserAction.setPopup({
        popup: "popup.html"
    });
}


chrome.browserAction.setBadgeBackgroundColor({
    color: [200, 0, 0, 100]
});

window.setInterval(function () {
    chrome.browserAction.setBadgeText({
        text: String(i)
    });
    i++;
}, 4000);

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(null,
        {code:"document.body.bgColor='red'"});
});

任何想法我可能做错了什么?感谢您花时间阅读本文.

any ideas what I may be doing wrong? Thanks for taking your time to reading this.

推荐答案

如果你定义了 default_popup,你就不能拥有 browserAction.onClicked 的监听器.在这种情况下,您只需将处理程序中的代码添加到您的 popup.js.

If you define default_popup, you can't have a listener for browserAction.onClicked. In this case you can simply add the code in your handler to your popup.js.

EDIT:即在 popup.js 中添加以下内容:

EDIT: That is, add to popup.js the following:

chrome.tabs.executeScript(null, {code:"document.body.bgColor='red'"});

这篇关于无法使用 google chrome 扩展触发 chrome.browserAction.onClicked.addListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆