使用Chrome扩展程序获取Gmail邮件ID

时间:2014-04-02 14:50:34

标签: javascript google-chrome google-chrome-extension gmail

我正在尝试编写一个Chrome扩展程序,该扩展程序会将收到的所有电子邮件都收集到收件箱中并进行处理。实际处理位已经构建,目前位于上下文小工具中,因此可以重复使用。

这是我到目前为止所处的位置。

{
  "name": "Test execution",
  "description": "Does this trigger myscript.js",
  "version": "1.0",
  "permissions": [
    "tabs",
    "activeTab",
    "http://*/"
  ],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },

  "browser_action": {
    "default_title": "Does it execute?"
  },
  "manifest_version": 2
}

background.js:

chrome.browserAction.onClicked.addListener(function(tab) {

    chrome.tabs.executeScript(null, {file: "myscript.js"},function() {
        if (chrome.runtime.lastError) {
            console.error(chrome.runtime.lastError.message);
        }
    });

});

myscript.js:

var x = document.getElementsByClassName("zA yO x7");
alert(x.length);

for (var i = 0; i < x.length; i++) {
    alert(x[i].innerHTML);
}

这是正确选择仅勾选的消息,但我无法看到如何从那里获取实际消息 - 只有收件箱中可见的预览信息。有什么想法吗?

由于

1 个答案:

答案 0 :(得分:1)

我知道这已经很老了,但我正在做类似的事情,我想分享我的解决方案。请注意,这不是一个优雅或干净的解决方案,但它起作用,至少在Google决定更改其Gmail用户界面之前。

我解决方案的关键是使用VIEW_DATA变量,该变量包含大量有关电子邮件的信息,包括其ID。

首先,我们必须获取所选电子邮件的索引:

    var selectedIndexes = [];
    var checkBoxes = $jQcl('[role="checkbox"]');
    //Looks for selected mails
    for (var i = 0; i < checkBoxes.length; i++) {
        if (checkBoxes[i].attributes.class &&
            checkBoxes[i].attributes.class.value.indexOf("oZ-jc") > -1  &&
            checkBoxes[i].attributes['aria-checked'] &&
            checkBoxes[i].attributes['aria-checked'].value == "true")
            {
                //Saves the index of the selected mail
                selectedIndexes.push(i);
            }
    }

    // Iterates through the indexes of the selected mails and gets their IDs
    var selectedIds = [];
    selectedIndexes.forEach( function(elem) {
        selectedIds.push(getMailID(elem));
    });

    return selectedIds;

getMailID函数:

        //Returns the ID of the email looking for its index.
        //
        //<param name="indexSelected">Index of the selected email</param>
        var getMailID = function (indexSelected)
        {
            // Puts the index in (0-9) array format
            indexSelected--;

            // gets all of the <script> elements on the page
            var scripts = document.getElementsByTagName( 'script' ),
                thisScript, varViewDataPos, viewDataScript, viewData;

            // loop through each one looking for VIEW_DATA being defined
            for( var i = 0; i < scripts.length; i++ ) {
                thisScript = scripts[ i ].textContent;
                varViewDataPos = thisScript.indexOf( 'var VIEW_DATA=' );

                if( varViewDataPos >= 0 ) {
                    // might as well toss everything before VIEW_DATA is defined
                    viewDataScript = thisScript.slice( varViewDataPos );
                    break;
                }
            }

            // eval what we found (if anything), but in a closure to avoid polluting
            // the global namespace
            viewData = ( function( script ) {
                eval( script );
                return VIEW_DATA;
            } )( viewDataScript );

            //Goes through the VIEW_DATA looking for the mails info
            //I know this is ugly, but it works
            preDataMail = viewData[Math.floor(indexSelected/10)+3];
            preDataMail = preDataMail[2];
            preDataMail = preDataMail[indexSelected % 10];
            preDataMail = preDataMail[2];

            //Returns the mail ID
            return preDataMail;
        }