1

I have a background script in my extension that creates a context menu item and handles it. When it is clicked, a cookie is created with specific details. Here is the source for that file:

function createC() {
    var x = 1;
    var y = 2;
    //Create Cookie
    document.cookie = document.URL + "=" + " " + x + " " + y + " ; " + 4102444799;
    console.log("Cookie Created."); 
}

chrome.contextMenus.create({

    title: "Create Cookie", 
    contexts:["all"], 
    onclick: createC,

});

Obviously the variables used in it are for testing. When I run document.cookie; in the console, the cookie does not appear. I have tried using the chrome.cookies API and had no luck.

Does the cookie not appear because it is created in the background script?

Edit: manifest.json

{
  "manifest_version": 2,

  "name": "MyExtension",
  "description": "Do stuff",
  "version": "0.1",
  "icons": { "16": "icon.png",
           "48": "icon.png",
          "128": "icon.png" },
  "options_page": "options.html",
  "permissions": [
    "tabs", "<all_urls>", "contextMenus", "cookies"
  ],
  "background": {
    "scripts": ["script.js"]
  },
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["nav.js"]
    }
  ]
}
  • Does the function still get called on click? Do you get the Cookie Created print out? – CubanAzcuy Feb 23 '14 at 19:06
  • After more reading I a little confused do you intend this cookie to be added to your background page or to the current page? http://stackoverflow.com/questions/21199965/accessing-cookies-in-chrome-extension – CubanAzcuy Feb 23 '14 at 19:20
  • I do get the Cookie Created print out. I intend for the cookie to be added to the current page, for example, youtube.com, not the background page. I have the permission in my manifest, so that can't be the issue. – user3339723 Feb 23 '14 at 19:27
  • You should be using [chrome.cookies.set](http://developer.chrome.com/extensions/cookies#method-set) – abraham Feb 23 '14 at 19:32
  • @abraham I was using it, but I had the same problem. The cookie was not set to the page. When using `chrome.cookies.get` with the cookie's defined values, it returned undefined. – user3339723 Feb 23 '14 at 19:33
  • what does your permissions array look like. – CubanAzcuy Feb 23 '14 at 20:05
  • @CubanAzcuy Added it to my post. – user3339723 Feb 23 '14 at 20:25

0 Answers0