Skip to content
Advertisement

Creating and accessing global variable in google chrome extension

All of the information I can find on this is pretty old. Like the title says I am trying to make a global variable in one script and access it from another. The purpose of the extension is to search for a class named “page-title” and then return the innerHTML of that HTML element. Once I get the code working I will specify the URL I want the extension to run on so it’s not constantly running.

After a couple iterations trying to accomplish this in different ways I followed the method explained in this answer but my needs have different requirements and I am receiving the error “Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.” tied to the popup.html. I tried the Unchecked runtime error solution found here but it’s been awhile (~ 7 years) since I’ve dived into any coding and I’m not sure I implemented it correctly. I’ve also tried to pass the value between JS documents is the HTML injection method, but without overriding security defaults in the manifest that doesn’t really work. It also seemed super bootstrappy and I wanted to pass the information in a more conventional way. I tried creating a global variable by simply declaring the variable outside of a function/class/if statement and loading that .js file first, but that was unsuccessful as well.

Manifest

JavaScript

popup.html My popup.html is super simple and really just has a button to press. I included all the .js files in the order I thought necessary

JavaScript

globalVariable.js This one is straight forward. I need to pull the client’s name out of the HTML of the page then use it in an API call when I click the button in popup.js This initializes the variable and uses it as place holder.

JavaScript

ContentScript.js I only want to run this if importScripts is not undefined. So I threw it in the if statement. Then I make sure I pulled a client name from the page. If not I throw an error message saying no client was found.

JavaScript

popup.js This one pulls up the globalVariable.js to use the clientInfo. and makes a call to the button in background.js

JavaScript

background.js Same thing here. I import the globalVariable script to use the global variable. The notification will eventually be replaced with the API call when the rest of the code is working properly. I probably don’t need to import the script here to access the variable because I can mass it with the event listener in popup.js, but I put it in here out of desperation.

JavaScript

Advertisement

Answer

You can have the popup.js listen for a button click and content.js handle all the logic of finding the correct element.

popup.js

JavaScript

content.js

JavaScript

Example of findClientName function:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement