Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Chrome tab pid from Chrome extension

I'm trying to get the process id associated with current tab thru chrome extension.

I did manage to get it thru chrome.processes experimental API.

There is any way to get the tab pid with standard (non-experimental) API ?

like image 558
AK87 Avatar asked Dec 15 '22 06:12

AK87


2 Answers

If you want to get the real process ID (i.e. one that can be used by other programs to identify processes), then your only option is chrome.processes, but this API is only available on the Dev channel (so not for Chrome stable nor Beta).

If you just need an identifier to uniquely identify processes, then you can get the "process ID of a tab" via the chrome.webNavigation API. This ID is only meaningful within Chrome. Before I delve into the details, let's first say that multiple tabs can share the same process ID, and that one tab can contain multiple processes (when the Site isolation project is enabled).

So, by "tab PID", I assume that you're referring to the process that hosts the top-level frame. Then you can retrieve a list of frames and extract the process ID for the tab as follows:

background.js

'use strict';
chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.webNavigation.getAllFrames({
        tabId: tab.id,
    }, function(details) {
        if (chrome.runtime.lastError) {
            alert('Error: ' + chrome.runtime.lastError.message);
            return;
        }
        for (var i = 0; i < details.length; ++i) {
            var frame = details[i];
            // The top-level frame has frame ID 0.
            if (frame.frameId === 0) {
                alert('Tab info:\n' +
                      'PID: ' + frame.processId + '\n' +
                      'URL: ' + frame.url);
                return; // There is only one frame with ID 0.
            }
        }
        alert('The top-level frame was not found!');
    });
});

manifest.json

{
    "name": "Show tab PID",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "browser_action": {
        "default_title": "Show tab PID"
    },
    "permissions": [
        "webNavigation"
    ]
}
like image 104
Rob W Avatar answered Dec 22 '22 03:12

Rob W


No, there is no other way except experimental API chrome.processes

like image 27
Haibara Ai Avatar answered Dec 22 '22 03:12

Haibara Ai