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.

  • Possible duplicate of [Chrome extension: open link in new tab?](http://stackoverflow.com/questions/16503879/chrome-extension-open-link-in-new-tab) – SmokeDetector Dec 24 '16 at 03:26
  • Please [edit] the question to be on-topic: include a **complete** [mcve] 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 [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Dec 24 '16 at 03: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 Dec 24 '16 at 03:41
  • My bad, forgot part of it – user7336571 Dec 24 '16 at 03:46
  • The following link describes/shows how to open the [various appropriate consoles for your extension](http://stackoverflow.com/a/38920982/3773011). 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 Dec 24 '16 at 03:47
  • How/where is `addtocart()` called? – Makyen Dec 24 '16 at 03:47
  • No, nothing is displayed in the console once clicked. – user7336571 Dec 24 '16 at 03:48
  • addtocart() is called when a button is clicked in the HTML. I tried the function with what I have in the post now, and what I tried earlier: `chrome.tabs.create({url: "http://www.youtube.com"})` and both didn't seem to work. – user7336571 Dec 24 '16 at 03:50
  • What HTML? Try removing the `function addtocart() {` and closing `}`. – Makyen Dec 24 '16 at 03:51
  • @user7336571, BTW: If you want a specific person to be notified of your comment, you need to include their ID in your comment with an `@` in front of it. For instance, for me it would be `@Makyen`. If you do this as the first thing in your comment, the system will provide auto-complete suggestions from those who have already posted comments on that answer/question. The original poster of the answer/question on which you are commenting will always be notified. This [meta post](http://meta.stackexchange.com/a/43020/271271) has more information. – Makyen Dec 24 '16 at 03:52
  • @Makyen I have an HTML document with a button that triggers addtocart() when clicked. `` I also have the script linked to the HTML with a script tag. I tried removing the `function addtocart()` and its closing `}`, still did not work, and nothing displayed in the console. – user7336571 Dec 24 '16 at 03:57
  • @user7336571, How is that HTML loaded into Chrome? I don't see any reference to it in the *manifest.json*, or in your code in the Question. – Makyen Dec 24 '16 at 03:59
  • @Makyen How would I link the HTML in manifest.json? – user7336571 Dec 24 '16 at 04:02
  • browserAction.onClicked won't work if you specify an html page for the popup e.g. via `"default_popup"` in manifest. See [Unable to trigger chrome.browserAction.onClicked.addListener with google chrome extensions](//stackoverflow.com/a/18767589) – wOxxOm Dec 24 '16 at 05:09
  • I would suggest that you read the [Chrome extension architecture overview](https://developer.chrome.com/extensions/overview#arch) (and perhaps work through reading the pages linked from there). It has overall architecture information which should help your understanding of how things are generally organized/done. – Makyen Dec 24 '16 at 05:15
  • @user7336571, I don't know what you are trying to do with your HTML, so can not comment on how to use it, or even if it should be used. You will have to show us the HTML and explain/show how you are trying to display it with Chrome for us to have any idea what is going on. – Makyen Dec 24 '16 at 05:18
  • @Makyen I have edited the post to contain my HTML. My goal is to press a button on the HTML(the popup), and open a link via pressing the button. From the link, I could edit/interact with the elements on the webpage of my choosing. But, I'm just looking at the first step, which would be opening the link from the button. Maybe I am really misinformed and am messing up with the .json, I'm not too sure. I read the overview, and it cleared up a few things, but the background.html still doesn't make much sense to me. – user7336571 Dec 24 '16 at 06:12
  • @makyen Any ideas, still can't seem to figure out the problem. – user7336571 Dec 26 '16 at 22:46

0 Answers0