docs.rodeo

MDN Web Docs mirror

devtools.inspectedWindow.tabId

{{AddonSidebar}} 

The ID of the {{WebExtAPIRef("tabs.Tab", "tab")}}  that this instance of the devtools is attached to, represented as a number.

This can be sent to the extension’s background page, so that background page can use the {{WebExtAPIRef("tabs")}}  API to interact with the tab:

// devtools-panel.js

const scriptToAttach = "document.body.innerHTML = 'Hi from the devtools';";

attachContentScriptButton.addEventListener("click", () => {
  browser.runtime.sendMessage({
    tabId: browser.devtools.inspectedWindow.tabId,
    script: scriptToAttach,
  });
});
// background.js

function handleMessage(request, sender, sendResponse) {
  browser.tabs.executeScript(request.tabId, {
    code: request.script,
  });
}

browser.runtime.onMessage.addListener(handleMessage);

Browser compatibility

{{Compat}} 

{{WebExtExamples}} 

[!NOTE] This API is based on Chromium’s chrome.devtools API.

In this article

View on MDN