pageAction onClick没有响应

时间:2014-08-18 14:00:54

标签: javascript google-chrome-extension

我在一个工作示例应用程序(pageAction)中添加了click事件监听器,但我不知道哪里出了问题。看看这个文件中的2个:

我的清单json

{
  "name" : "Page action by content",
  "version" : "1.1",
  "description" : "Shows a page action for HTML pages containing a video",
  "background" : {
    "scripts": ["background.js"],
    "persistent": false
  },
  "page_action" :
  {
    "default_icon" : "video-19.png",
    "default_title" : "There's a <video> in this page!"
  },
  "permissions": [ "declarativeContent" ],
  "icons" : {
    "48" : "video-48.png",
    "128" : "video-128.png"
  },
  "manifest_version": 2
}

background js

chrome.runtime.onInstalled.addListener(function() {
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    chrome.declarativeContent.onPageChanged.addRules([{
      conditions: [
        // When a page contains a <video> tag...
        new chrome.declarativeContent.PageStateMatcher({
          css: ["video"]
        })
      ],
      // ... show the page action.
      actions: [new chrome.declarativeContent.ShowPageAction() ]
    }]);
  });

});


 chrome.pageAction.onClicked.addListener(function(tab) {
  console.log('Turning ' + tab.url + ' red!');
  chrome.tabs.executeScript({
    code: 'document.body.style.backgroundColor="red"'
  });
});

我甚至将click函数放在chrome.runtime中,没有任何反应。我不认为我忘记重新加载扩展包。

1 个答案:

答案 0 :(得分:2)

似乎您无法访问有效标签的内容,因此chrome.tabs.executeScript失败了。

另外尝试获取"activeTab" permission