【问题标题】:Get gmail message IDs using a Chrome extension使用 Chrome 扩展程序获取 gmail 消息 ID
【发布时间】:2023-04-21 21:47:01
【问题描述】:

我正在尝试编写一个 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);
}

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

谢谢

【问题讨论】:

  • 我想我可以使用 Google API 获取发件人、主题、接收时间和搜索,但这似乎有点笨拙。
  • 不是一个完整的答案,但gmail.js 是一个很好的匹配。

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


【解决方案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;
        }

【讨论】: