Matthew Snell
Matthew Snell

Reputation: 957

Send the URL of current tab to popup - Chrome extension

I've been following along on a few tutorials but have been running into examples that use deprecated code.

I'm trying to send the URL of the current tab to the popup.

My code thus far:

Manifest.json contains the proper permissions:

"permissions": [
    "activeTab",
    "tabs"
  ]

Popup.js, where I think the problem is but no errors in console.

function setup() {
  noCanvas();

  var url = 'URL'

  function sendMessage() {
    var msg = {
      from: 'popup',
      page: url
    }

    var params = {
      active: true,
      currentWindow: true
    }
    chrome.tabs.query(params, gotTabs);

    function gotTabs(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, msg);
      url = document.getElementById('currentLink').innerHTML;
      console.log(url)
    }
  }
}

Popup.html

<body>
    <h1>Popup Interface Example</h1>
    <p id="currentLink">Loading ...</p>
</body>

What am I missing here?

Upvotes: 4

Views: 1953

Answers (1)

Matthew Snell
Matthew Snell

Reputation: 957

I figured it out. Was making it WAY too hard.

Popup.js

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
    var url = tabs[0].url;
    console.log(url);
    document.getElementById("myText").innerHTML = url;
});

Popup.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Extension Pop-Up</title>
    <script src="libraries/p5.js" type="text/javascript"></script>
    <script src="libraries/p5.dom.js" type="text/javascript"></script>
    <script src="popup.js" type="text/javascript"></script>
  </head>
  <body>
    <h1>Wordnik Lookup</h1>
    <p id="myText"></p>
  </body>
</html>

Upvotes: 3

Related Questions