简体   繁体   English

Chrome扩展程序与背景页面不使用清单版本2

[英]Chrome Extension With Background Page Not Working With Manifest Version 2

I have a simple chrome extension which displays a little icon in Google Chrome. 我有一个简单的Chrome扩展程序,在Google Chrome中显示一个小图标。 On click, it loads a search page of my site, which in term redirects you to the right page. 点击后,它会加载我网站的搜索页面,该页面会将您重定向到正确的页面。

https://chrome.google.com/webstore/detail/w3patrol-watch-over-any-w/addcgpijdjacmndaadfgcpbfinagiplm is the extension. https://chrome.google.com/webstore/detail/w3patrol-watch-over-any-w/addcgpijdjacmndaadfgcpbfinagiplm是扩展程序。

Now, Google is forcing me to update to manifest version 2, instead of 1. But this is breaking my working extension. 现在,谷歌迫使我更新到清单版本2,而不是1.但这打破了我的工作扩展。

In manifest.json I have added manifest_version 2, but since then the icon does not work anymore when I click on it. manifest.json我添加了manifest_version 2,但从那时起,当我点击它时,图标不再起作用了。

{
   "background": {
    "page": "background.html"
    },
   "browser_action": {
      "default_icon": "icon19.png",
      "default_title": "__MSG_default_title__"
   },
   "default_locale": "en",
   "description": "__MSG_description__",
   "icons": {
      "128": "icon128.png",
      "19": "icon19.png",
      "48": "icon48.png"
   },
   "name": "__MSG_name__",
   "permissions": [ "tabs", "http://*.w3patrol.com/" ],
   "update_url": "http://clients2.google.com/service/update2/crx",
   "version": "1.0",
   "manifest_version": 2
}

This is background.html 这是background.html

<script type="text/javascript">
chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.getSelected(null,function(tab) {
        chrome.tabs.create( { url: "http://w3patrol.com/search.php?q=" +tab.url } );
    });
});

</script>

What do I need to add/change to get it working with manifest version 2? 我需要添加/更改以使其与清单版本2一起使用?

You just need to remove the script tag from your background page. 您只需要从后台页面中删除脚本标记即可。 Here's how background.js(instead of background.html) should look like: 以下是background.js(而不是background.html)的外观:

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.getSelected(null,function(tab) {
        chrome.tabs.create( { url: "http://w3patrol.com/search.php?q=" +tab.url } );
    });
});

And remove the 'page' property in background. 并在后台删除“页面”属性。 Add 'scripts' property: 添加'脚本'属性:

  "background": {
    "scripts": ["background.js"]
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM