Access Gmail attachment from a chrome extension?

2020-06-29 02:35发布

问题:

Hi I am trying to write a chrome extension which needs to read in an email attachment (plain txt). I feel this should be possible as gmail gives you a download link, however it is not a direct link does this make it impossible?

I have the following code to read a remote file which works just not for gmail:

<script>
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "http://remote.com/remote_file", true);
txtFile.onreadystatechange = function() {
  if (txtFile.readyState === 4) {  // Makes sure the document is ready to parse.
    if (txtFile.status === 200) {  // Makes sure it's found the file.

        allText = txtFile.responseText; 
        lines = txtFile.responseText.split("\n"); // Will separate each line into an array
        alert(allText)
    }
}
}
txtFile.send(null);
</script>

Does anybody know how I can read a gmail attachment like this? thanks

回答1:

Gmail has a link to original email source ("Show Original" from the menu). Not sure if it is possible to read it programmatically, but if it is, I would try to parse original message source instead and get attachments from there (they are base64 encoded I beleive).