chrome.cookies.set甚至没有运行

Sta*_*sey 1 javascript google-chrome-extension

以下代码由于某些原因不起作用:

Background.js

alert("1");
chrome.cookies.set({ url: "https://mywebsite.com", name: "SuperDuperTest" });
alert("2");
Run Code Online (Sandbox Code Playgroud)

的manifest.json

"权限":["通知"," https://mywebsite.com ","cookies","http://*/*","https://*/*"],

仅提醒("1"); 永远火了.警报2从不这样做,为什么我的铬饼干根本不会被解雇?

Sud*_*han 7

在哪里查看您的cookie是否已设置?请使用console.log()而不是alert()

示例代码

的manifest.json

已注册的后台页面和Cookies API的授权.

{
    "name": "Cookie API Demo",
    "version": "1",
    "description": "http://stackoverflow.com/questions/14448039/chrome-cookies-set-doesnt-even-run",
    "permissions": [
        "cookies",
        "<all_urls>"
    ],
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "manifest_version": 2
}
Run Code Online (Sandbox Code Playgroud)

background.js

cookies.html页面设置cookie的简单代码

chrome.cookies.set({
    "name": "Sample1",
    "url": "http://developer.chrome.com/extensions/cookies.html",
    "value": "Dummy Data"
}, function (cookie) {
    console.log(JSON.stringify(cookie));
    console.log(chrome.extension.lastError);
    console.log(chrome.runtime.lastError);
});
Run Code Online (Sandbox Code Playgroud)

产量

转到https://developer.chrome.com/extensions/cookies.html并打开如此处所示的开发人员工具,您可以看到正在设置cookie!

单击"大图"

在此输入图像描述

进一步调试

如果此示例代码不起作用,则chrome.extension.lastError和的值是chrome.runtime.lastError什么?