如何获取 firefox web 扩展中当前选项卡的标题?
How to get the title of the current tab in firefox web extension?
我正在尝试编写一个简单的 firefox 扩展程序,在按下扩展程序的按钮时显示用户正在查看的当前选项卡的标题。
这是我的 manifest.json 文件:
{
"manifest_version": 2,
"name": "Perspective",
"version": "1.0",
"description": "Displays current tab title when clicked",
"permissions": [
"tabs"
],
"icons": {
"48": "icons/border-48.png"
},
"browser_action": {
"browser_style": true,
"default_popup": "popup/choose_page.html",
"default_icon": {
"16": "icons/news-icon-16.png",
"32": "icons/news-icon-32.png"
}
}
}
而且,这是我的 choose_page.js 文件
//Fetch the title of the active tab
console.log("-----------Button Clicked------------");
var activeTab = browser.tabs.query({active: true, currentWindow: true});
console.log("---------Printing type of variable \"activeTab\"");
if (typeof activeTab == "undefined")
console.log("The object is undefined")
else
console.log("The type of activeTab is:" + typeof activeTab)
console.log("The title of activeTab is:")
console.log(activeTab.title);
每当我点击我的扩展程序按钮时,我都会在控制台中得到以下输出:
-----------Button Clicked------------
---------Printing type of variable "activeTab"
The type of activeTab is:object
The title of activeTab is:
undefined
为什么未定义“activeTab”的标题,我如何实际获取用户正在查看的当前选项卡的标题?
这是扩展名中所有文件的 link:https://drive.google.com/file/d/1L7vho9iSL9nX2LKBmCveJvODmKQBlqX9/view?usp=sharing
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/query
This is an asynchronous function that returns a Promise.
你得先等promise的resolution。尝试这样的事情:
browser.tabs.query({active: true, currentWindow: true})
.then(tabs => {
for (const tab of tabs) {
console.log(tab.title);
}
});
您的 activeTab
变量不是活动选项卡,而是您需要等待解决的承诺。此外,当 promise 解决时,它会为您提供 array 个选项卡,而不是单个选项卡。
我正在尝试编写一个简单的 firefox 扩展程序,在按下扩展程序的按钮时显示用户正在查看的当前选项卡的标题。
这是我的 manifest.json 文件:
{
"manifest_version": 2,
"name": "Perspective",
"version": "1.0",
"description": "Displays current tab title when clicked",
"permissions": [
"tabs"
],
"icons": {
"48": "icons/border-48.png"
},
"browser_action": {
"browser_style": true,
"default_popup": "popup/choose_page.html",
"default_icon": {
"16": "icons/news-icon-16.png",
"32": "icons/news-icon-32.png"
}
}
}
而且,这是我的 choose_page.js 文件
//Fetch the title of the active tab
console.log("-----------Button Clicked------------");
var activeTab = browser.tabs.query({active: true, currentWindow: true});
console.log("---------Printing type of variable \"activeTab\"");
if (typeof activeTab == "undefined")
console.log("The object is undefined")
else
console.log("The type of activeTab is:" + typeof activeTab)
console.log("The title of activeTab is:")
console.log(activeTab.title);
每当我点击我的扩展程序按钮时,我都会在控制台中得到以下输出:
-----------Button Clicked------------
---------Printing type of variable "activeTab"
The type of activeTab is:object
The title of activeTab is:
undefined
为什么未定义“activeTab”的标题,我如何实际获取用户正在查看的当前选项卡的标题?
这是扩展名中所有文件的 link:https://drive.google.com/file/d/1L7vho9iSL9nX2LKBmCveJvODmKQBlqX9/view?usp=sharing
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/query
This is an asynchronous function that returns a Promise.
你得先等promise的resolution。尝试这样的事情:
browser.tabs.query({active: true, currentWindow: true})
.then(tabs => {
for (const tab of tabs) {
console.log(tab.title);
}
});
您的 activeTab
变量不是活动选项卡,而是您需要等待解决的承诺。此外,当 promise 解决时,它会为您提供 array 个选项卡,而不是单个选项卡。