我尝试使用gmail.js来获取当前用户的电子邮件,以便可以将其与popup.js中用于登录的电子邮件一起检查。我需要尽快从当前打开的gmail中获取当前用户的电子邮件,因此我可以将其存储在存储中,然后从那里检查登录提供的电子邮件是否与该gmail的电子邮件匹配。但是,我遇到Error in event handler for tabs.onUpdated: ReferenceError: Gmail is not defined
问题。
我该如何解决?
这是我的文件
{
"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);
如果我改为在内容脚本中使用,则无法定义。
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);