如何从Chrome扩展程序访问网页?

时间:2016-12-24 03:22:08

标签: javascript google-chrome-extension

我正在尝试通过点击Chrome扩展程序中的按钮在新标签页中打开网页。以下代码不起作用:

的manifest.json

"manifest_version": 2,
"name": "Add to Cart Extension",
"version": "1",

"icons": {"16": "images/cart16.png",
        "48": "images/cart48.png",
       "128": "images/cart128.png" },

"browser_action": {
"default_title": "Add to Cart",
"default_popup": "index.html",
"default_icon": { 
        "16": "images/cart16.png",
        "24": "images/cart24.png",
        "32": "images/cart32.png"
        }
    },

 "content_scripts": [
    {
        "matches": ["http://www.amazon.com/*","https://www.amazon.com/*"],
        "js": ["script.js"]
    }
  ],

"permissions": [
"activeTab",
"tabs",
"http://www.amazon.com/*",
"https://www.amazon.com/*",
],

"background": {
"scripts": ["script.js"],
"persistent": false
 }
}

的script.js

function addtocart() {
    chrome.tabs.create({url: "http://www.amazon.com"});
}

的index.html

<!doctype html>
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="script.js" type="text/javascript"></script>
</head>
<body>
    <button onclick="addtocart()">Add to Cart</button>
</body>
</html>

我如何得到我想要的东西? 它不会让我编辑,没有更详细的细节,详细介绍细节。

0 个答案:

没有答案