0

I have a chrome extension that has two buttons from the HTML file.

I use getElementByID('button_name') and addEventListener('click', function(){});

The function is in the javascript file and tries to access and print a global JSON.stringify() object.

I used an alert and it says: chrome-extension://"something"/popup.html.

I tried to declare a var outside the function then initialize it inside, but it did not work. I tried many alerts() to try to see the object and see if I can access it, but the only thing that worked so far was a String, I'm guessing because it is not as complex as an object.

popup.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <button id="list">Missing videos for this page</button>
        <button id="reset">Clear List</button>
        <script type="text/javascript" src="popup.js"></script>
        <script type="text/javascript" src="content.js"></script>
    </body>
</html>

content.js

//far up
const url = window.location.href;
var delString = JSON.stringify([url, "delete"]);

//html button
var reset = document.getElementById('reset');
if(reset){
    reset.addEventListener('click',  function(){
        alert(window.location.href); // gives chrome-extension:// not url
        alert(JSON.parse(delString)); // gives the array, but url is same as above.
        //myStorage.setItem(delString, "");
    });
}
jkwan3
  • 1
  • 2
  • 1) It's unclear where that global variable is defined. If it's a page variable defined by the site, see [this answer](https://stackoverflow.com/a/9517879). 2) The browser_action popup is a separate page with its own chrome-extension:// URL so it doesn't make sense to run content.js there because content.js is for web pages and those are different separate pages. – wOxxOm Mar 28 '20 at 05:12
  • Sorry my edit did not save, but the global variables were all the way at the top. And I do not know too much about browser_action, but does the connection from button id from popup.html go to the content.js? – jkwan3 Mar 28 '20 at 06:16
  • Content script is an entirely different thing, you can't use it here and you don't need it. As for location.href it will always be chrome-extension:// URL in the popup because the popup is a separate page (just like any web page it has its own URL). See [How can I get the URL of the current tab from a Google Chrome extension?](https://stackoverflow.com/q/1979583) – wOxxOm Mar 28 '20 at 07:49
  • So the content.js uses the current page and grabs information. The manifest is matched to youtube and "js": ["content.js"], is this not declaratively injecting? And what do you mean I do not need content script? Sorry this is my first project with chrome extensions. – jkwan3 Mar 28 '20 at 08:19
  • The content script is not need in popup.html. The content scripts run in web pages. The popup page is a different page neither related nor connected to web pages. – wOxxOm Mar 28 '20 at 08:20

0 Answers0