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);