Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create Gmail Filters Programmatically?

I would like to create Gmail filters programmatically in a Chrome extension for standard Gmail users (read non Google App). Currently, thanks to those 2 (outdated) resources:

  1. Grexit Filters API
  2. Gmail Filter Assistant, Userscript

I have managed to develop an early prototype able to create a filter which automatically archives ("cf2_ar=true") emails received at a certain email address ("cf1_to="). However, it seems like any other "cf2" parameters used by Gmail are not valid anymore. For instance, applying a label with parameters "cf2_cat=" or "cf2_sel=" does nothing.

I know that Gmail's code is not very friendly and open when it comes to app or extension development but I would appreciate any help if some of you have any ideas, suggestions or updates regarding the current parameters used by Gmail to create filters, especially the one(s) used to apply labels to messages.

script.js (DOM end)

// Inject API into Gmail DOM
var s = document.createElement('script');
s.src = chrome.runtime.getURL('js/lib/api.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
    s.parentNode.removeChild(s);
};

document.addEventListener('Demo_connectExtension', function(e) {

if(e.detail) {

   // Gmail GLOBALS : DATA
    DATA = e.detail;
    user_data[0] = DATA[9]; // Id (ik)
    user_data[1] = DATA[10]; // Email
    user_data[2] = DATA[17][9][8]; // Locale
    user_data[3] = DATA[7]; // Gmail inbox

    var emailarr = user_data[1].split('@');
    user_data[4] = emailarr[0] + '+do@' + emailarr[1]; // email for filter


    var regex_cookie = new RegExp("GMAIL_AT=(.+?);");

    if (typeof document.cookie !== 'undefined') {

            // Get cookie
        var gmcookie = document.cookie.match(regex_cookie)[1];
        console.log('cookie:' + gmcookie);


        var gmail_filter_url =  'https://mail.google.com' + user_data[3] + 
                                '/?ui=2&ik=' + user_data[0] +
                                '&at=' + gmcookie +
                                '&view=up&act=cf&pcd=1&mb=0&rt=c';

        var postdata = 'search=cf&cf1_to=' +
                        encodeURIComponent(user_data[4]) +
                        '&cf2_ar=true';

        $.post(gmail_filter_url, postdata, function(gmail_response){
            console.log(gmail_response);
            });
    }

      // [...]

api.js (injected into Gmail)

'use strict';

var GLOBALS;

setTimeout(function() {
    /* Send Gmail Data to my extension */
    document.dispatchEvent(
        new CustomEvent('Demo_connectExtension', {
            detail: GLOBALS
        }));
}, 1);
like image 660
flo Avatar asked Nov 11 '22 11:11

flo


1 Answers

Actually, cf2_sel parameter does work for me. I had somewhat similar issue, described in this question and answer.

My code uses Grexit filters project and solves a sample use case:

"When a keyword is present in email content, add label, archive and set filter for this keyword"

I tested it successfully and when looking at Grexit code, 'labelas' action sets abovementioned "cf2_sel" parameter.

My sample project can be found here and provide you with means for quick testing and verification if this works for you.

like image 194
zencodism Avatar answered Nov 15 '22 10:11

zencodism