0

working on my first Chrome Extension and running into a problem. I want to be able to have on the options.html page a checkbox for deciding if it should open in a tab or windows. I have the following code which isn't working:

options.html

<div class="checkbox">
    <label>
        <input type="checkbox" id="useTab"> Open as Tab?
    </label>
</div>

background.js

chrome.browserAction.onClicked.addListener(
  function(tab) {
    if(document.getElementById('useTab').checked) {
      chrome.tabs.create({
          url: chrome.runtime.getURL("<website>"),
          //type: "popup"
          }, function(win) {

          });
    }
    else {
      chrome.windows.create({
        url: chrome.runtime.getURL("<website>"),
        //type:popup"
        }, function(win) {

        });
    }
  }
);

The idea of the code is such that when the extension icon in the browser is selected, the function is run from background.js, and if the option checkbox is checked it opens as a tab, and if it isn't, it is opened as a window.

Michael
  • 1
  • 1
  • 1
    https://developer.chrome.com/extensions/overview – Daniel Herr Jul 19 '17 at 16:00
  • 1. See the overview's architecture article: the background page is a separate page with its own `document`. 2. You need a separate popup.html declared in "default_popup", see a [demo extension](https://developer.chrome.com/extensions/samples#search:popup) 3. Always check documentation before using a method you're not familiar with: chrome.runtime.getURL is only for extension's own pages. – wOxxOm Jul 19 '17 at 16:11
  • Yes, the is a local webpage that works for that function. If I manually use the full command, without trying the if/else login, it will open as a window, or tab depending on the command, I'm trying to get it so that the checkbox "decides" which way to open the extension though. – Michael Jul 19 '17 at 16:29
  • I've voted to close as a duplicate, because your options for solving the issue are covered in the duplicate. There's nothing wrong with a question being a duplicate. They can be good signposts to direct people to questions which cover their issue. However, this question actually lacks a [MCVE], which would make it off-topic. My next comments will cover what's needed for debugging questions, and why. – Makyen Jul 19 '17 at 19:38
  • Debugging questions: include a [mcve] that duplicates the problem. For Chrome extensions or Firefox WebExtensions this almost always means including your *manifest.json* and some of the background, content, and/or popup scripts/HTML. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Jul 19 '17 at 19:38
  • The reason that a [mcve] is required is that *we want to help*. It is **much** easier to help if we don't have to recreate any of the code needed to duplicate the problem. This is code that you already have. So, please help us to help you and provide a *complete* [mcve] that duplicates the problem. Without a [mcve], the amount of effort required to even begin to help you is **much** higher, which *significantly* reduces the number of people willing/able to help you. Even if we put out the extra effort, we have to **guess** at significant portions of what your problem might be. – Makyen Jul 19 '17 at 19:39

0 Answers0