Google chrome extension 单击后获取当前选项卡url

Google chrome extension 单击后获取当前选项卡url,google-chrome-extension,Google Chrome Extension,我希望当前选项卡url在单击扩展后显示在弹出窗口中。我有一个弹出html和popup.js。出于测试目的,我一直试图提醒选项卡url,而不是替换html元素 Manifest.json { "manifest_version": 2, "name": "first extension", "version": "1.0", "browser_action": { "default_icon": "icon.png", "defau

我希望当前选项卡url在单击扩展后显示在弹出窗口中。我有一个弹出html和popup.js。出于测试目的,我一直试图提醒选项卡url,而不是替换html元素

Manifest.json

{
    "manifest_version": 2,
    "name": "first extension",
    "version": "1.0",

    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html",
        "default_title": "Share it!"
    },

    "permissions": [
        "activeTab"
    ]
}
popup.html

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="popup.js"></script>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    URL:
    <p>www.blahblah.com</p>
    <form>
        Title:<br />
        <input type="text" name="title"><br />
        Description:<br />
        <textarea rows="4"></textarea><br />
        <button>Get</button>
    </form>
</body>
</html>

您可能希望遵循本节中建议的解决方法。尝试此chrome.tabs.queryobject queryInfo函数回调,它将获取具有指定属性的所有选项卡,如果未指定属性,则获取所有选项卡

chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    console.log(tabs[0].url);
});

下面是另一个可能也有帮助的线程:

注意,这种方法需要tabs权限,activeTab可能不够。
chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
    console.log(tabs[0].url);
});