【问题标题】:Issue in tagging gmail mails with gmail API chrome extension使用 gmail API chrome 扩展标记 gmail 邮件的问题
【发布时间】:2016-02-09 19:27:32
【问题描述】:

我正在尝试开发一种功能,在该功能中,所选邮件应带有“一般”标签。此功能应该在按钮单击时执行,因此对于按钮创建我使用 inboxsdk API 和标记功能在按钮单击时工作我使用了 gmail API,但我在执行代码时遇到问题。我在 tag.js 文件中收到以下错误

Uncaught SyntaxError: Unexpected token

我在下面发布代码。请检查一下。

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

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

【问题讨论】:

  • 我删除了我的答案,因为它没有帮助。你能告诉我你想做什么吗?在后台页面中有 UI 元素是没有用的。
  • 您好实际上我正在尝试使用 gmail api 为批量标记功能构建 chrome 扩展。由于没有机会使用 gmail api 构建按钮 UI,我正在使用 inboxsdk 在 gmail 收件箱中创建按钮,并使用批量标记功能来处理按钮单击,我正在使用 gmail api。在单击按钮时,所选邮件应在 gmail 收件箱中使用“常规”标签进行标记。我正在使用的按钮将显示在现有 gmail 场景中的删除按钮旁边。
  • 然后使用内容脚本来定义点击事件。要使用 gmail API,您必须将其注入页面。一旦注入,它将在页面上下文中运行,因此也需要注入回调函数。
  • 意思是从谷歌开发者网站注入gmail API,我在上面的index.html页面中写了授权代码。
  • 其实客户端js需要从apis.google.com加载。在本地下载它不起作用,因为它取决于其他脚本。那么如何让它在 gmail 中工作 - 你需要将此库加载到 gmail 页面中,即使用内容脚本将其注入到 gmail 页面中。还要确保回调函数也被注入。

标签: javascript jquery google-chrome-extension gmail-api inbox


【解决方案1】:

您似乎根本没有掌握extension architecture

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

此外,您不能在 Chrome 扩展程序中包含任何类型的内联代码,并且无法覆盖。您必须将扩展代码放在脚本文件中(因此应考虑通过指定 scripts 数组而不是 page 来以“新”样式声明背景页面。

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

【讨论】:

  • 您好,实际上我遵循了仅从 github 提取的 Google 开发人员链接下提到的示例之一的扩展架构。无论如何,我更改了结构并再次发布了上述当前存在的错误。
猜你喜欢
  • 2019-04-17
  • 2023-03-20
  • 2012-03-24
  • 2012-08-18
  • 1970-01-01
  • 2017-04-28
  • 2019-10-26
  • 2014-11-04
  • 1970-01-01
相关资源
最近更新 更多