SinbadEV
SinbadEV

Reputation: 203

Loop to created multiple Context Menus

I have a chunk of code:

        for (game in settings_object.games)
        {   
            chrome.contextMenus.create({
                "title": "Add thread("+request.thread+") to game: "+game,
                "contexts":["page"],
                "onclick": function () {addThreadToGame(game,request.thread)}
            });

        }

That generates a context menue like:

and the intention is for when the user clicks on "Add thread (1234) to game: ID of Game One" then addThreadToGame("ID of Game One", "1234") get's executed... unfortunately it seems like addThreadToGame is always triggered as addThreadToGame ("ID of Game Three", "1234") because the value being passed to the function is always the last value it has at run time not menu generation time... what am I missing?

Upvotes: 0

Views: 207

Answers (1)

SinbadEV
SinbadEV

Reputation: 203

Worked out the answer:

      for (game in settings_object.games)
      {   
          chrome.contextMenus.create({
              "title": "Add thread("+request.thread+") to game: "+game,
              "contexts":["page"],
              "onclick": (function(element) {
                      return function(info, tab) {
                          addThreadToGame(element,request.thread)
                      }
                  })(game)

          });
      }

after adapting the code I found in selection and site search in chrome extension

Upvotes: 1

Related Questions