如何让谷歌PageAction示例扩展在谷歌Chrome上运行

How to get the google PageAction sample extension working on Google Chrome as supplied?

本文关键字:谷歌 Chrome 运行 扩展 PageAction      更新时间:2023-09-26

这可能是我在做一些愚蠢的事情,但我不知道是什么。我正在尝试让一个示例谷歌Chrome页面扩展演示工作。演示的来源可以在这里找到:http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/pageAction/

这是一个简单的程序,有两个代码文件——manifest和一个background.js文件。这是背景.js:

// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
  // If the letter 'g' is found in the tab's URL...
  if (tab.url.indexOf('g') > -1) {
    // ... show the page action.
    chrome.pageAction.show(tabId);
  }
};
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);

这是manifest.json文件:

{
  "name": "Page action by URL",
  "version": "1.0",
  "description": "Shows a page action for urls which have the letter 'g' in them.",
  "background": { "scripts": ["background.js"] },
  "page_action" :
  {
    "default_icon" : "icon-19.png",
    "default_title" : "There's a 'G' in this URL!"
  },
  "permissions" : [
    "tabs"
  ],
  "icons" : {
    "48" : "icon-48.png",
    "128" : "icon-128.png"
  },
  "manifest_version": 2
}

正如所写的,这段代码对我不起作用。Chrome加载扩展插件很好,但当我导航到URL中有g的页面时,没有图标显示。

我找到了这个答案:如何为特定页面显示page_action?

所以我尝试了以下-

  • 创建了一个background.html文件:

    <html><head><script> ... cut and pasted contents of background.js above </script></head></html>
    
  • 将manifest.json更改为manifest_version:1,而不是manifest_version:2

  • 将manifest.json中的background属性更改为background_page:"background.html"

这非常有效。

但是我绝对不能用background.js文件和manifest_version=2来运行这个东西。

所以我想知道如何使用manifest_version=2和background.js文件。此外,这甚至很重要吗?也就是说,每个人都只是使用manifest_version=1,而不担心这类事情吗?

您使用的是什么版本的Chrome?我19岁了,一切都很顺利。这些新的背景选项在18岁时出现,因此该示例确实应该使用minimum_chrome_version键来避免这个问题。如果非trunk文档没有指向在这些文档所使用的Chrome版本中不起作用的示例,那也会很好。

http://code.google.com/chrome/extensions/trunk/manifestVersion.html
这个页面上写着。。。。

不建议在Chrome 17或更低版本中设置manifest_version 2。如果您的扩展需要在旧版本的Chrome中工作,请坚持目前是版本1。我们之前会给你充分的警告版本1停止工作。

有点傻,是吗?如果他们的Chrome版本是预清单版本2,为什么我们不能有后台和后台页面作为依据?。。。我试过了,它抱怨background_page不适用于manifest版本2,也没有安装它。所以你可能想暂时推迟使用manifest版2
尽管阅读这些版本2的文档并开始使用它将要强制执行的一些实践(例如没有内联脚本)是一个很好的主意。

另一个可能导致此问题的因素:

我遇到这个问题是因为我忘记按照示例在清单中包含declarativeContent权限。

希望这能帮助到别人。。。