What is wrong with this Chrome extension?

Asked

Viewed 165 times

0

I made a small extension in Chrome, and it’s not working properly. My extension simply randomizes a word between one of those randomVals and displays. I’d like you to help me figure out what’s wrong with her. This is the code:

Manifest

{
  "name": "RandomHype",
  "version": "1.0",
  "manifest_version": 2,
  "description": "RandomHype official Extensions.",
  "browser_action": {
     "popup": "rh.html"
  }
}

rh.html

<html>
  <head>
    <link rel="stylesheet" href="typeget.css">
    <script>
    function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    var randomVals = ['Play BTD5',
        'Code',
        'Watch a video',
        'Play Chess',
        'Study',
        'Chat (Skype)',
        'Check Twitter',
        'Check the news on Tecmundo',
        'Talk about SW'
    ];
    var r_i = getRandomInt(0, randomVals.length - 1);
    document.getElementById('doThing').innerHTML = randomVals[r_i];
</script>
  </head>
<body>
<center>
  <h3>I should</h3><br><h1><p id="doThing">Code</p>
</center>
</body>
</html>
  • 1

    The ideal is you put here your code without us having to go to another site to see it. You can use the Snippet Stack using the "JS/HTML/CSS Snippet" button in the edit bar. Nor can you ask us to review all your code looking for the error. What goes wrong? What did you try to fix? Debug (console.log)?

  • Sorry, I’m pretty new here.

  • It went wrong: The extension is added, but Popup does not open when I click. What I tried: Change something from HTML and . json but could not.

  • It’s getting better :) I imagine that the first part is the Manifest and the second is the popup rh.html.... is that it? If you can also explain what your extension does it would be cool.

  • Yes, My extension simply randomizes a word between one of those words and displays.

1 answer

3


First, there is a tag h1 unopened.

Another problem is that your manifest is incorrect. Instead of popup, utilize default_popup:

{
  "name": "RandomHype",
  "version": "1.0",
  "manifest_version": 2,
  "description": "RandomHype official Extensions.",
  "browser_action": {
        "default_popup": "rh.html"
    }
}

Continuing, if your intention is to change the content of <p id="doThing">...</p> with a predefined value each time script is executed, you need to put this script after the element.

Opening the console, you will see the following error:

Typeerror: Document.getElementById(...) is null

This is because inside the <head>, specifically on that line document.getElementById('doThing') you try to get an element that has not yet exists was loaded.

You can...

Place this script at the end of the document before closing the tag <body:

<html>

<head>
  <link rel="stylesheet" href="typeget.css">
</head>

<body>
  <center>
    <h3>I should</h3>
    <br>
    <h1><p id="doThing">Code</p></h1>
  </center>
  <script>
    function getRandomInt(min, max) {
      return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    var randomVals = ['Play BTD5',
      'Code',
      'Watch a video',
      'Play Chess',
      'Study',
      'Chat (Skype)',
      'Check Twitter',
      'Check the news on Tecmundo',
      'Talk about SW'
    ];

    var r_i = getRandomInt(0, randomVals.length - 1);
    document.getElementById('doThing').innerHTML = randomVals[r_i];
  </script>
</body>

</html>


Run the script when the GIFT be ready:

<html>

<head>
  <link rel="stylesheet" href="typeget.css">

  <script>
    document.addEventListener('DOMContentLoaded', function() {

      function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
      }
      var randomVals = ['Play BTD5',
        'Code',
        'Watch a video',
        'Play Chess',
        'Study',
        'Chat (Skype)',
        'Check Twitter',
        'Check the news on Tecmundo',
        'Talk about SW'
      ];


      var r_i = getRandomInt(0, randomVals.length - 1);
      document.getElementById('doThing').innerHTML = randomVals[r_i];

    }, false);
  </script>

</head>

<body>
  <center>
    <h3>I should</h3>
    <br>
    <h1><p id="doThing">Code</p></h1>
  </center>
</body>

</html>


More information on the DOMContentLoaded at that link.

These examples were to rotate the snippet, in Chrome extensions the Javascript file should be created part and not inline in the HTML document. Create a file popup.js (for example), put your Javascript code in it and only refer to HTML:

<script src='popup.js'></script>
  • OK, it seems that this is now working. However, I still can not open the popup

  • @Mucap I edited my answer.

  • 1

    Thank you, had given an error that the JS may not be in the html file, but I resolved importing the script, ready, answered. If you want to edit the answer for future people, you just have to create another script file

Browser other questions tagged

You are not signed in. Login or sign up in order to post.