Change cookies when doing jQuery.ajax requests in Chrome Extensions

Posted by haskellguy on Stack Overflow See other posts from Stack Overflow or by haskellguy
Published on 2012-12-08T18:39:21Z Indexed on 2012/12/09 5:05 UTC
Read the original article Hit count: 140

I have wrote a plugin for facebook that sends data to testing-fb.local. The request goes through if the user is logged in.

Here is the workflow:

  1. User logs in from testing-fb.local
  2. Cookies are stored
  3. When $.ajax() are fired from the Chrome extension
    • Chrome extension listen with chrome.webRequest.onBeforeSendHeaders
    • Chrome extension checks for cookies from chrome.cookies.get
    • Chrome changes the Set-Cookies header to be sent

And the request goes through.

I wrote this part of code that shoud be this:

function getCookies (callback) {
  chrome.cookies.get({url:"https://testing-fb.local", name: "connect.sid"}, function(a){
    return callback(a)
  })
}

chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
    getCookies(function(a){

// Here something happens

    })
  },
  {urls: ["https://testing-fb.local/*"]},
  ['blocking']);

Here is my manifest.json:

{
  "name": "test-fb",
  "version": "1.0",
  "manifest_version": 1,
  "description": "testing",
  "permissions": [
    "cookies",
    "webRequest",
    "tabs",
    "http://*/*",
    "https://*/*"
  ],    
  "background": {
    "scripts": ["background.js"]
  },
  "content_scripts": [
        {
          "matches": ["http://*.facebook.com/*", "https://*.facebook.com/*"],
          "exclude_matches" : [
            "*://*.facebook.com/ajax/*",
            "*://*.channel.facebook.tld/*",
            "*://*.facebook.tld/pagelet/generic.php/pagelet/home/morestories.php*",
            "*://*.facebook.tld/ai.php*"
          ],
          "js": ["jquery-1.8.3.min.js", "allthefunctions.js"]
        }
      ]
}

In allthefunction.js I have the $.ajax calls, and in background.js is where I put the code above which however looks not to run..

In summary, I have not clear:

  • What I should write in Here something happens
  • If this strategy is going to work
  • Where should I put this code?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery