How to automatically set gmail filter via chrome extension?

假装没事ソ 提交于 2019-12-20 07:19:08

问题


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

  • user visits gmail
  • exension checks current email body for a keyword
  • if a keyword is present, a gmail filter is added and saved (adding label, archiving, the details are not important here)

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?


回答1:


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.



来源:https://stackoverflow.com/questions/29607996/how-to-automatically-set-gmail-filter-via-chrome-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!