Google chrome extension chrome.experional.notification(富通知)和外部URL

Google chrome extension chrome.experional.notification(富通知)和外部URL,google-chrome-extension,notifications,Google Chrome Extension,Notifications,我正在尝试使用以下内容打开一个丰富的通知: var options = { templateType: "basic", title: "John Doe", message: "Lorem ipsum", iconUrl: "https://www.MyExternalURL.com/image.jpg" }; chrome.experimental.notification.create("notifyId", options, function(id) {

我正在尝试使用以下内容打开一个丰富的通知:

var options = {
    templateType: "basic",
    title: "John Doe",
    message: "Lorem ipsum",
    iconUrl: "https://www.MyExternalURL.com/image.jpg"
};

chrome.experimental.notification.create("notifyId", options, function(id) {
    console.log("Succesfully created notification");
});
var options = {
    templateType: "basic",
    title: request.name,
    message: request.message,
    iconUrl: chrome.runtime.getURL("/images/cat.png"),
};
但由于某些原因,这不起作用,但如果我将选项替换为以下选项:

var options = {
    templateType: "basic",
    title: "John Doe",
    message: "Lorem ipsum",
    iconUrl: "https://www.MyExternalURL.com/image.jpg"
};

chrome.experimental.notification.create("notifyId", options, function(id) {
    console.log("Succesfully created notification");
});
var options = {
    templateType: "basic",
    title: request.name,
    message: request.message,
    iconUrl: chrome.runtime.getURL("/images/cat.png"),
};
通知工作得很好

这是我清单文件中的重要内容

{
"manifest_version": 2,
"name": ...,
"description": ...,
"version": ...,
"icons": { 
    ...
},
"content_scripts": [
    ...
],
"background": {
    "scripts": ["background.js"]
},
"permissions": [
    "tabs", 
    "experimental"
],
"web_accessible_resources": [
    "https://www.MyExternalURL.com/*"
] 
}


我应该如何使用外部图像作为iconURL?

脚本和对象资源只能从扩展包加载,不能从整个web加载。这确保扩展只执行您特别批准的代码,防止活动网络攻击者恶意重定向您的资源请求

因此,您的代码无法加载外部JPEG图标
iconUrl:“https://www.MyExternalURL.com/image.jpg“

相反,从下载文件并将其内联,如方法2所示

更多关于
web\u accessible\u resources
不接受任何带有HTTP URL的内容,它只接受一个字符串数组,指定预期在网页上下文中可用的打包资源的路径(相对于包根)

参考文献

通过Ajax获取外部图像的blob,应用转换后的数据URI

var options = {
    type: "basic",
    title: "TITLE",
    message: "MESSAGE"
};

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.MyExternalURL.com/image.jpg");
xhr.responseType = "blob";
xhr.onload = function(){
        var blob = this.response;
        options.iconUrl = window.URL.createObjectURL(blob);
        chrome.notifications.create("notifyId", options, function(notId){});
    };
xhr.send(null);

但是如果image.jpg不是一个常量图像呢?而是依赖于通知?比如我是否需要为特定用户弹出一张个人资料图片?@MathiasVorreiterPedersen:您必须下载所有图像并将它们放在根文件夹中:):)或者执行ajax请求以获取图像块并显示它们。。