使用gmail API chrome扩展标记gmail邮件时出现的问题

Issue in tagging gmail mails with gmail API chrome extension

本文关键字:gmail 问题 chrome API 扩展 使用      更新时间:2023-09-26

我正在尝试开发一种功能,在该功能中,所选邮件应该被标记为"通用"标签。这个功能应该在按钮点击时执行,所以对于按钮创建,我使用inboxsdk API,对于标签功能,我使用gmail API来执行按钮点击,但我在执行代码时面临问题。我在标签.js文件中得到以下错误

未捕获的语法错误:意外的标记<

我在下面张贴代码。请检查一下。

manifest.json:-
{
"name": "Gmail Extension",
"description": "Extension for tagging",
"version": "0.1",
"manifest_version": 2,
"minimum_chrome_version": "29",
"background": {
"page": "/background/index.html"
  },
"content_scripts": [
{
"matches": [
"https://mail.google.com/*",
"https://inbox.google.com/*"],
"js": ["/libs/inboxsdk.js", "/libs/alertify/alertify.min.js", "/contentScript/tag.js"],
"css": ["/libs/alertify/alertify.default.css", "/libs/alertify/alertify.core.css"],
"run_at": "document_end"
}],
"web_accessible_resources": ["/icons/tag.png", "*"],
"permissions": ["identity", "<all_urls>", "tabs", "webRequest", "webRequestBlocking", "https://accounts.google.com/*", "https://www.googleapis.com/*", "https://mail.google.com/",
"https://inbox.google.com/"],
"content_security_policy": "script-src 'self' 'sha256-Y+2PBkTuXdKc9Mz9jB6CV7zSLRMuViwjLM28phOgupM=' https://apis.google.com; object-src 'self'",
"oauth2": {
"client_id": "763145023672-pomd352gi79664h9tf0hg1uu160s4hop.apps.googleusercontent.com",
"scopes": ["https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.labels",
"https://www.googleapis.com/auth/gmail.send"]
  }
  } 
index.html:-
<!DOCTYPE html>
<html>
<head>
<title>Extension for tagging</title>
<meta charset='utf-8' />
<script src = "auth.js" </script>
<script src = "/libs/inboxsdk.js" </script>
<script src = "/contentScript/tag.js" </script>
</head>
<body>
<div id="authorize-div" style="display: none">
<span>Authorize access to Gmail API</span>
<!--Button for the user to click to initiate auth sequence -->
<button id="authorize-button" onclick="handleAuthClick(event)">
Authorize
</button>
</div>
</body>
</html>
auth.js:-
var CLIENT_ID = '763145023672-pomd352gi79664h9tf0hg1uu160s4hop.apps.googleusercontent.com';
var SCOPES = [
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.labels',
'https://www.googleapis.com/auth/gmail.send'
 ];
function checkAuth() {
gapi.auth.authorize({
'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true
},
handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadGmailApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
function handleAuthClick(event) {
gapi.auth.authorize({
'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': false
},
handleAuthResult);
return false;
}
function loadGmailApi() {
gapi.client.load('gmail', 'v1', updateLabel);
updateLabel();
}
 <script type="text/javascript" src="https://apis.google.com/js/client.js?onload=checkAuth"></script>
tag.js:-
function updateLabel() {
var request = gapi.client.gmail.users.labels.update({
'userId': 'me'
});
request.execute(function(resp) {
function whenNoneSelected(route) {
  return false;
}
function register(sdk) {
sdk.Toolbars.registerToolbarButtonForList({
title: 'General',
section: sdk.Toolbars.SectionNames.INBOX_STATE,
iconUrl: chrome.extension.getURL('/icons/tag.png'),
onClick: tagThread(){
var label = GmailApp.getUserLabelByName("General");
var threads = label.getThreads(); // var threads = GmailApp.getThreads();
for (var i=0; i<threads.length; i++) {
//add label "General" for selected threads
threads[i].addLabel(label);
}
alertify.success('Threads tagged as General');
},
hasDropdown: false,
hideFor: whenNoneSelected,
keyboardShortcutHandle: null
});
}
InboxSDK.load('1', 'sdk_mailtag_fd47af3e65').then(register);
});
}

任何有相关解决方案的人都将不胜感激。

您似乎根本没有掌握扩展体系结构。

您将index.html声明为后台页面——一个用户无法与之交互的不可见页面。为什么它里面有一个按钮呢?

此外,Chrome扩展中不能有任何类型的内联代码,也不可能重写。您必须将扩展代码放在脚本文件中(因此,应该考虑通过指定scripts数组而不是page来以"新"样式声明背景页。

我怀疑修复这些问题是否会使您的扩展按预期工作(您可能不希望在后台使用此代码),但这会用特定的错误回答您最初的问题。