google-chrome-extensiongmail-apigoogle-email-settings-api

How to automatically set gmail filter via chrome extension?


I would like to implement the following use case as a Chrome extension:

The first part sounds easier: there is gmail API to work with and even a gmail.js project that should make it easier.

Adding filter seems to be much harder. There is email settings API doing precisely what I want but I am fairly sure it is usable only by business accounts (custom email domains, won't work for gmail.com). I want the solution to be more universal.

One thing I thought of was to use browser automation - upon seeing the trigger keyword, the script automatically clicks 'Add filter' link, waits for AJAX, sets filter parameters and confirms.

An example of simulated user activity is in this answer

This could happen either on gmail page behind the popup ('Please wait, adjusting filters') or in background tab to keep it from interfering with user's flow. This seems like ugly workaround for me, though.

Is there a more straightforward or simply better approach that I'm missing?


Solution

  • After more experimentation and reviving an older github project I found out that setting the filter for a logged in user can be achieved simply by issuing a specific POST message to gmail from the current session.

    I don't fully understand the parameters used in this request (if anyone has better information, please share), but I found a sample code which was greatly helpful.

    Second issue, widely discussed in gmail.js community, is that Gmail security policies will prevent you from injecting your own scripts. This is bypassed by method shown in this boilerplate project

    I compiled these solutions to solve my particular use case. Here is an example project with my solution, which should work out of the box - and when in doubt, see readme.