How to access the url of the current tab

0

I have part of this code that belongs to a chrome extension. By clicking the share button, you do not share the content of the page I want.

Does anyone know how I can share the page you are on at the time of the click?

      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'xxxx',
          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))

      //var e = document.createElement('script');
     // e.src = document.location.protocol + 'all.js';
      //e.async = true;
     // document.getElementById('fb-root').appendChild(e);
      return
  js = d.createElement(s); 
  js.id = id;
  js.src = "sdk.js";
  js.async = true;
  fjs.parentNode.insertBefore(js, fjs);

}



(document, 'script', 'facebook-jssdk'));

HTML:

<!doctype html>
<html>
    <head>
        <script src="fbbutton.js"></script>
        <script src="settings.js"></script>
        <style>
          div span, iframe {
            height:50px!important;
            width:100px!important;
          }

        </style>
    </head>

    <body>


<div id="fb-root"></div>



        <div style="width:160px; height:200px; font: normal 21px/23px 'ProximaNovaSemibold',Helvetica,Arial,sans-serif">
            <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 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>
    
asked by anonymous 29.12.2014 / 21:02

1 answer

0

As this answer in the SOen , read about it at:

An example of using your js file would look like this:

chrome.tabs.getSelected(null, function(tab) {
    console.log(tab.url);
});

You need to change the data-href attribute of your share button:

<div class="fb-share-button" data-href="https://developers.facebook.com/docs/plugins/" data-layout="button" style="width:100px; height:50px;"></div>

You can use something like:

chrome.tabs.getSelected(null, function(tab) {
    document.querySelector("div.fb-share-button").setAttribute("data-href", tab.url);
});
    
29.12.2014 / 21:07