Google拡張機能からページのタイトルの値を取得する方法はありますか?
2 に答える
At first you should declare the tabs
API permission in your manifest.json
:
{
"name": "My extension",
...
"permissions": ["tabs"],
...
}
Then you will be able to use the tabs API, you are looking for the chrome.tabs.getSelected(windowId, callback)
method.
To get the selected tab of the current window, you can simply pass null
as the windowId
.
This method will execute the callback function passing a Tab object as its first argument, where you can simply get the title
property:
chrome.tabs.getSelected(null,function(tab) { // null defaults to current window
var title = tab.title;
// ...
});
CMSで言及されている上記の方法は、Chrome33以降廃止されていることに注意してください。
tabs
ここで行っているのは高度なアクションではないため、マニフェストファイルで権限を指定する必要はありません。権限を指定しなくても、ほとんどのtabs
アクションを実行できます。いくつかの特定の方法についてのみ、あなたはそうする必要があります。
現在選択されているタブをクエリする新しい方法は、次のコードによるものです。
chrome.tabs.query({ active: true }, function (tab) {
// do some stuff here
});
これにより、複数のウィンドウを開いている場合は、すべてのウィンドウで選択したタブが表示されます。現在のウィンドウで選択したタブのみを表示する場合は、次を使用します。
chrome.tabs.query({ active: true, currentWindow: true }, function (tab) {
// do some other fanciful stuff here
});
詳細については、https://developer.chrome.com/extensions/tabs#method-queryを参照してください。