Git Product home page Git Product logo

chrome-extension-cookies-sample's Introduction

Chrome Sample Cookie Use Extension

This extension demonstrates reading cookies from a website from a Chrome extension.

One use case which seemingly requires reading cookies is performing XMLHttpRequest calls from one website to another, for example to present data from the second website to the first website. It appears that if such a request is initiated from an extension then despite the browser having cookies for the target site, these cookies are not passed with the XHR automatically. Setting these cookies on an XHR object manually requires manually obtaining said cookies, which is the process this sample extension covers.

Mechanism

As explained here a Chrome extension has 3 execution contexts: content, page and background. The extension code normally runs in either the page context or the content context, and cookies can only be read from the background context. As such the page context passes a custom event to the content context which sends a Chrome message to the background context; code in the background context listens for Chrome messages, reads cookies and replies to the messages with cookie values; code in the content context receives the reply from the background context and forwards the data via another custom event to the page context.

It seems possible to move some or all of the code which runs in the page context to the content context, in which case half of the message passing will cease to be necessary.

Step By Step

Let's build the Chrome extension!

Backend

In order to demonstrate the extension we need two Web sites on different domains. To this end there are two Python scripts in this repository:

  • host.py is the "host" Web page. It runs on port 8112 and I reference this site via a /etc/hosts alias striker as follows: http://striker:8112/.
  • server.py is the "remote" Web page.It runs on port 8192 and I reference this site as http://localhost:8192/.

Both servers can set a cookie named now. Initially the cookie is not set. Go to http://localhost:8192/ and click "Set cookie" - you will have the cookie set on localhost but not on striker. If you go to http://localhost:8112/ the cookie will be set - the cookie is bound to the hostname, not to port, which is why we need two separate hostnames.

Install The Extension

Next, install the extension in this repository via "Load unpacked extension" method.

Page To Content: Custom Event

The cookie retrieval is triggered when the host page loads. In the page context we create a custom event containing the action we want our background code to perform (getCookies), the URL to retrieve the cookies for and the name of the cookie to retrieve. This event is then dispatched:

// our payoad
var message = {
  action: 'getCookies',
  url: 'http://localhost:8192/',
  cookieName: 'now',
};
var event = new CustomEvent("PassToBackground", {detail: message});
window.dispatchEvent(event);

Content script listens for this event and forwards it to the background context:

window.addEventListener("PassToBackground", function(evt) {
  // evt.detail has our message/payload
  chrome.runtime.sendMessage(evt.detail, function(response) {
    // handle response from background context - will write later
  });
}, false);

Page and content scripts both have access to the page's DOM including the window object and events, hence these scripts are able to communicate by passing custom events between each other.

Content To Background: Chrome Messaging

As shown above, the content code uses chrome.runtime.sendMessage API to send an event to the background code:

  chrome.runtime.sendMessage(evt.detail, function(response) {
    // handle response from background context - will write later
  });

The background code listens for this event and reads the cookies:

// listen for sendMessage events
chrome.runtime.onMessage.addListener(
  // our event handler
  function(request, sender, sendResponse) {
    if (request.action == "getCookies") {
      // handle the event.
      // request has our message/payload.
      // perform security checks here, then do the work.
      // call sendResponse when done.
      // if sendResponse is called asynchronously,
      // this event handler must return true
    }
  }
);

Reading Cookies

We need to send something back, hence let's read the cookies on the target site. This is easy:

if (request.action == "getCookies") {
  chrome.cookies.get({url: request.url, name: request.cookieName},
    function(cookie) {
      // return the cookie to content context
      sendResponse({
        cookieName: request.cookieName,
        cookieValue: cookie && cookie.value,
      });
  })
  // cookie retrieval is asynchronous, event handler must return true
  return true;
}

The important point here is that cookie retrieval is asynchronous -

Resources

Chrome's 3 layers (content, page and background):

Passing a message from page context to content context:

Passing a message from content context to background context:

Specifics on reading cookies:

chrome-extension-cookies-sample's People

Contributors

p avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

chrome-extension-cookies-sample's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.