gpt4 book ai didi

javascript - 使用 chrome.notifications.create 时出错 "Uncaught TypeError: Cannot read property ' create' of undefined"

转载 作者:数据小太阳 更新时间:2023-10-29 03:59:21 30 4
gpt4 key购买 nike

您好,我在 chrome 应用的 js 函数中调用 chrome.notifications.create 时出错。可以在函数外部正常使用,但在函数内部时出现以下错误:

Uncaught TypeError: Cannot read property 'create' of undefined

代码如下:

document.addEventListener('DOMContentLoaded', function () {
document.getElementById('submit').addEventListener('click', submit);
});
function submit() {
var options = {
type:"basic",
title:"nameVal",
message:"msgVal",
iconUrl:"icon.png",
};
//notification options set
chrome.notifications.create(options,callback);
//notification set
}
function callback() {
console.log("Notification succesfull");
//notification confirmed
}

谢谢,我是 js 和 chrome 应用程序的菜鸟,所以非常感谢您的帮助:)

最佳答案

有两个可能的原因。

  • 您正在尝试从 content script 中使用它.你不能:内容脚本在它们可以调用的 Chrome API 方面非常有限。

    However, content scripts have some limitations. They cannot:

    Use chrome.* APIs, with the exception of:
    extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest )
    i18n
    runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage )
    storage

    在这种情况下,您需要将此调用委托(delegate)给后台脚本:send a message从内容脚本中,在后台脚本中获取它并执行操作。

  • 您正在尝试从扩展脚本中调用它,但未声明“notifications” 权限。

    在那种情况下,修复是微不足道的 - 只需添加权限即可。

关于javascript - 使用 chrome.notifications.create 时出错 "Uncaught TypeError: Cannot read property ' create' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34912279/

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