pagaction on点击无响应

pageAction onClick no respond

本文关键字:响应 on pagaction      更新时间:2023-09-26

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

my manifest 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
}
背景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"'
  });
});

我甚至把点击功能在chrome.runtime,什么也没发生。我想我没有忘记重新加载扩展包

似乎您无法访问活动选项卡的内容,因此您的chrome.tabs.executeScript失败了。

另外尝试获取"activeTab"权限