0

I'm trying to include gmail.js from here. But the object gmsrc is invalid, the function get.user_email() fails. I'm trying to develop a Chrome extention, the chrome developer tools (F12) tells me

Uncaught TypeError: Cannot read property 'user_email' of undefined

Here is my code:

var jq = document.createElement('script');
jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js";
document.getElementsByTagName('body')[0].appendChild(jq);
var gmsrc = document.createElement('script');
gmsrc.src = "https://rawgit.com/KartikTalwar/gmail.js/master/src/gmail.js";
document.getElementsByTagName('body')[0].appendChild(gmsrc);
console.log("Scripts loaded, Start playing ...");
var name = gmsrc.get.user_email();
alert(name);

Where is my mistake?

Update (still not working):

I tried this, but it doesnt work either:

    var jq = document.createElement('script');
    jq.src = chrome.extension.getURL("lib/jquery-2.1.1.min.js");
    document.body.appendChild(jq)

    //inject gmail.js
    var gmsrc= document.createElement('script');
    gmsrc.src = chrome.extension.getURL("lib/gmail.js");
    document.body.appendChild(gmsrc);

    var name = gmsrc.get.user_email();
    alert(name);
superstar
  • 1
  • 2

2 Answers2

0

Gmail.js is a framework very useful for work around mail.google.com website as a plugin. You have to build the project first from https://github.com/josteink/gmailjs-node-boilerplate follow the readme and using node.js create the Chrome extension and play with it

-1

Looking to your code, the gmsrc variable is a reference to the script element created with the source from gmail.js not the Gmail script logic, that's why you're getting this error.

The correct way to use the API is to make the include (like you did) and after make some like:

var gmail = Gmail();
var name = gmail.get.user_email();
// Returns the user e-mail here;
  • thanks, but then the error is "Uncaught ReferenceError: Gmail is not defined" – superstar Jan 02 '15 at 17:26
  • The Gmail will be only avaliable on global scope after the the gmsrc finished de load process. To avoid error like this use the `load` listener. The `onload` it's a event that javascript triggers when the object that you're including finished the load process. Make the use like this: `gmsrc.onload = function () { // logic here }`. – Júlio Gori Corradi Jan 02 '15 at 17:50
  • Here some example with your code and the listener: http://codepen.io/anon/pen/NPbdNm – Júlio Gori Corradi Jan 02 '15 at 18:14
  • 1
    I tried that, but still the same error: "Uncaught ReferenceError: Gmail is not defined" – superstar Jan 02 '15 at 18:39