How to access the url of the current tab

Asked

Viewed 736 times

0

I have part of this code that belongs to an extension of Chrome. Clicking the share button does not share the content of the page I want.

Does anyone know how to share the page you are on at the moment 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>
  • Don’t use the snippet unnecessarily. Another thing you can read the documentation, because if for every detail of your application, you will create a question, which can be faster to learn, so at all times read the documentation and study for it! : ) Success

1 answer

0


As stated in the Soen, read about this at:

An example of using your js file would be something like:

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

It is necessary to change the attribute data-href from 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);
});
  • When obtaining the URL of the email I realized that to share it is necessary to obtain the contents of this url. I used the function Document.getElementById('share2'). innerHTML=tab.data; However I’m not getting it. I just need keywords to search. Thank you.

  • 1

    It’s true @Marta facebook isn’t usually able to access Urls where there is authentication, but if you think about it, you don’t have to share an authenticated page. But the use of tab.data still has more advantages than injecting the share-button because of "security", not that facebook will "steal" access something improperly, but any injected code can become more complicated, where for example we use authentication (even if the page is published, there may still be authentication).

  • I tried to ask the user for permission, but something’s not right. &#xA;chrome.tabs.getSelected(null, function(tab) {&#xA; document.getElementById('share2').innerHTML = tab.url;&#xA; chrome.permissions.onAdded.addListener({permissions: ['tabs'], origins: ['http://www.google.com/']}) { console.log("The Permissions are Nedd."); Document.getElementById('share2').innerHTML = tab.data; Document.querySelector("div.fb-share-button").setAttribute("data-href", tab.url); }); ;});

  • 1

    @Marta this is already another issue. If you followed the examples of documentation strictly, please open a new question. This link can help you: http://answall.com/help/mcve. Goodbye :)

Browser other questions tagged

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