Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6370375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:55:22+00:00 2026-05-25T00:55:22+00:00

I have written a Chrome extension that adds a char counter to some input

  • 0

I have written a Chrome extension that adds a char counter to some input elements. There is no UI for this, it just injects the char counting code if the user is on a specific page.

There is a limit to how many chars should appear in the inputs. I initially wrote it to just display the number of chars but then I decided it would be nice if the user could choose to see how many chars they have left instead. So I decided to create an options page.

At some point in my options page I do this:

localStorage["count"] = count; // count is the string "countUp" or "countDown"

It turns out I can’t access the localStorage of options.html from the contentscript.js so I reluctantly created a background.html page which listens to requests from the contentscript and returns the localStorage values that the content script asks for.

In contentscript (simplified)

var countOption;

chrome.extension.sendRequest(
    {method: "localStorage", key: "count"},
    function(response){
        countOption = response.data;
        console.log(countOption); // returns "countUp"
    }
);

console.log(countOption); // returns undefined

if(countOption === "countUp") {
    doSomething();
} else {
    doSomethingElse();
}

Background page

<script type="text/javascript">
    chrome.extension.onRequest.addListener(
        function(request, sender, sendResponse) {
            if(request.method === "localStorage") {
                sendResponse({data: localStorage[request.key]});
            } else {
                sendResponse({});
            }
        }
    );
</script>

The problem is I can’t assign a value to countOption so that it is accessible in the scope of the rest of my script. When I assign it in the chrome.extension.sendRequest‘s response this is the window object so I have tried declaring countOption globally but it is still undefined in the rest of my script.

Can anyone see where I’m going wrong? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:55:23+00:00Added an answer on May 25, 2026 at 12:55 am

    Would something like this work?

    function executeCount(countOption)
    {
    console.log(countOption); // returns undefined
    
    if(countOption === "countUp") {
        doSomething();
    } else {
        doSomethingElse();
    }
    }
    
    chrome.extension.sendRequest(
        {method: "localStorage", key: "count"},
        function(response){
            executeCount(response.data);
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an extension in Google Chrome that opens a tab to a
I've never really written a real Chrome extension. I did one that was just
I have written a very simple Chrome extension. It consists of this background page:
I've written an extension for Google Chrome that will be released with the next
I've written a Google Chrome extension (https://chrome.google.com/webstore/detail/fhmcfamnddgoloojehbeokifhaiiebfm), and I'm noticing that the extension works
I have written an extension for google chrome and I have a bug I
I have a web app written in ASP.NET MVC 3.0. There are some largish
I have written some JavaScript code. It adds new function to NodeList type in
I have written an AIR Application that downloads videos and documents from a server.
I have written some code in my VB.NET application to send an HTML e-mail

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.