Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to return result of nested function in JavaScript when using addListener?

Okay. So I am very, very new to JavaScript (as in: barely knew any before, and just started actually learning today so I could work on this project). I have a pretty good knowledge of Java, but that's not very useful with the more functional aspects of JS.

I'm currently working on some scripting for a Firefox extension, specifically the context menu part. I'm trying to make an anonymous function (I think that's what it's called; correct me if I'm wrong) so if the right-clicked tab is found in an array in localStorage, then this function returns true and sets the variable to true, therefore making the context menu item have a check next to it.

My issue comes from how the ...onClicked.addListener() returns a prompt

Here's the contextMenus.create() call:

browser.contextMenus.create({
    id: "idPlaceholder",
    type: "checkbox",
    title: "titlePlaceholder",
    contexts: [
        "page",
        "link",
        "tab"
    ],
    checked: checkedState,
}, onCreated);

And here's what I have so far for the problem child — I mean, the checkedState variable declaration:

var checkedState = () => {
    if (JSON.parse(localStorage.getItem('tabs')
        .includes(browser.contextMenus.onClicked.addListener(function (info, tab) {
            //somehow return "info.pageUrl" to .includes() method
        })))) {
    }
};

There's bound to be another way to do this, I just can't seem to find it by looking online. So, I turn to you, Internet.

If you need any more information on what I'm attempting to do, let me know. I tried to not make it too long.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Figured the issue out with help from wOxxOm.

browser.contextMenus.onShown.addListener(function (info, tab) {
    if (JSON.parse(localStorage.getItem('tabs')).includes(info.pageUrl)) {
        browser.contextMenus.update("idPlaceholder", {
            checked: true,
        });
    }
    browser.contextMenus.refresh();
})
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!