为什么我的chrome扩展程序的pageAction.js不会返回活动标签页或标题

时间:2016-01-27 02:40:17

标签: javascript jquery google-chrome google-chrome-extension

我正在Windows 10中进行页面操作扩展,Chrome版本为48.0.2564.82 m

的manifest.json

{
    "manifest_version": 2,       // using 2, as instructed by google
    "name": "my page action :) ",
    "version": "1.0",
    "permissions": [
        "activeTab",       // active tabs are permitted
        "https://www.google.com/"    // oddly, my icon appears on any site not just google
    ],
        "page_action": {
        "default_title": "my page action extension :) ",
        "default_icon": "bookmark20grey.png",
        "default_popup": "popup.htm"       // source below
    },
    "background": {
        "scripts": ["background.js"],     // using background script as per documentation
        "persistent": false
    }
}

background.js

chrome.tabs.onUpdated.addListener(function(id, info, tab){
    chrome.pageAction.show(tab.id);
    chrome.tabs.executeScript(null, {"file": "pageaction.js"}); // injecting code to page
});

popup.htm     

<html>
  <head>
    <title>add url</title>
    <script src="jquery214.js"></script>
    <script src="popup.js"></script>  <!-- doing my UI work here using jQuery-->
    <link rel="stylesheet" type="text/css" href="popup.css" media="screen" />
  </head>
  <body>
  </body>
</html>

popup.js

$(function(){

    var funx = {
        getCurrentTab : function(){ // using deferreds for function sequencing purposes later
            return new Promise(function(resolve, reject){
                chrome.tabs.query(
                    {
                        active:true,
                        lastFocusedWindow:true
                    },
                    function(tabs){
                        console.log(tabs[0].url); // shows undefined
                        console.log(tabs[0].title); // shows undefined
                        resolve(tabs[0]);
                    }
                );
            });
        }
    }

    funx.getCurrentTab()
        .then(function(tab){
            console.log(tab); // gives me correct tab info, but it's devoid of title and url
        })
    ;


});

这可以很好地作为常规扩展(图标位于位置栏之外),但我正在尝试将其转换为特定域的页面操作

预期结果 如果我在弹出窗口右键单击常规扩展并检查时,我会看到控制台日志,并显示一个对象:

active: true
audible: false
favIconUrl: "http://cdn.sstatic.net/stackoverflow/img/favicon.ico?v=4f32ecc8f43d"
height: 979
highlighted: true
id: 38
incognito: false
index: 5
mutedInfo: Object
pinned: false
selected: true
status: "complete"
title: "why does my chrome extension's pageAction.js not return the active tab URL or title - Stack Overflow"
url: "http://stackoverflow.com/questions/ask"
width: 1683
windowId: 6

但是当我将其作为页面操作运行,并检查弹出窗口时,我得到了这个:

active: true
audible: false
height: 979
highlighted: true
id: 38
incognito: false
index: 5
mutedInfo: Object
pinned: false
selected: true
status: "complete"
width: 1683
windowId: 6

0 个答案:

没有答案