0

I tried to use gmail.js for getting the current user email so I can check that email with the email used in popup.js for logging in. I need the current user email from the currently opened gmail as soon as possible so I can store it in storage and get from there to check if provided email from login matches with the email of that gmail or not. However, I am getting Error in event handler for tabs.onUpdated: ReferenceError: Gmail is not defined issue.

How do i solve this?

here is my file

{
    "page_action": {
        "default_title": "Browser Extension",
        "default_popup": "index.html"
    },
    "background": {
        "scripts": ["extension/js/background.js"],
        "persistent": true
    },
    "options_page": "index.html",
    "content_scripts": [
        {
            "matches": [
                "https://mail.google.com/mail/*/*",
                "http://mail.google.com/mail/*/*"
            ],
            "js": [
                "extension/js/jquery.min.js",
                "extension/js/gmail.js",
                "extension/js/content.js"
            ],
            "css": ["extension/css/main.css"],
            "run_at": "document_end"
        }
    ],
    "permissions": [
        "contextMenus",
        "tabs",
        "storage",
        "webNavigation",
        "notifications",
        "cookies",
        "identity",
        "identity.email",
        "*://mail.google.com/mail/*/*",
    ],
}


background.js

function check(tabId, changeInfo, tab) {
    const filter = [
        'https://mail.google.com/',
        'http://mail.google.com/',
        'https://inbox.google.com/'
    ];
  if (changeInfo.status !== undefined && changeInfo.status === 'complete') {
        console.log('completed');
        var gmail = new Gmail(); // I get error here 
        console.log('Hello from background', gmail.get.user_email());
    }
    filter.forEach(item => {
        if (tab.url.indexOf(item) >= 0) {
            chrome.pageAction.show(tabId);
        }
    });
}

chrome.tabs.onUpdated.addListener(check);

if i instead use in content script i get undefined.

content.js

const main = function() {
    // NOTE: Always use the latest version of gmail.js from
    // https://github.com/KartikTalwar/gmail.js
    var j = document.createElement('script');
    j.src = chrome.extension.getURL('jquery.min.js');
    console.log('j', j);
    (document.head || document.documentElement).appendChild(j);

    var g = document.createElement('script');
    g.src = chrome.extension.getURL('gmail.js');
    (document.head || document.documentElement).appendChild(g);
    console.log('main');
    const gmail = new Gmail();
    console.log('Hello,', gmail.get.user_email()); // undefined
};

refresh(main);
milan
  • 2,409
  • 2
  • 34
  • 71

1 Answers1

0

"Gmail" is not Google Apps built-in and needs to be enabled under "Advanced service".

Follow the steps to enable it:

https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services

Jon
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 08 '22 at 06:12
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32405171) – Webdeveloper_Jelle Aug 09 '22 at 11:09