Get extension permissions

Asked

Viewed 109 times

0

I’m trying to get permissions from the user when a gmail link is posted on facebook. The goal is to share gmail content on facebook.

//code from facebook
window.fbAsyncInit = function() {
    FB.init({
        appId      : '326350284188834',
        xfbml      : true,
        cookie     : true, // enable cookies to allow the server to access the session
        status     : true, // check login status
        version    : 'v2.0'
    });
};

(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];

  if (d.getElementById(id))
      return
      js = d.createElement(s); 
      js.id = id;
      js.src = "sdk.js";
      js.async = true;
      fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));


//Get current URL to share in facebook
chrome.tabs.getSelected(null, function(tab) {

    //cut permission in gmail
    chrome.permissions.request({
        permissions: ['tabs'],
        origins: ['http://www.google.com/']
    }, function(teste) {
        chrome.permissions.contains({
        permissions: ['tabs'],
        origins: ['http://www.google.com/']}, 
        function(result) {
            if (result) {
                var teste="pede permissão1";
                console.log(teste);
            } else {
                var teste="não pede permissão1";
                console.log(teste);
            }
        });

        if (teste=="pede permissão") {
            console.log('permissão pedida'); 
        } else {
            console.log('permissão não pedida');
            document.getElementById('share2').innerHTML = tab.url;
            document.getElementById('share2').innerHTML = tab.data;
            console.log(tab.data);
            document.querySelector("div.fb-share-button").setAttribute("data-href", tab.url);
        }
    });
});

html:

<!doctype html>
<html>
    <head>
        <!--<link rel="stylesheet" href="http://s3-ak.buzzfed.com/static/css/webfonts.css?v=1370354313" type="text/css" />-->
        <script src="fbbutton.js"></script>
       <!-- <script src="settings.js"></script>-->
        <!--<script src="sdk.js"></script>-->
        <style>
          div span, iframe {
            height:50px!important;
            width:100px!important;
          }
        </style>
    </head>
    <body>
        <div style="width:160px; height:200px; font: normal 21px/23px 'ProximaNovaSemibold',Helvetica,Arial,sans-serif">
        <div id="fb-root"></div>
            <form id="share" action="" method="">
                <label>
                    <input type="checkbox" id="share2" name="share" /> <span style="position:relative; top:3px;margin-bottom:20px;c">Quer partilhar no face?</span>
                </label>
               <div id= "share1"class="fb-share-button" data-href="https://developers.facebook.com/docs/plugins/" data-layout="button" style="width:100px; height:50px;"></div>
            </form>          
        </div>
    </body>
</html>

manifest.json

{
  "manifest_version": 2,
  "name": "teste-gmail",
  "description": "This extension will share the contents of your gmail.",
  "version": "1.0",


  "page_action": {
    "default_icon": "icon.png",
    "default_title":"Gmail",
    "default_popup": "popup.html"
  },

"permissions": [
    "http://mail.google.com/*",
    "http://google.com/",
    "http://www.facebook.com/",
    "https://mail.google.com/*",
    "https://www.facebook.com/",
    "https://www.google.com/",
    "tabs",
    "storage",
    "activeTab"
  ],
  "optional_permissions": [ "tabs", "http://mail.google.com/*",  "http://google.com/"],

  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },

  "content_security_policy": "script-src 'self'; object-src 'self'"
}

background js.

chrome.tabs.onUpdated.addListener(function(id, info, tab){  
        chrome.pageAction.show(tab.id);     
        chrome.tabs.executeScript(tab.id, {
        file: "fbbutton.js"
    }, function() {
        if (chrome.runtime.lastError) {
            console.error(chrome.runtime.lastError.message);
        }
    }); 
});
  • And what’s the problem?

  • The code does not ask the user permissions to be posted on facebook the content of each email.

  • @Marta why you are using the snippet to format the code?

  • 1

    Another thing @Marta your code is with several "indentation" flaws and have some that I see you copied, but you "modified" without any need, leaving flaws in the code. Let me ask you a question, have you ever programmed outside this code? Don’t be upset with my comment please.

1 answer

2


It’s like I said in your other two questions Marta, you should read the documentation and make sure you’re using the example of the way it’s there. In this case (permissions) the link is this: https://developer.chrome.com/extensions/permissions

The original code is thus (as documented):

chrome.permissions.request({
  permissions: ['tabs'],
  origins: ['http://www.google.com/']
}, function(granted) {
  // The callback argument will be true if the user granted the permissions.
  if (granted) {
    doSomething();
  } else {
    doSomethingElse();
  }
});

But you did it, you totally modified the code:

chrome.permissions.onAdded.addListener({permissions: ['tabs'], origins: ['http://www.google.com/']});

To solve the problem you have to understand what are "callbacks", a good answer here on own Stackoverflow.

To summarize the code should be something like (modify inside the if(granted) as needed):

chrome.permissions.request({
    permissions: ['tabs'],
    origins: ['http://www.google.com/']
}, function(granted) {
  if (granted) {
    doSomething();
  } else {
    document.getElementById('share2').innerHTML = tab.url;
    document.getElementById('share2').innerHTML = tab.data;
    document.querySelector("div.fb-share-button").setAttribute("data-href", tab.url);
  }
});

Full documentation

  • @Marta about not receiving the data, could you tell which email you are trying? I will try to reproduce the problem

  • It’s Gmail. I can post all the code here if necessary.

  • 1

    In this case I think you can make an exception (even if you misread the question), edit your question and post html and js. I look forward.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.