简体   繁体   English

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

[英]Issue in tagging gmail mails with gmail API chrome extension

I am trying to develop a functionality in which the selected mails should get tagged with "general" label.我正在尝试开发一种功能,在该功能中,所选邮件应该被标记为“常规”标签。 This functionality should get executed on button click so for the button creation I used inboxsdk API and for the tagging functionality to work on button click I used gmail API but I am facing issue in executing the code.此功能应该在按钮单击时执行,因此对于按钮创建我使用了 inboxsdk API 和标记功能在按钮单击上工作我使用了 gmail API 但我在执行代码时遇到了问题。 I am getting the below error in tag.js file我在 tag.js 文件中收到以下错误

Uncaught SyntaxError: Unexpected token <未捕获的语法错误:意外的标记 <

I am posting the code below.我在下面发布代码。 Please check it.请检查一下。

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

Anyone with relevant solution will be greatly appreciated.任何有相关解决方案的人都将不胜感激。

Doesn't seem like you grasp the extension architecture at all.似乎您根本没有掌握扩展架构

You declared the index.html to be a background page - an invisible page that a user can't interact with.您将index.html声明为背景页面 - 用户无法与之交互的不可见页面。 So why does it even have a button in it?那么为什么它甚至有一个按钮呢?

Also, you cannot have any kind of inline code in Chrome extensions, and it's impossible to override.此外,您不能在 Chrome 扩展程序中使用任何类型的内联代码,并且无法覆盖。 You must put your extension code in script files (and therefore should consider declaring the background page in the "new" style by specifying scripts array instead of a page .您必须将扩展代码放在脚本文件中(因此应该考虑通过指定scripts数组而不是 page 来以“新”样式声明背景page

I doubt that fixing those will make your extension work as intended (you probably don't want this code in the background), but that answers your initial question with the specific error.我怀疑修复这些会使您的扩展程序按预期工作(您可能不希望在后台运行此代码),但这会通过特定错误回答您的初始问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM