Unable to execute the gmail chrome extension

169 views
Skip to first unread message

Shivani

unread,
Nov 19, 2015, 1:18:54 AM11/19/15
to Chromium-extensions
Hi actually I am trying to build a chrome extension for bulk tagging functionality by using gmail api. As there is no chance to build a button UI with gmail api I am using inboxsdk for button creation in gmail inbox and for bulk tagging functionality to work on button click I am using gmail api. In the sense when the button is clicked the selected mails should get tagged with the "general" label in gmail inbox. The button which I am using will be displayed in the gmail toolbar next to delete button in existing gmail scenario.
Now where I am facing the issue is that When I am debugging the code I am not finding any errors but when I am trying to execute the code. The mail is automatically redirected to gmail login page and this login page is only getting repeated every time.

I am pasting the UI of actual needed scenario below



Now I am posting the code below

    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 type="text/
javascript" src="https://apis.google.com/js/client.js?onload=checkAuth"></script>

   
<script src = "background.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();
    }
   
    background.js:-


    chrome.identity.getAuthToken({
     'interactive': true
     }, function(token) {
      // Use the token.
     });


    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.

PhistucK

unread,
Nov 19, 2015, 1:47:35 AM11/19/15
to Shivani, Chromium-extensions
Looking at the code, you use an inline event handler in your background page, which does not work in extensions. You must add an event listener in your external JavaScript instead.
By the way, you can use chrome.runtime.getManifest() to get manifest values, instead of hard-coding the scopes and the client ID in your script as well as in your manifest.

Also, to make it easier for others to help and debug, attach a ZIP of your extension so anyone could easily load it as an unpacked extension and debug it.


PhistucK

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/99207568-e786-495f-824e-a42262909191%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Shivani

unread,
Nov 19, 2015, 2:09:36 AM11/19/15
to Chromium-extensions
Hi guys I am attaching the complete ZIP folder of my extension. Please check and revert back with relevant solution.
Gmail Extension.zip
Reply all
Reply to author
Forward
0 new messages