2

I am trying to open a webpage in a new tab by clicking a button in a chrome extension. The following code doesn't work:

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>

How would I get what I am looking for? It will not let me edit without more detailsdetailsdetailsdetailsdetails.

18
  • Possible duplicate of Chrome extension: open link in new tab? Commented Dec 24, 2016 at 3:26
  • Please edit the question to be on-topic: include a complete minimal reproducible example that duplicates the problem. Including a manifest.json, some of the background/content/popup scripts/HTML. Questions seeking debugging help ("why isn't this code working?") must include: ►the desired behavior, ►a specific problem or error and ►the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: "How to create a minimal reproducible example", What topics can I ask about here?, and How to Ask.
    – Makyen
    Commented Dec 24, 2016 at 3:32
  • @SmokeDetector, While the (now old) title of the possible duplicate makes it sound like this might be a duplicate, the question here does not provided us enough information to know if it is a duplicate of that one. That one is quite specific as to its issue being the Content Security Policy. I have edited that question so its title more accurately reflects the issue.
    – Makyen
    Commented Dec 24, 2016 at 3:41
  • My bad, forgot part of it Commented Dec 24, 2016 at 3:46
  • The following link describes/shows how to open the various appropriate consoles for your extension. In this instance, the console of concern would be the background page. Is there anything displayed in that console when you load your extension and click the browser action button?
    – Makyen
    Commented Dec 24, 2016 at 3:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.