nariboi
nariboi

Reputation: 1

How can I get current tab title in a Chrome extension?

I am trying to display the title of the current tab in my Popup Window.

I have already wrote the tabs permission in my JSON File:

{
    "name": "Price Comparing Tool",
    "version": "0.1.0",
    "description": "Immediate notification on potential savings!",
    "permissions": ["tabs"],
    "background": {
      "service_worker": "script.js"
    },

and I am using the Code from the Chrome Extension Documentation Here

Here the HTML Line

 <h3 id="grabedTitle"></h3>

And here my JS Code for the TitleGrab

var theTitle = async function getCurrentTab() {
    let queryOptions = { active: true, lastFocusedWindow: true };
    // `tab` will either be a `tabs.Tab` instance or `undefined`.
    let [tab] = await chrome.tabs.query(queryOptions);
    return tab;
  }

document.getElementById("grabedTitle").innerHTML = theTitle;

obviously, this doesn't work. theres is nothing written in the Popup But why? And how can I fix it?

What I am trying to achieve:

Print the title of the Current Tab, always, when I am switching the Tab, reload and get current title again. (Probably I need an Eventlistener, how can I implement it?)

Upvotes: 0

Views: 897

Answers (1)

Andrew Parks
Andrew Parks

Reputation: 8107

Change

document.getElementById("grabedTitle").innerHTML = theTitle;

to:

document.getElementById("grabedTitle").innerHTML = (await theTitle()).title;

And, obviously, rename your function to something that reflects that it is returning a tab, not a title.

Upvotes: 1

Related Questions