从 Chrome 扩展中获取 Chrome 选项卡 pid
Get Chrome tab pid from Chrome extension
我正在尝试通过 chrome 扩展获取与当前选项卡关联的进程 ID。
我确实通过 chrome.processes
实验 API 获得了它。
有什么方法可以使用标准(非实验)获取选项卡 pid API ?
不行,除了实验没有别的办法APIchrome.processes
如果你想获得真正的进程 ID(即可以被其他程序用来识别进程的 ID),那么你唯一的选择是 chrome.processes
, but this API is only available on the Dev channel(所以对于 Chrome 稳定版和测试版)。
如果你只需要一个标识符来唯一标识进程,那么你可以通过启用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获得"process ID of a tab"。
因此,根据 "tab PID",我假设您指的是托管顶级框架的进程。然后您可以检索框架列表并提取选项卡的进程 ID,如下所示:
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"
]
}
我正在尝试通过 chrome 扩展获取与当前选项卡关联的进程 ID。
我确实通过 chrome.processes
实验 API 获得了它。
有什么方法可以使用标准(非实验)获取选项卡 pid API ?
不行,除了实验没有别的办法APIchrome.processes
如果你想获得真正的进程 ID(即可以被其他程序用来识别进程的 ID),那么你唯一的选择是 chrome.processes
, but this API is only available on the Dev channel(所以对于 Chrome 稳定版和测试版)。
如果你只需要一个标识符来唯一标识进程,那么你可以通过启用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获得"process ID of a tab"。
因此,根据 "tab PID",我假设您指的是托管顶级框架的进程。然后您可以检索框架列表并提取选项卡的进程 ID,如下所示:
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"
]
}