在这个扩展中,我的错在哪里?

时间:2017-11-15 15:28:42

标签: javascript google-chrome-extension

以下代码来自我正在撰写的扩展程序。这是一个扩展,当您单击按钮时,它将显示通知。但是,当我运行它时,按下按钮没有任何反应。

清单文件

{
    "manifest_version":2,

    "name":"NOTE",
    "description":"It is an example for using Notification",
    "version":"1.0",

    "browser_action":{
        "default_popup":"popup.html",
        "default_icon":{
            "16":"icon_16.png",
            "48":"icon_48.png",
            "128":"icon_128.png"
        }
    },

    "permissions": [
        "notifications"
    ]
}

popupHtml

<html>
    <head>
        <title>NOTIFICATION</title>
        <script src="popup.js"></script>
        <script src="jquery-1.10.2.js"></script>
    </head>
    <body>
        <input id="change" type="submit" value="Notification" />
    </body>
</html>

popupJS //函数在此文件中:

$(function(){
    $('#change').click(function(){
        var opt ={
           type: "basic",
           title: "notification",
          message: "Total has been reset back to 0.",
           iconUrl: "icon_48.png"
        }
        chrome.notifications.create('change', opt, function () { }); 
    });
});

1 个答案:

答案 0 :(得分:0)

您在加载之前使用jQuery。订单很重要:

<script src="popup.js"></script>
<script src="jquery-1.10.2.js"></script>

应该是

<script src="jquery-1.10.2.js"></script>
<script src="popup.js"></script>

如上所述,您还应该use button instead of submit

如果您正确调试了弹出窗口,您将能够看到此错误;为此,右键单击该按钮并选择“Inspect popup”以显示它的Dev Tools。