Get Permissions on Extension

0

I'm trying to get user permissions 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);
        }
    }); 
});
    
asked by anonymous 30.12.2014 / 17:40

1 answer

2

It's like I said in your two other Marta questions, you should read the documentation and make sure you are using the example the way it is there. In this case (permissions) the link is this: link

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 this, totally changed the code:

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

To solve the problem, you have to understand what callbacks are, a good answer here in the StackOverflow .

To summarize the code should be something like (modify within 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

    
30.12.2014 / 18:06