0

I am creating a Google Chrome Extension, and I'm trying to make a button that opens up a stored random website in a new tab once clicked. I've scoured this website and tried everything to no avail. I'm sure it's something simple that I'm missing.

I know the random website function itself works, as I initially had it as a context menu function where it worked fine. However, now that I've attempted to make it a button, it doesn't seem to work.

In randomSiteTestScript.js :

var links = [multiple hhtp:// links]

document.getElementById("randomsite").addEventListener("click", openSite);

function openSite() {
    code that works
};

In popup.html :

<p><button id="randomsite">Click for random site!</button></p>
<script src="randomSiteTestScript.js"></script>

Once the button is clicked, absolutely nothing happens. I'm not getting any errors, but nothing is happening. Again, I know the openSite function works properly as I changed nothing since moving from a context menu functionality. I don't believe the function is being called at all. Thanks in advance for any help!

  • try adding `alert('something')` in your openSite function and remove all content. your code should work – ACD Apr 03 '19 at 04:34
  • No alert came up. Really not sure why it's not working... – John Pappas Apr 03 '19 at 04:43
  • Without a real [MCVE](/help/mcve) this sounds like a job for debugger, which is one of the primary programming tools. You can debug the code in browser's [built-in devtools](https://stackoverflow.com/a/38920982). – wOxxOm Apr 03 '19 at 04:50
  • Have you checked if your "randomsite" button exists in DOM when the randomSiteTestScript.js is initialized? The issue might be because the click event is not getting bound to the element – Chintan Bhatt Apr 03 '19 at 05:09
  • @JohnPappas check chrome debugger > sources. see if your js file actually loaded. – ACD Apr 03 '19 at 05:41
  • Yep, I think this is the problem. Once I click on inspect pop up, and popup.js shows “ERR_FILE_NOT_FOUND”. I’ve looked up solutions but I can’t find anything solid. Maybe I should re create the extension and paste the code in the appropriate files again? – John Pappas Apr 04 '19 at 01:14

1 Answers1

-1

No need for a event listener here... just use an inline onclick event handler.

<button id="randomsite" onclick="openSite(links[Math.floor(Math.random()*links.length)]);">Click for random site!</button>

Pass the URL to OpeSite(URL) and process the opening from there.